static-injector 1.0.10 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/import/commonjs/index.js +331 -118
- package/import/es2015/di/create_injector.js +30 -0
- package/import/es2015/di/initializer_token.js +15 -0
- package/import/es2015/di/inject_switch.js +27 -6
- package/import/es2015/{decorator → di}/injectable.js +0 -0
- package/import/es2015/di/injection_token.js +6 -0
- package/import/es2015/di/injector.js +6 -8
- package/import/es2015/di/injector_compatibility.js +90 -34
- package/import/es2015/di/injector_token.js +1 -1
- package/import/es2015/di/interface/defs.js +39 -0
- package/import/es2015/di/interface/injector.js +1 -0
- package/import/es2015/di/internal_tokens.js +9 -0
- package/import/es2015/di/metadata.js +4 -4
- package/import/es2015/di/provider_collection.js +49 -0
- package/import/es2015/di/r3_injector.js +96 -66
- package/import/es2015/di/scope.js +2 -2
- package/import/es2015/index.js +1 -1
- package/import/es2015/render3/errors_di.js +1 -1
- package/import/fesm2015/index.js +322 -108
- package/import/typings/di/create_injector.d.ts +23 -0
- package/import/typings/di/initializer_token.d.ts +15 -0
- package/import/typings/di/inject_switch.d.ts +7 -2
- package/import/typings/{decorator → di}/injectable.d.ts +1 -1
- package/import/typings/di/injection_token.d.ts +4 -0
- package/import/typings/di/injector.d.ts +20 -7
- package/import/typings/di/injector_compatibility.d.ts +42 -21
- package/import/typings/di/interface/defs.d.ts +10 -0
- package/import/typings/di/interface/injector.d.ts +26 -0
- package/import/typings/di/interface/provider.d.ts +1 -6
- package/import/typings/di/internal_tokens.d.ts +10 -0
- package/import/typings/di/provider_collection.d.ts +30 -0
- package/import/typings/di/r3_injector.d.ts +67 -36
- package/import/typings/di/scope.d.ts +3 -2
- package/import/typings/index.d.ts +1 -1
- package/package.json +4 -2
- package/transform/compiler/index.js +5 -1
- package/transform/compiler/src/injectable_compiler_2.d.ts +7 -36
- package/transform/compiler/src/injectable_compiler_2.js +20 -23
- package/transform/compiler/src/output/output_ast.d.ts +29 -108
- package/transform/compiler/src/output/output_ast.js +66 -188
- package/transform/compiler/src/render3/r3_factory.js +11 -7
- package/transform/compiler/src/render3/util.d.ts +64 -0
- package/transform/compiler/src/render3/util.js +52 -9
- package/transform/compiler/src/render3/view/util.js +5 -1
- package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/util.d.ts +5 -4
- package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/util.js +32 -23
- package/transform/compiler-cli/src/ngtsc/annotations/src/factory.js +1 -1
- package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +33 -21
- package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +6 -2
- package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +5 -1
- package/transform/compiler-cli/src/ngtsc/imports/index.js +5 -1
- package/transform/compiler-cli/src/ngtsc/reflection/index.js +5 -1
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +1 -1
- package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +6 -2
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +2 -2
- package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +36 -23
- package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +18 -13
- package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +5 -1
- package/transform/compiler-cli/src/ngtsc/transform/index.js +5 -1
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +2 -2
- package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +13 -8
- package/transform/compiler-cli/src/ngtsc/translator/index.js +5 -1
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +3 -3
- package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +7 -3
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +0 -4
- package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +5 -17
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +3 -6
- package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +67 -55
- package/{import/es2015/decorator/interface/provider.js → transform/compiler-cli/src/ngtsc/ts_compatibility/index.d.ts} +1 -1
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/index.js +24 -0
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.d.ts +151 -0
- package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.js +210 -0
- package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +5 -1
- package/transform/index.js +5 -1
- package/transform/injectable-transform.js +30 -38
- package/import/typings/decorator/interface/provider.d.ts +0 -312
- package/transform/compiler/src/render3/partial/util.d.ts +0 -16
- package/transform/compiler/src/render3/partial/util.js +0 -44
|
@@ -6,14 +6,18 @@
|
|
|
6
6
|
* found in the LICENSE file at https://angular.io/license
|
|
7
7
|
*/
|
|
8
8
|
import { getFactoryDef } from '../render3/definition_factory';
|
|
9
|
-
import {
|
|
9
|
+
import { newArray } from '../util/array_utils';
|
|
10
|
+
import { EMPTY_ARRAY } from '../util/empty';
|
|
10
11
|
import { stringify } from '../util/stringify';
|
|
11
12
|
import { resolveForwardRef } from './forward_ref';
|
|
13
|
+
import { ENVIRONMENT_INITIALIZER } from './initializer_token';
|
|
12
14
|
import { InjectionToken } from './injection_token';
|
|
13
|
-
import { catchInjectorError, injectArgs, NG_TEMP_TOKEN_PATH, setCurrentInjector, THROW_IF_NOT_FOUND, USE_VALUE, ɵɵinject, } from './injector_compatibility';
|
|
15
|
+
import { catchInjectorError, convertToBitFlags, injectArgs, NG_TEMP_TOKEN_PATH, setCurrentInjector, THROW_IF_NOT_FOUND, USE_VALUE, ɵɵinject, } from './injector_compatibility';
|
|
14
16
|
import { INJECTOR } from './injector_token';
|
|
15
|
-
import {
|
|
17
|
+
import { setInjectImplementation } from './inject_switch';
|
|
18
|
+
import { getInheritedInjectableDef, getInjectableDef, } from './interface/defs';
|
|
16
19
|
import { InjectFlags } from './interface/injector';
|
|
20
|
+
import { INJECTOR_DEF_TYPES } from './internal_tokens';
|
|
17
21
|
import { NullInjector } from './null_injector';
|
|
18
22
|
import { INJECTOR_SCOPE } from './scope';
|
|
19
23
|
/**
|
|
@@ -32,61 +36,51 @@ const CIRCULAR = {};
|
|
|
32
36
|
* A lazily initialized NullInjector.
|
|
33
37
|
*/
|
|
34
38
|
let NULL_INJECTOR = undefined;
|
|
35
|
-
function getNullInjector() {
|
|
39
|
+
export function getNullInjector() {
|
|
36
40
|
if (NULL_INJECTOR === undefined) {
|
|
37
41
|
NULL_INJECTOR = new NullInjector();
|
|
38
42
|
}
|
|
39
43
|
return NULL_INJECTOR;
|
|
40
44
|
}
|
|
41
45
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @publicApi
|
|
46
|
+
* An `Injector` that's part of the environment injector hierarchy, which exists outside of the
|
|
47
|
+
* component tree.
|
|
45
48
|
*/
|
|
46
|
-
export
|
|
47
|
-
const injector = createInjectorWithoutInjectorInstances(defType, parent, additionalProviders, name);
|
|
48
|
-
injector._resolveInjectorDefTypes();
|
|
49
|
-
return injector;
|
|
49
|
+
export class EnvironmentInjector {
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
* should be resolved at a later point by calling `_resolveInjectorDefTypes`.
|
|
55
|
-
*/
|
|
56
|
-
export function createInjectorWithoutInjectorInstances(defType, parent = null, additionalProviders = null, name) {
|
|
57
|
-
return new R3Injector(defType, additionalProviders, parent || getNullInjector(), name);
|
|
58
|
-
}
|
|
59
|
-
export class R3Injector {
|
|
60
|
-
constructor(def, additionalProviders, parent, source = null) {
|
|
51
|
+
export class R3Injector extends EnvironmentInjector {
|
|
52
|
+
constructor(providers, parent, source, scopes) {
|
|
53
|
+
super();
|
|
61
54
|
this.parent = parent;
|
|
55
|
+
this.source = source;
|
|
56
|
+
this.scopes = scopes;
|
|
62
57
|
/**
|
|
63
58
|
* Map of tokens to records which contain the instances of those tokens.
|
|
64
59
|
* - `null` value implies that we don't have the record. Used by tree-shakable injectors
|
|
65
60
|
* to prevent further searches.
|
|
66
61
|
*/
|
|
67
62
|
this.records = new Map();
|
|
68
|
-
/**
|
|
69
|
-
* The transitive set of `InjectorType`s which define this injector.
|
|
70
|
-
*/
|
|
71
|
-
this.injectorDefTypes = new Set();
|
|
72
63
|
/**
|
|
73
64
|
* Set of values instantiated by this injector which contain `ngOnDestroy` lifecycle hooks.
|
|
74
65
|
*/
|
|
75
|
-
this.
|
|
66
|
+
this._ngOnDestroyHooks = new Set();
|
|
67
|
+
this._onDestroyHooks = [];
|
|
76
68
|
this._destroyed = false;
|
|
77
|
-
// Start off by creating Records for every provider
|
|
78
|
-
|
|
79
|
-
// important because `def` may include providers that override ones in additionalProviders.
|
|
80
|
-
additionalProviders &&
|
|
81
|
-
deepForEach(additionalProviders, (provider) => this.processProvider(provider, def, additionalProviders));
|
|
69
|
+
// Start off by creating Records for every provider.
|
|
70
|
+
forEachSingleProvider(providers, (provider) => this.processProvider(provider));
|
|
82
71
|
// Make sure the INJECTOR token provides this injector.
|
|
83
72
|
this.records.set(INJECTOR, makeRecord(undefined, this));
|
|
73
|
+
// And `EnvironmentInjector` if the current injector is supposed to be env-scoped.
|
|
74
|
+
if (scopes.has('environment')) {
|
|
75
|
+
this.records.set(EnvironmentInjector, makeRecord(undefined, this));
|
|
76
|
+
}
|
|
84
77
|
// Detect whether this injector has the APP_ROOT_SCOPE token and thus should provide
|
|
85
78
|
// any injectable scoped to APP_ROOT_SCOPE.
|
|
86
79
|
const record = this.records.get(INJECTOR_SCOPE);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
80
|
+
if (record != null && typeof record.value === 'string') {
|
|
81
|
+
this.scopes.add(record.value);
|
|
82
|
+
}
|
|
83
|
+
this.injectorDefTypes = new Set(this.get(INJECTOR_DEF_TYPES.multi, EMPTY_ARRAY, InjectFlags.Self));
|
|
90
84
|
}
|
|
91
85
|
/**
|
|
92
86
|
* Flag indicating that this injector was previously destroyed.
|
|
@@ -106,19 +100,42 @@ export class R3Injector {
|
|
|
106
100
|
this._destroyed = true;
|
|
107
101
|
try {
|
|
108
102
|
// Call all the lifecycle hooks.
|
|
109
|
-
|
|
103
|
+
for (const service of this._ngOnDestroyHooks) {
|
|
104
|
+
service.ngOnDestroy();
|
|
105
|
+
}
|
|
106
|
+
for (const hook of this._onDestroyHooks) {
|
|
107
|
+
hook();
|
|
108
|
+
}
|
|
110
109
|
}
|
|
111
110
|
finally {
|
|
112
111
|
// Release all references.
|
|
113
112
|
this.records.clear();
|
|
114
|
-
this.
|
|
113
|
+
this._ngOnDestroyHooks.clear();
|
|
115
114
|
this.injectorDefTypes.clear();
|
|
115
|
+
this._onDestroyHooks.length = 0;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
onDestroy(callback) {
|
|
119
|
+
this._onDestroyHooks.push(callback);
|
|
120
|
+
}
|
|
121
|
+
runInContext(fn) {
|
|
122
|
+
this.assertNotDestroyed();
|
|
123
|
+
const previousInjector = setCurrentInjector(this);
|
|
124
|
+
const previousInjectImplementation = setInjectImplementation(undefined);
|
|
125
|
+
try {
|
|
126
|
+
return fn();
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
setCurrentInjector(previousInjector);
|
|
130
|
+
setInjectImplementation(previousInjectImplementation);
|
|
116
131
|
}
|
|
117
132
|
}
|
|
118
133
|
get(token, notFoundValue = THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
|
|
119
134
|
this.assertNotDestroyed();
|
|
135
|
+
flags = convertToBitFlags(flags);
|
|
120
136
|
// Set the injection context.
|
|
121
137
|
const previousInjector = setCurrentInjector(this);
|
|
138
|
+
const previousInjectImplementation = setInjectImplementation(undefined);
|
|
122
139
|
try {
|
|
123
140
|
// Check for the SkipSelf flag.
|
|
124
141
|
if (!(flags & InjectFlags.SkipSelf)) {
|
|
@@ -176,16 +193,31 @@ export class R3Injector {
|
|
|
176
193
|
}
|
|
177
194
|
finally {
|
|
178
195
|
// Lastly, restore the previous injection context.
|
|
196
|
+
setInjectImplementation(previousInjectImplementation);
|
|
179
197
|
setCurrentInjector(previousInjector);
|
|
180
198
|
}
|
|
181
199
|
}
|
|
182
200
|
/** @internal */
|
|
183
|
-
|
|
184
|
-
|
|
201
|
+
resolveInjectorInitializers() {
|
|
202
|
+
const previousInjector = setCurrentInjector(this);
|
|
203
|
+
const previousInjectImplementation = setInjectImplementation(undefined);
|
|
204
|
+
try {
|
|
205
|
+
const initializers = this.get(ENVIRONMENT_INITIALIZER.multi, EMPTY_ARRAY, InjectFlags.Self);
|
|
206
|
+
for (const initializer of initializers) {
|
|
207
|
+
initializer();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
setCurrentInjector(previousInjector);
|
|
212
|
+
setInjectImplementation(previousInjectImplementation);
|
|
213
|
+
}
|
|
185
214
|
}
|
|
186
215
|
toString() {
|
|
187
|
-
const tokens = []
|
|
188
|
-
records
|
|
216
|
+
const tokens = [];
|
|
217
|
+
const records = this.records;
|
|
218
|
+
for (const token of records.keys()) {
|
|
219
|
+
tokens.push(stringify(token));
|
|
220
|
+
}
|
|
189
221
|
return `R3Injector[${tokens.join(', ')}]`;
|
|
190
222
|
}
|
|
191
223
|
assertNotDestroyed() {
|
|
@@ -193,19 +225,10 @@ export class R3Injector {
|
|
|
193
225
|
throw new Error('Injector has already been destroyed.');
|
|
194
226
|
}
|
|
195
227
|
}
|
|
196
|
-
/**
|
|
197
|
-
* Add an `InjectorType` or `InjectorTypeWithProviders` and all of its transitive providers
|
|
198
|
-
* to this injector.
|
|
199
|
-
*
|
|
200
|
-
* If an `InjectorTypeWithProviders` that declares providers besides the type is specified,
|
|
201
|
-
* the function will return "true" to indicate that the providers of the type definition need
|
|
202
|
-
* to be processed. This allows us to process providers of injector types after all imports of
|
|
203
|
-
* an injector definition are processed. (following View Engine semantics: see FW-1349)
|
|
204
|
-
*/
|
|
205
228
|
/**
|
|
206
229
|
* Process a `SingleProvider` and add it.
|
|
207
230
|
*/
|
|
208
|
-
processProvider(provider
|
|
231
|
+
processProvider(provider) {
|
|
209
232
|
// Determine the token from the provider. Either it's its own token, or has a {provide: ...}
|
|
210
233
|
// property.
|
|
211
234
|
provider = resolveForwardRef(provider);
|
|
@@ -213,16 +236,13 @@ export class R3Injector {
|
|
|
213
236
|
? provider
|
|
214
237
|
: resolveForwardRef(provider && provider.provide);
|
|
215
238
|
// Construct a `Record` for the provider.
|
|
216
|
-
const record = providerToRecord(provider
|
|
239
|
+
const record = providerToRecord(provider);
|
|
217
240
|
if (!isTypeProvider(provider) && provider.multi === true) {
|
|
218
241
|
// If the provider indicates that it's a multi-provider, process it specially.
|
|
219
242
|
// First check whether it's been defined already.
|
|
220
243
|
let multiRecord = this.records.get(token);
|
|
221
244
|
if (multiRecord) {
|
|
222
245
|
// It has. Throw a nice error if
|
|
223
|
-
// if (ngDevMode && multiRecord.multi === undefined) {
|
|
224
|
-
// throwMixedMultiProviderError();
|
|
225
|
-
// }
|
|
226
246
|
}
|
|
227
247
|
else {
|
|
228
248
|
multiRecord = makeRecord(undefined, NOT_YET, true);
|
|
@@ -233,17 +253,11 @@ export class R3Injector {
|
|
|
233
253
|
multiRecord.multi.push(provider);
|
|
234
254
|
}
|
|
235
255
|
else {
|
|
236
|
-
|
|
237
|
-
// if (ngDevMode && existing && existing.multi !== undefined) {
|
|
238
|
-
// throwMixedMultiProviderError();
|
|
239
|
-
// }
|
|
256
|
+
const existing = this.records.get(token);
|
|
240
257
|
}
|
|
241
258
|
this.records.set(token, record);
|
|
242
259
|
}
|
|
243
260
|
hydrate(token, record) {
|
|
244
|
-
// if (ngDevMode && record.value === CIRCULAR) {
|
|
245
|
-
// throwCyclicDependencyError(stringify(token));
|
|
246
|
-
// } else
|
|
247
261
|
if (record.value === NOT_YET) {
|
|
248
262
|
record.value = CIRCULAR;
|
|
249
263
|
record.value = record.factory();
|
|
@@ -251,7 +265,7 @@ export class R3Injector {
|
|
|
251
265
|
if (typeof record.value === 'object' &&
|
|
252
266
|
record.value &&
|
|
253
267
|
hasOnDestroy(record.value)) {
|
|
254
|
-
this.
|
|
268
|
+
this._ngOnDestroyHooks.add(record.value);
|
|
255
269
|
}
|
|
256
270
|
return record.value;
|
|
257
271
|
}
|
|
@@ -261,7 +275,7 @@ export class R3Injector {
|
|
|
261
275
|
}
|
|
262
276
|
const providedIn = resolveForwardRef(def.providedIn);
|
|
263
277
|
if (typeof providedIn === 'string') {
|
|
264
|
-
return providedIn === 'any' ||
|
|
278
|
+
return providedIn === 'any' || this.scopes.has(providedIn);
|
|
265
279
|
}
|
|
266
280
|
else {
|
|
267
281
|
return this.injectorDefTypes.has(providedIn);
|
|
@@ -299,14 +313,20 @@ function getUndecoratedInjectableFactory(token) {
|
|
|
299
313
|
// def from an ancestor if there is one.
|
|
300
314
|
// Otherwise this really is a simple class with no dependencies, so return a factory that
|
|
301
315
|
// just instantiates the zero-arg constructor.
|
|
302
|
-
|
|
316
|
+
const inheritedInjectableDef = getInheritedInjectableDef(token);
|
|
317
|
+
if (inheritedInjectableDef !== null) {
|
|
318
|
+
return () => inheritedInjectableDef.factory(token);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
return () => new token();
|
|
322
|
+
}
|
|
303
323
|
}
|
|
304
|
-
function providerToRecord(provider
|
|
324
|
+
function providerToRecord(provider) {
|
|
305
325
|
if (isValueProvider(provider)) {
|
|
306
326
|
return makeRecord(undefined, provider.useValue);
|
|
307
327
|
}
|
|
308
328
|
else {
|
|
309
|
-
const factory = providerToFactory(provider
|
|
329
|
+
const factory = providerToFactory(provider);
|
|
310
330
|
return makeRecord(factory, NOT_YET);
|
|
311
331
|
}
|
|
312
332
|
}
|
|
@@ -380,3 +400,13 @@ function couldBeInjectableType(value) {
|
|
|
380
400
|
return (typeof value === 'function' ||
|
|
381
401
|
(typeof value === 'object' && value instanceof InjectionToken));
|
|
382
402
|
}
|
|
403
|
+
function forEachSingleProvider(providers, fn) {
|
|
404
|
+
for (const provider of providers) {
|
|
405
|
+
if (Array.isArray(provider)) {
|
|
406
|
+
forEachSingleProvider(provider, fn);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
fn(provider);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* Use of this source code is governed by an MIT-style license that can be
|
|
6
6
|
* found in the LICENSE file at https://angular.io/license
|
|
7
7
|
*/
|
|
8
|
-
import { InjectionToken } from
|
|
8
|
+
import { InjectionToken } from './injection_token';
|
|
9
9
|
/**
|
|
10
10
|
* An internal token whose presence in an injector indicates that the injector should treat itself
|
|
11
11
|
* as a root scoped injector when processing requests for unknown tokens which may indicate
|
|
12
12
|
* they are provided in the root scope.
|
|
13
13
|
*/
|
|
14
|
-
export const INJECTOR_SCOPE = new InjectionToken(
|
|
14
|
+
export const INJECTOR_SCOPE = new InjectionToken('Set Injector scope.');
|
package/import/es2015/index.js
CHANGED
|
@@ -10,5 +10,5 @@ import { stringifyForError } from './util/stringify_utils';
|
|
|
10
10
|
/** Throws an error when a token is not found in DI. */
|
|
11
11
|
export function throwProviderNotFoundError(token, injectorName) {
|
|
12
12
|
const injectorDetails = injectorName ? ` in ${injectorName}` : '';
|
|
13
|
-
throw new RuntimeError("201" /* PROVIDER_NOT_FOUND */, `No provider for ${stringifyForError(token)} found${injectorDetails}`);
|
|
13
|
+
throw new RuntimeError("201" /* RuntimeErrorCode.PROVIDER_NOT_FOUND */, `No provider for ${stringifyForError(token)} found${injectorDetails}`);
|
|
14
14
|
}
|