static-injector 1.0.8

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.
Files changed (146) hide show
  1. package/import/commonjs/index.js +1248 -0
  2. package/import/es2015/decorator/injectable.js +14 -0
  3. package/import/es2015/decorator/interface/provider.js +8 -0
  4. package/import/es2015/di/forward_ref.js +53 -0
  5. package/import/es2015/di/inject_switch.js +31 -0
  6. package/import/es2015/di/injection_token.js +71 -0
  7. package/import/es2015/di/injector.js +63 -0
  8. package/import/es2015/di/injector_compatibility.js +172 -0
  9. package/import/es2015/di/injector_marker.js +8 -0
  10. package/import/es2015/di/injector_token.js +21 -0
  11. package/import/es2015/di/interface/defs.js +72 -0
  12. package/import/es2015/di/interface/injector.js +25 -0
  13. package/import/es2015/di/interface/provider.js +8 -0
  14. package/import/es2015/di/metadata.js +49 -0
  15. package/import/es2015/di/null_injector.js +19 -0
  16. package/import/es2015/di/provider_token.js +8 -0
  17. package/import/es2015/di/r3_injector.js +382 -0
  18. package/import/es2015/di/scope.js +14 -0
  19. package/import/es2015/index.js +11 -0
  20. package/import/es2015/interface/lifecycle_hooks.js +8 -0
  21. package/import/es2015/interface/type.js +8 -0
  22. package/import/es2015/render3/definition_factory.js +15 -0
  23. package/import/es2015/render3/error_code.js +29 -0
  24. package/import/es2015/render3/errors_di.js +14 -0
  25. package/import/es2015/render3/fields.js +11 -0
  26. package/import/es2015/render3/instructions/di.js +23 -0
  27. package/import/es2015/render3/util/stringify_utils.js +37 -0
  28. package/import/es2015/util/array_utils.js +17 -0
  29. package/import/es2015/util/closure.js +19 -0
  30. package/import/es2015/util/decorators.js +30 -0
  31. package/import/es2015/util/empty.js +14 -0
  32. package/import/es2015/util/property.js +15 -0
  33. package/import/es2015/util/stringify.js +30 -0
  34. package/import/fesm2015/index.js +1211 -0
  35. package/import/typings/decorator/injectable.d.ts +82 -0
  36. package/import/typings/decorator/interface/provider.d.ts +312 -0
  37. package/import/typings/di/forward_ref.d.ts +49 -0
  38. package/import/typings/di/inject_switch.d.ts +17 -0
  39. package/import/typings/di/injection_token.d.ts +57 -0
  40. package/import/typings/di/injector.d.ts +76 -0
  41. package/import/typings/di/injector_compatibility.d.ts +77 -0
  42. package/import/typings/di/injector_marker.d.ts +23 -0
  43. package/import/typings/di/injector_token.d.ts +18 -0
  44. package/import/typings/di/interface/defs.d.ts +155 -0
  45. package/import/typings/di/interface/injector.d.ts +48 -0
  46. package/import/typings/di/interface/provider.d.ts +312 -0
  47. package/import/typings/di/metadata.d.ts +176 -0
  48. package/import/typings/di/null_injector.d.ts +11 -0
  49. package/import/typings/di/provider_token.d.ts +17 -0
  50. package/import/typings/di/r3_injector.d.ts +92 -0
  51. package/import/typings/di/scope.d.ts +14 -0
  52. package/import/typings/index.d.ts +11 -0
  53. package/import/typings/interface/lifecycle_hooks.d.ts +28 -0
  54. package/import/typings/interface/type.d.ts +21 -0
  55. package/import/typings/render3/definition_factory.d.ts +24 -0
  56. package/import/typings/render3/error_code.d.ts +24 -0
  57. package/import/typings/render3/errors_di.d.ts +2 -0
  58. package/import/typings/render3/fields.d.ts +8 -0
  59. package/import/typings/render3/instructions/di.d.ts +20 -0
  60. package/import/typings/render3/util/stringify_utils.d.ts +20 -0
  61. package/import/typings/util/array_utils.d.ts +10 -0
  62. package/import/typings/util/closure.d.ts +17 -0
  63. package/import/typings/util/decorators.d.ts +27 -0
  64. package/import/typings/util/empty.d.ts +14 -0
  65. package/import/typings/util/property.d.ts +8 -0
  66. package/import/typings/util/stringify.d.ts +8 -0
  67. package/package.json +34 -0
  68. package/readme.md +27 -0
  69. package/transform/compiler/index.d.ts +5 -0
  70. package/transform/compiler/index.js +17 -0
  71. package/transform/compiler/src/core.d.ts +19 -0
  72. package/transform/compiler/src/core.js +9 -0
  73. package/transform/compiler/src/injectable_compiler_2.d.ts +53 -0
  74. package/transform/compiler/src/injectable_compiler_2.js +159 -0
  75. package/transform/compiler/src/output/output_ast.d.ts +534 -0
  76. package/transform/compiler/src/output/output_ast.js +986 -0
  77. package/transform/compiler/src/parse_util.d.ts +50 -0
  78. package/transform/compiler/src/parse_util.js +61 -0
  79. package/transform/compiler/src/render3/partial/api.d.ts +166 -0
  80. package/transform/compiler/src/render3/partial/api.js +11 -0
  81. package/transform/compiler/src/render3/partial/util.d.ts +16 -0
  82. package/transform/compiler/src/render3/partial/util.js +44 -0
  83. package/transform/compiler/src/render3/r3_factory.d.ts +97 -0
  84. package/transform/compiler/src/render3/r3_factory.js +239 -0
  85. package/transform/compiler/src/render3/r3_identifiers.d.ts +20 -0
  86. package/transform/compiler/src/render3/r3_identifiers.js +51 -0
  87. package/transform/compiler/src/render3/util.d.ts +21 -0
  88. package/transform/compiler/src/render3/util.js +41 -0
  89. package/transform/compiler/src/render3/view/util.d.ts +30 -0
  90. package/transform/compiler/src/render3/view/util.js +57 -0
  91. package/transform/compiler-cli/src/ngtsc/annotations/src/factory.d.ts +11 -0
  92. package/transform/compiler-cli/src/ngtsc/annotations/src/factory.js +21 -0
  93. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.d.ts +42 -0
  94. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +278 -0
  95. package/transform/compiler-cli/src/ngtsc/annotations/src/util.d.ts +91 -0
  96. package/transform/compiler-cli/src/ngtsc/annotations/src/util.js +370 -0
  97. package/transform/compiler-cli/src/ngtsc/diagnostics/error.d.ts +24 -0
  98. package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +76 -0
  99. package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.d.ts +188 -0
  100. package/transform/compiler-cli/src/ngtsc/diagnostics/error_code.js +206 -0
  101. package/transform/compiler-cli/src/ngtsc/diagnostics/index.d.ts +2 -0
  102. package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +14 -0
  103. package/transform/compiler-cli/src/ngtsc/imports/index.d.ts +1 -0
  104. package/transform/compiler-cli/src/ngtsc/imports/index.js +13 -0
  105. package/transform/compiler-cli/src/ngtsc/imports/src/core.d.ts +36 -0
  106. package/transform/compiler-cli/src/ngtsc/imports/src/core.js +25 -0
  107. package/transform/compiler-cli/src/ngtsc/reflection/index.d.ts +3 -0
  108. package/transform/compiler-cli/src/ngtsc/reflection/index.js +15 -0
  109. package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +667 -0
  110. package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +98 -0
  111. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +24 -0
  112. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +271 -0
  113. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.d.ts +50 -0
  114. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +396 -0
  115. package/transform/compiler-cli/src/ngtsc/reflection/src/util.d.ts +12 -0
  116. package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +45 -0
  117. package/transform/compiler-cli/src/ngtsc/transform/index.d.ts +2 -0
  118. package/transform/compiler-cli/src/ngtsc/transform/index.js +14 -0
  119. package/transform/compiler-cli/src/ngtsc/transform/src/api.d.ts +115 -0
  120. package/transform/compiler-cli/src/ngtsc/transform/src/api.js +30 -0
  121. package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +15 -0
  122. package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +84 -0
  123. package/transform/compiler-cli/src/ngtsc/translator/index.d.ts +5 -0
  124. package/transform/compiler-cli/src/ngtsc/translator/index.js +17 -0
  125. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.d.ts +274 -0
  126. package/transform/compiler-cli/src/ngtsc/translator/src/api/ast_factory.js +9 -0
  127. package/transform/compiler-cli/src/ngtsc/translator/src/api/import_generator.d.ts +27 -0
  128. package/transform/compiler-cli/src/ngtsc/translator/src/api/import_generator.js +9 -0
  129. package/transform/compiler-cli/src/ngtsc/translator/src/context.d.ts +18 -0
  130. package/transform/compiler-cli/src/ngtsc/translator/src/context.js +27 -0
  131. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +29 -0
  132. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +70 -0
  133. package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +58 -0
  134. package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +270 -0
  135. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +54 -0
  136. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +245 -0
  137. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.d.ts +13 -0
  138. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_translator.js +21 -0
  139. package/transform/compiler-cli/src/ngtsc/util/src/typescript.d.ts +11 -0
  140. package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +39 -0
  141. package/transform/index.d.ts +1 -0
  142. package/transform/index.js +13 -0
  143. package/transform/injectable-transform.d.ts +41 -0
  144. package/transform/injectable-transform.js +195 -0
  145. package/transform/node-Iteration.d.ts +2 -0
  146. package/transform/node-Iteration.js +12 -0
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { getClosureSafeProperty } from '../../util/property';
9
+ /**
10
+ * Construct an injectable definition which defines how a token will be constructed by the DI
11
+ * system, and in which injectors (if any) it will be available.
12
+ *
13
+ * This should be assigned to a static `ɵprov` field on a type, which will then be an
14
+ * `InjectableType`.
15
+ *
16
+ * Options:
17
+ * * `providedIn` determines which injectors will include the injectable, by either associating it
18
+ * with an `@NgModule` or other `InjectorType`, or by specifying that this injectable should be
19
+ * provided in the `'root'` injector, which will be the application-level injector in most apps.
20
+ * * `factory` gives the zero argument function which will create an instance of the injectable.
21
+ * The factory can call `inject` to access the `Injector` and request injection of dependencies.
22
+ *
23
+ * @codeGenApi
24
+ * @publicApi This instruction has been emitted by ViewEngine for some time and is deployed to npm.
25
+ */
26
+ export function ɵɵdefineInjectable(opts) {
27
+ return {
28
+ token: opts.token,
29
+ providedIn: opts.providedIn || null,
30
+ factory: opts.factory,
31
+ value: undefined,
32
+ };
33
+ }
34
+ /**
35
+ * Construct an `InjectorDef` which configures an injector.
36
+ *
37
+ * This should be assigned to a static injector def (`ɵinj`) field on a type, which will then be an
38
+ * `InjectorType`.
39
+ *
40
+ * Options:
41
+ *
42
+ * * `providers`: an optional array of providers to add to the injector. Each provider must
43
+ * either have a factory or point to a type which has a `ɵprov` static property (the
44
+ * type must be an `InjectableType`).
45
+ * * `imports`: an optional array of imports of other `InjectorType`s or `InjectorTypeWithModule`s
46
+ * whose providers will also be added to the injector. Locally provided types will override
47
+ * providers from imports.
48
+ *
49
+ * @codeGenApi
50
+ */
51
+ export function ɵɵdefineInjector(options) {
52
+ return { providers: options.providers || [], imports: options.imports || [] };
53
+ }
54
+ /**
55
+ * Read the injectable def (`ɵprov`) for `type` in a way which is immune to accidentally reading
56
+ * inherited value.
57
+ *
58
+ * @param type A type which may have its own (non-inherited) `ɵprov`.
59
+ */
60
+ export function getInjectableDef(type) {
61
+ return getOwnDefinition(type, NG_PROV_DEF);
62
+ }
63
+ /**
64
+ * Return definition only if it is defined directly on `type` and is not inherited from a base
65
+ * class of `type`.
66
+ */
67
+ function getOwnDefinition(type, field) {
68
+ return type.hasOwnProperty(field) ? type[field] : null;
69
+ }
70
+ export const NG_PROV_DEF = getClosureSafeProperty({
71
+ ɵprov: getClosureSafeProperty,
72
+ });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ /**
9
+ * Injection flags for DI.
10
+ *
11
+ * @publicApi
12
+ */
13
+ export var InjectFlags;
14
+ (function (InjectFlags) {
15
+ // TODO(alxhub): make this 'const' (and remove `InternalInjectFlags` enum) when ngc no longer
16
+ // writes exports of it into ngfactory files.
17
+ /** Check self and check parent injector if needed */
18
+ InjectFlags[InjectFlags["Default"] = 0] = "Default";
19
+ /** Don't ascend to ancestors of the node requesting injection. */
20
+ InjectFlags[InjectFlags["Self"] = 2] = "Self";
21
+ /** Skip the node that is requesting injection. */
22
+ InjectFlags[InjectFlags["SkipSelf"] = 4] = "SkipSelf";
23
+ /** Inject `defaultValue` instead if token not found. */
24
+ InjectFlags[InjectFlags["Optional"] = 8] = "Optional";
25
+ })(InjectFlags || (InjectFlags = {}));
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ export {};
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { makeParamDecorator } from '../util/decorators';
9
+ import { attachInjectFlag } from './injector_compatibility';
10
+ /**
11
+ * Inject decorator and metadata.
12
+ *
13
+ * @Annotation
14
+ * @publicApi
15
+ */
16
+ export const Inject = attachInjectFlag(
17
+ // Disable tslint because `DecoratorFlags` is a const enum which gets inlined.
18
+ // tslint:disable-next-line: no-toplevel-property-access
19
+ makeParamDecorator('Inject', (token) => ({ token })), -1 /* Inject */);
20
+ /**
21
+ * Optional decorator and metadata.
22
+ *
23
+ * @Annotation
24
+ * @publicApi
25
+ */
26
+ export const Optional =
27
+ // Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
28
+ // tslint:disable-next-line: no-toplevel-property-access
29
+ attachInjectFlag(makeParamDecorator('Optional'), 8 /* Optional */);
30
+ /**
31
+ * Self decorator and metadata.
32
+ *
33
+ * @Annotation
34
+ * @publicApi
35
+ */
36
+ export const Self =
37
+ // Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
38
+ // tslint:disable-next-line: no-toplevel-property-access
39
+ attachInjectFlag(makeParamDecorator('Self'), 2 /* Self */);
40
+ /**
41
+ * `SkipSelf` decorator and metadata.
42
+ *
43
+ * @Annotation
44
+ * @publicApi
45
+ */
46
+ export const SkipSelf =
47
+ // Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
48
+ // tslint:disable-next-line: no-toplevel-property-access
49
+ attachInjectFlag(makeParamDecorator('SkipSelf'), 4 /* SkipSelf */);
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { stringify } from "../util/stringify";
9
+ import { THROW_IF_NOT_FOUND } from "./injector_compatibility";
10
+ export class NullInjector {
11
+ get(token, notFoundValue = THROW_IF_NOT_FOUND) {
12
+ if (notFoundValue === THROW_IF_NOT_FOUND) {
13
+ const error = new Error(`NullInjectorError: No provider for ${stringify(token)}!`);
14
+ error.name = "NullInjectorError";
15
+ throw error;
16
+ }
17
+ return notFoundValue;
18
+ }
19
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ export {};
@@ -0,0 +1,382 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { getFactoryDef } from '../render3/definition_factory';
9
+ import { deepForEach, newArray } from '../util/array_utils';
10
+ import { stringify } from '../util/stringify';
11
+ import { resolveForwardRef } from './forward_ref';
12
+ import { InjectionToken } from './injection_token';
13
+ import { catchInjectorError, injectArgs, NG_TEMP_TOKEN_PATH, setCurrentInjector, THROW_IF_NOT_FOUND, USE_VALUE, ɵɵinject, } from './injector_compatibility';
14
+ import { INJECTOR } from './injector_token';
15
+ import { getInjectableDef, } from './interface/defs';
16
+ import { InjectFlags } from './interface/injector';
17
+ import { NullInjector } from './null_injector';
18
+ import { INJECTOR_SCOPE } from './scope';
19
+ /**
20
+ * Marker which indicates that a value has not yet been created from the factory function.
21
+ */
22
+ const NOT_YET = {};
23
+ /**
24
+ * Marker which indicates that the factory function for a token is in the process of being called.
25
+ *
26
+ * If the injector is asked to inject a token with its value set to CIRCULAR, that indicates
27
+ * injection of a dependency has recursively attempted to inject the original token, and there is
28
+ * a circular dependency among the providers.
29
+ */
30
+ const CIRCULAR = {};
31
+ /**
32
+ * A lazily initialized NullInjector.
33
+ */
34
+ let NULL_INJECTOR = undefined;
35
+ function getNullInjector() {
36
+ if (NULL_INJECTOR === undefined) {
37
+ NULL_INJECTOR = new NullInjector();
38
+ }
39
+ return NULL_INJECTOR;
40
+ }
41
+ /**
42
+ * Create a new `Injector` which is configured using a `defType` of `InjectorType<any>`s.
43
+ *
44
+ * @publicApi
45
+ */
46
+ export function createInjector(defType, parent = null, additionalProviders = null, name) {
47
+ const injector = createInjectorWithoutInjectorInstances(defType, parent, additionalProviders, name);
48
+ injector._resolveInjectorDefTypes();
49
+ return injector;
50
+ }
51
+ /**
52
+ * Creates a new injector without eagerly resolving its injector types. Can be used in places
53
+ * where resolving the injector types immediately can lead to an infinite loop. The injector types
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) {
61
+ this.parent = parent;
62
+ /**
63
+ * Map of tokens to records which contain the instances of those tokens.
64
+ * - `null` value implies that we don't have the record. Used by tree-shakable injectors
65
+ * to prevent further searches.
66
+ */
67
+ this.records = new Map();
68
+ /**
69
+ * The transitive set of `InjectorType`s which define this injector.
70
+ */
71
+ this.injectorDefTypes = new Set();
72
+ /**
73
+ * Set of values instantiated by this injector which contain `ngOnDestroy` lifecycle hooks.
74
+ */
75
+ this.onDestroy = new Set();
76
+ this._destroyed = false;
77
+ // Start off by creating Records for every provider declared in every InjectorType
78
+ // included transitively in additional providers then do the same for `def`. This order is
79
+ // important because `def` may include providers that override ones in additionalProviders.
80
+ additionalProviders &&
81
+ deepForEach(additionalProviders, (provider) => this.processProvider(provider, def, additionalProviders));
82
+ // Make sure the INJECTOR token provides this injector.
83
+ this.records.set(INJECTOR, makeRecord(undefined, this));
84
+ // Detect whether this injector has the APP_ROOT_SCOPE token and thus should provide
85
+ // any injectable scoped to APP_ROOT_SCOPE.
86
+ const record = this.records.get(INJECTOR_SCOPE);
87
+ this.scope = record != null ? record.value : null;
88
+ // Source name, used for debugging
89
+ this.source = source || (typeof def === 'object' ? null : stringify(def));
90
+ }
91
+ /**
92
+ * Flag indicating that this injector was previously destroyed.
93
+ */
94
+ get destroyed() {
95
+ return this._destroyed;
96
+ }
97
+ /**
98
+ * Destroy the injector and release references to every instance or provider associated with it.
99
+ *
100
+ * Also calls the `OnDestroy` lifecycle hooks of every instance that was created for which a
101
+ * hook was found.
102
+ */
103
+ destroy() {
104
+ this.assertNotDestroyed();
105
+ // Set destroyed = true first, in case lifecycle hooks re-enter destroy().
106
+ this._destroyed = true;
107
+ try {
108
+ // Call all the lifecycle hooks.
109
+ this.onDestroy.forEach((service) => service.ngOnDestroy());
110
+ }
111
+ finally {
112
+ // Release all references.
113
+ this.records.clear();
114
+ this.onDestroy.clear();
115
+ this.injectorDefTypes.clear();
116
+ }
117
+ }
118
+ get(token, notFoundValue = THROW_IF_NOT_FOUND, flags = InjectFlags.Default) {
119
+ this.assertNotDestroyed();
120
+ // Set the injection context.
121
+ const previousInjector = setCurrentInjector(this);
122
+ try {
123
+ // Check for the SkipSelf flag.
124
+ if (!(flags & InjectFlags.SkipSelf)) {
125
+ // SkipSelf isn't set, check if the record belongs to this injector.
126
+ let record = this.records.get(token);
127
+ if (record === undefined) {
128
+ // No record, but maybe the token is scoped to this injector. Look for an injectable
129
+ // def with a scope matching this injector.
130
+ const def = couldBeInjectableType(token) && getInjectableDef(token);
131
+ if (def && this.injectableDefInScope(def)) {
132
+ // Found an injectable def and it's scoped to this injector. Pretend as if it was here
133
+ // all along.
134
+ record = makeRecord(injectableDefOrInjectorDefFactory(token), NOT_YET);
135
+ }
136
+ else {
137
+ record = null;
138
+ }
139
+ this.records.set(token, record);
140
+ }
141
+ // If a record was found, get the instance for it and return it.
142
+ if (record != null /* NOT null || undefined */) {
143
+ return this.hydrate(token, record);
144
+ }
145
+ }
146
+ // Select the next injector based on the Self flag - if self is set, the next injector is
147
+ // the NullInjector, otherwise it's the parent.
148
+ const nextInjector = !(flags & InjectFlags.Self)
149
+ ? this.parent
150
+ : getNullInjector();
151
+ // Set the notFoundValue based on the Optional flag - if optional is set and notFoundValue
152
+ // is undefined, the value is null, otherwise it's the notFoundValue.
153
+ notFoundValue =
154
+ flags & InjectFlags.Optional && notFoundValue === THROW_IF_NOT_FOUND
155
+ ? null
156
+ : notFoundValue;
157
+ return nextInjector.get(token, notFoundValue);
158
+ }
159
+ catch (e) {
160
+ if (e.name === 'NullInjectorError') {
161
+ const path = (e[NG_TEMP_TOKEN_PATH] =
162
+ e[NG_TEMP_TOKEN_PATH] || []);
163
+ path.unshift(stringify(token));
164
+ if (previousInjector) {
165
+ // We still have a parent injector, keep throwing
166
+ throw e;
167
+ }
168
+ else {
169
+ // Format & throw the final error message when we don't have any previous injector
170
+ return catchInjectorError(e, token, 'R3InjectorError', this.source);
171
+ }
172
+ }
173
+ else {
174
+ throw e;
175
+ }
176
+ }
177
+ finally {
178
+ // Lastly, restore the previous injection context.
179
+ setCurrentInjector(previousInjector);
180
+ }
181
+ }
182
+ /** @internal */
183
+ _resolveInjectorDefTypes() {
184
+ this.injectorDefTypes.forEach((defType) => this.get(defType));
185
+ }
186
+ toString() {
187
+ const tokens = [], records = this.records;
188
+ records.forEach((v, token) => tokens.push(stringify(token)));
189
+ return `R3Injector[${tokens.join(', ')}]`;
190
+ }
191
+ assertNotDestroyed() {
192
+ if (this._destroyed) {
193
+ throw new Error('Injector has already been destroyed.');
194
+ }
195
+ }
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
+ /**
206
+ * Process a `SingleProvider` and add it.
207
+ */
208
+ processProvider(provider, ngModuleType, providers) {
209
+ // Determine the token from the provider. Either it's its own token, or has a {provide: ...}
210
+ // property.
211
+ provider = resolveForwardRef(provider);
212
+ let token = isTypeProvider(provider)
213
+ ? provider
214
+ : resolveForwardRef(provider && provider.provide);
215
+ // Construct a `Record` for the provider.
216
+ const record = providerToRecord(provider, ngModuleType, providers);
217
+ if (!isTypeProvider(provider) && provider.multi === true) {
218
+ // If the provider indicates that it's a multi-provider, process it specially.
219
+ // First check whether it's been defined already.
220
+ let multiRecord = this.records.get(token);
221
+ if (multiRecord) {
222
+ // It has. Throw a nice error if
223
+ // if (ngDevMode && multiRecord.multi === undefined) {
224
+ // throwMixedMultiProviderError();
225
+ // }
226
+ }
227
+ else {
228
+ multiRecord = makeRecord(undefined, NOT_YET, true);
229
+ multiRecord.factory = () => injectArgs(multiRecord.multi);
230
+ this.records.set(token, multiRecord);
231
+ }
232
+ token = provider;
233
+ multiRecord.multi.push(provider);
234
+ }
235
+ else {
236
+ // const existing = this.records.get(token);
237
+ // if (ngDevMode && existing && existing.multi !== undefined) {
238
+ // throwMixedMultiProviderError();
239
+ // }
240
+ }
241
+ this.records.set(token, record);
242
+ }
243
+ hydrate(token, record) {
244
+ // if (ngDevMode && record.value === CIRCULAR) {
245
+ // throwCyclicDependencyError(stringify(token));
246
+ // } else
247
+ if (record.value === NOT_YET) {
248
+ record.value = CIRCULAR;
249
+ record.value = record.factory();
250
+ }
251
+ if (typeof record.value === 'object' &&
252
+ record.value &&
253
+ hasOnDestroy(record.value)) {
254
+ this.onDestroy.add(record.value);
255
+ }
256
+ return record.value;
257
+ }
258
+ injectableDefInScope(def) {
259
+ if (!def.providedIn) {
260
+ return false;
261
+ }
262
+ const providedIn = resolveForwardRef(def.providedIn);
263
+ if (typeof providedIn === 'string') {
264
+ return providedIn === 'any' || providedIn === this.scope;
265
+ }
266
+ else {
267
+ return this.injectorDefTypes.has(providedIn);
268
+ }
269
+ }
270
+ }
271
+ function injectableDefOrInjectorDefFactory(token) {
272
+ // Most tokens will have an injectable def directly on them, which specifies a factory directly.
273
+ const injectableDef = getInjectableDef(token);
274
+ const factory = injectableDef !== null ? injectableDef.factory : getFactoryDef(token);
275
+ if (factory !== null) {
276
+ return factory;
277
+ }
278
+ // InjectionTokens should have an injectable def (ɵprov) and thus should be handled above.
279
+ // If it's missing that, it's an error.
280
+ if (token instanceof InjectionToken) {
281
+ throw new Error(`Token ${stringify(token)} is missing a ɵprov definition.`);
282
+ }
283
+ // Undecorated types can sometimes be created if they have no constructor arguments.
284
+ if (token instanceof Function) {
285
+ return getUndecoratedInjectableFactory(token);
286
+ }
287
+ // There was no way to resolve a factory for this token.
288
+ throw new Error('unreachable');
289
+ }
290
+ function getUndecoratedInjectableFactory(token) {
291
+ // If the token has parameters then it has dependencies that we cannot resolve implicitly.
292
+ const paramLength = token.length;
293
+ if (paramLength > 0) {
294
+ const args = newArray(paramLength, '?');
295
+ throw new Error(`Can't resolve all parameters for ${stringify(token)}: (${args.join(', ')}).`);
296
+ }
297
+ // The constructor function appears to have no parameters.
298
+ // This might be because it inherits from a super-class. In which case, use an injectable
299
+ // def from an ancestor if there is one.
300
+ // Otherwise this really is a simple class with no dependencies, so return a factory that
301
+ // just instantiates the zero-arg constructor.
302
+ return () => new token();
303
+ }
304
+ function providerToRecord(provider, ngModuleType, providers) {
305
+ if (isValueProvider(provider)) {
306
+ return makeRecord(undefined, provider.useValue);
307
+ }
308
+ else {
309
+ const factory = providerToFactory(provider, ngModuleType, providers);
310
+ return makeRecord(factory, NOT_YET);
311
+ }
312
+ }
313
+ /**
314
+ * Converts a `SingleProvider` into a factory function.
315
+ *
316
+ * @param provider provider to convert to factory
317
+ */
318
+ export function providerToFactory(provider, ngModuleType, providers) {
319
+ let factory = undefined;
320
+ if (isTypeProvider(provider)) {
321
+ const unwrappedProvider = resolveForwardRef(provider);
322
+ return (getFactoryDef(unwrappedProvider) ||
323
+ injectableDefOrInjectorDefFactory(unwrappedProvider));
324
+ }
325
+ else {
326
+ if (isValueProvider(provider)) {
327
+ factory = () => resolveForwardRef(provider.useValue);
328
+ }
329
+ else if (isFactoryProvider(provider)) {
330
+ factory = () => provider.useFactory(...injectArgs(provider.deps || []));
331
+ }
332
+ else if (isExistingProvider(provider)) {
333
+ factory = () => ɵɵinject(resolveForwardRef(provider.useExisting));
334
+ }
335
+ else {
336
+ const classRef = resolveForwardRef(provider &&
337
+ (provider.useClass ||
338
+ provider.provide));
339
+ // if (ngDevMode && !classRef) {
340
+ // throwInvalidProviderError(ngModuleType, providers, provider);
341
+ // }
342
+ if (hasDeps(provider)) {
343
+ factory = () => new classRef(...injectArgs(provider.deps));
344
+ }
345
+ else {
346
+ return (getFactoryDef(classRef) || injectableDefOrInjectorDefFactory(classRef));
347
+ }
348
+ }
349
+ }
350
+ return factory;
351
+ }
352
+ function makeRecord(factory, value, multi = false) {
353
+ return {
354
+ factory: factory,
355
+ value: value,
356
+ multi: multi ? [] : undefined,
357
+ };
358
+ }
359
+ function isValueProvider(value) {
360
+ return value !== null && typeof value == 'object' && USE_VALUE in value;
361
+ }
362
+ function isExistingProvider(value) {
363
+ return !!(value && value.useExisting);
364
+ }
365
+ function isFactoryProvider(value) {
366
+ return !!(value && value.useFactory);
367
+ }
368
+ export function isTypeProvider(value) {
369
+ return typeof value === 'function';
370
+ }
371
+ function hasDeps(value) {
372
+ return !!value.deps;
373
+ }
374
+ function hasOnDestroy(value) {
375
+ return (value !== null &&
376
+ typeof value === 'object' &&
377
+ typeof value.ngOnDestroy === 'function');
378
+ }
379
+ function couldBeInjectableType(value) {
380
+ return (typeof value === 'function' ||
381
+ (typeof value === 'object' && value instanceof InjectionToken));
382
+ }