forked from angular/di.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injector.js
311 lines (243 loc) · 8.92 KB
/
injector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import {
annotate,
readAnnotations,
hasAnnotation,
Provide as ProvideAnnotation,
TransientScope as TransientScopeAnnotation
} from './annotations';
import {isFunction, toString} from './util';
import {getUniqueId} from './profiler';
import {createProviderFromFnOrClass} from './providers';
function constructResolvingMessage(resolving, token = null) {
if (token) {
resolving.push(token);
}
if (resolving.length > 1) {
return ` (${resolving.map(toString).join(' -> ')})`;
}
return '';
}
// Injector encapsulate a life scope.
// There is exactly one instance for given token in given injector.
//
// All the state is immutable, the only state changes is the cache. There is however no way to produce different instance under given token. In that sense it is immutable.
//
// Injector is responsible for:
// - resolving tokens into
// - provider
// - value (cache/calling provider)
// - dealing with isPromise
// - dealing with isLazy
// - loading different "providers" and modules
class Injector {
constructor(modules = [], parentInjector = null, providers = new Map()) {
this.cache = new Map();
this.providers = providers;
this.parent = parentInjector;
this.id = getUniqueId();
this._loadModules(modules);
}
// Collect all registered providers that has given annotation.
// Inclugind providers defined in parent injectors.
_collectProvidersWithAnnotation(annotationClass, collectedProviders) {
this.providers.forEach((provider, token) => {
if (!collectedProviders.has(token) && hasAnnotation(provider.provider, annotationClass)) {
collectedProviders.set(token, provider);
}
});
if (this.parent) {
this.parent._collectProvidersWithAnnotation(annotationClass, collectedProviders);
}
}
// Load modules/function/classes.
// This mutates `this.providers`, but it is only called during the constructor.
_loadModules(modules) {
for (var module of modules) {
// A single provider.
if (isFunction(module)) {
this._loadFnOrClass(module);
continue;
}
// A module (map of providers).
Object.keys(module).forEach((key) => {
if (isFunction(module[key])) {
this._loadFnOrClass(module[key], key);
}
});
}
}
// Load a function or class.
// This mutates `this.providers`, but it is only called during the constructor.
_loadFnOrClass(fnOrClass, key) {
// TODO(vojta): should we expose provider.token?
var annotations = readAnnotations(fnOrClass);
var token = annotations.provide.token || key || fnOrClass;
var provider = createProviderFromFnOrClass(fnOrClass, annotations);
this.providers.set(token, provider);
}
// Returns true if there is any provider registered for given token.
// Inclugind parent injectors.
_hasProviderFor(token) {
if (this.providers.has(token)) {
return true;
}
if (this.parent) {
return this.parent._hasProviderFor(token);
}
return false;
}
// Return an instance for given token.
get(token, resolving = [], wantPromise = false, wantLazy = false) {
var resolvingMsg = '';
var instance;
var injector = this;
// Special case, return itself.
if (token === Injector) {
if (wantPromise) {
return Promise.resolve(this);
}
return this;
}
// TODO(vojta): optimize - no child injector for locals?
if (wantLazy) {
return function createLazyInstance() {
var lazyInjector = injector;
if (arguments.length) {
var locals = [];
var args = arguments;
for (var i = 0; i < args.length; i += 2) {
locals.push((function(ii) {
var fn = function createLocalInstance() {
return args[ii + 1];
};
annotate(fn, new ProvideAnnotation(args[ii]));
return fn;
})(i));
}
lazyInjector = injector.createChild(locals);
}
return lazyInjector.get(token, resolving, wantPromise, false);
};
}
// Check if there is a cached instance already.
if (this.cache.has(token)) {
instance = this.cache.get(token);
if (this.providers.get(token).isPromise) {
if (!wantPromise) {
resolvingMsg = constructResolvingMessage(resolving, token);
throw new Error(`Cannot instantiate ${toString(token)} synchronously. It is provided as a promise!${resolvingMsg}`);
}
} else {
if (wantPromise) {
return Promise.resolve(instance);
}
}
return instance;
}
var provider = this.providers.get(token);
// No provider defined (overriden), use the default provider (token).
if (!provider && isFunction(token) && !this._hasProviderFor(token)) {
provider = createProviderFromFnOrClass(token, readAnnotations(token));
this.providers.set(token, provider);
}
if (!provider) {
if (!this.parent) {
resolvingMsg = constructResolvingMessage(resolving, token);
throw new Error(`No provider for ${toString(token)}!${resolvingMsg}`);
}
return this.parent.get(token, resolving, wantPromise, wantLazy);
}
if (resolving.indexOf(token) !== -1) {
resolvingMsg = constructResolvingMessage(resolving, token);
throw new Error(`Cannot instantiate cyclic dependency!${resolvingMsg}`);
}
resolving.push(token);
// TODO(vojta): handle these cases:
// 1/
// - requested as promise (delayed)
// - requested again as promise (before the previous gets resolved) -> cache the promise
// 2/
// - requested as promise (delayed)
// - requested again sync (before the previous gets resolved)
// -> error, but let it go inside to throw where exactly is the async provider
var delayingInstantiation = wantPromise && provider.params.some((param) => !param.isPromise);
var args = provider.params.map((param) => {
if (delayingInstantiation) {
return this.get(param.token, resolving, true, param.isLazy);
}
return this.get(param.token, resolving, param.isPromise, param.isLazy);
});
// Delaying the instantiation - return a promise.
if (delayingInstantiation) {
var delayedResolving = resolving.slice(); // clone
resolving.pop();
// Once all dependencies (promises) are resolved, instantiate.
return Promise.all(args).then(function(args) {
try {
instance = provider.create(args);
} catch (e) {
resolvingMsg = constructResolvingMessage(delayedResolving);
var originalMsg = 'ORIGINAL ERROR: ' + e.message;
e.message = `Error during instantiation of ${toString(token)}!${resolvingMsg}\n${originalMsg}`;
throw e;
}
if (!hasAnnotation(provider.provider, TransientScopeAnnotation)) {
injector.cache.set(token, instance);
}
// TODO(vojta): if a provider returns a promise (but is not declared as @ProvidePromise),
// here the value will get unwrapped (because it is returned from a promise callback) and
// the actual value will be injected. This is probably not desired behavior. Maybe we could
// get rid off the @ProvidePromise and just check the returned value, whether it is
// a promise or not.
return instance;
});
}
try {
instance = provider.create(args);
} catch (e) {
resolvingMsg = constructResolvingMessage(resolving);
var originalMsg = 'ORIGINAL ERROR: ' + e.message;
e.message = `Error during instantiation of ${toString(token)}!${resolvingMsg}\n${originalMsg}`;
throw e;
}
if (!hasAnnotation(provider.provider, TransientScopeAnnotation)) {
this.cache.set(token, instance);
}
if (!wantPromise && provider.isPromise) {
resolvingMsg = constructResolvingMessage(resolving);
throw new Error(`Cannot instantiate ${toString(token)} synchronously. It is provided as a promise!${resolvingMsg}`);
}
if (wantPromise && !provider.isPromise) {
instance = Promise.resolve(instance);
}
resolving.pop();
return instance;
}
getPromise(token) {
return this.get(token, [], true);
}
// Create a child injector, which encapsulate shorter life scope.
// It is possible to add additional providers and also force new instances of existing providers.
createChild(modules = [], forceNewInstancesOf = []) {
var forcedProviders = new Map();
for (var annotation of forceNewInstancesOf) {
this._collectProvidersWithAnnotation(annotation, forcedProviders);
}
return new Injector(modules, this, forcedProviders);
}
dump() {
var serialized = {
id: this.id,
parent_id: this.parent ? this.parent.id : null,
providers: {}
};
Object.keys(this.providers).forEach((token) => {
serialized.providers[token] = {
name: token,
dependencies: this.providers[token].params
};
});
return serialized;
}
}
export {Injector};