static-injector 1.0.9 → 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.
Files changed (78) hide show
  1. package/import/commonjs/index.js +331 -118
  2. package/import/es2015/di/create_injector.js +30 -0
  3. package/import/es2015/di/initializer_token.js +15 -0
  4. package/import/es2015/di/inject_switch.js +27 -6
  5. package/import/es2015/{decorator → di}/injectable.js +0 -0
  6. package/import/es2015/di/injection_token.js +6 -0
  7. package/import/es2015/di/injector.js +6 -8
  8. package/import/es2015/di/injector_compatibility.js +90 -34
  9. package/import/es2015/di/injector_token.js +1 -1
  10. package/import/es2015/di/interface/defs.js +39 -0
  11. package/import/es2015/di/interface/injector.js +1 -0
  12. package/import/es2015/di/internal_tokens.js +9 -0
  13. package/import/es2015/di/metadata.js +4 -4
  14. package/import/es2015/di/provider_collection.js +49 -0
  15. package/import/es2015/di/r3_injector.js +96 -66
  16. package/import/es2015/di/scope.js +2 -2
  17. package/import/es2015/index.js +1 -1
  18. package/import/es2015/render3/errors_di.js +1 -1
  19. package/import/fesm2015/index.js +322 -108
  20. package/import/typings/di/create_injector.d.ts +23 -0
  21. package/import/typings/di/initializer_token.d.ts +15 -0
  22. package/import/typings/di/inject_switch.d.ts +7 -2
  23. package/import/typings/{decorator → di}/injectable.d.ts +1 -1
  24. package/import/typings/di/injection_token.d.ts +4 -0
  25. package/import/typings/di/injector.d.ts +20 -7
  26. package/import/typings/di/injector_compatibility.d.ts +42 -21
  27. package/import/typings/di/interface/defs.d.ts +10 -0
  28. package/import/typings/di/interface/injector.d.ts +26 -0
  29. package/import/typings/di/interface/provider.d.ts +1 -6
  30. package/import/typings/di/internal_tokens.d.ts +10 -0
  31. package/import/typings/di/provider_collection.d.ts +30 -0
  32. package/import/typings/di/r3_injector.d.ts +67 -36
  33. package/import/typings/di/scope.d.ts +3 -2
  34. package/import/typings/index.d.ts +1 -1
  35. package/package.json +4 -2
  36. package/transform/compiler/index.js +5 -1
  37. package/transform/compiler/src/injectable_compiler_2.d.ts +7 -36
  38. package/transform/compiler/src/injectable_compiler_2.js +20 -23
  39. package/transform/compiler/src/output/output_ast.d.ts +29 -108
  40. package/transform/compiler/src/output/output_ast.js +66 -188
  41. package/transform/compiler/src/render3/r3_factory.js +11 -7
  42. package/transform/compiler/src/render3/util.d.ts +64 -0
  43. package/transform/compiler/src/render3/util.js +52 -9
  44. package/transform/compiler/src/render3/view/util.js +5 -1
  45. package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/util.d.ts +5 -4
  46. package/transform/compiler-cli/src/ngtsc/annotations/{src → common/src}/util.js +32 -23
  47. package/transform/compiler-cli/src/ngtsc/annotations/src/factory.js +1 -1
  48. package/transform/compiler-cli/src/ngtsc/annotations/src/injectable.js +33 -21
  49. package/transform/compiler-cli/src/ngtsc/diagnostics/error.js +6 -2
  50. package/transform/compiler-cli/src/ngtsc/diagnostics/index.js +5 -1
  51. package/transform/compiler-cli/src/ngtsc/imports/index.js +5 -1
  52. package/transform/compiler-cli/src/ngtsc/reflection/index.js +5 -1
  53. package/transform/compiler-cli/src/ngtsc/reflection/src/host.d.ts +1 -1
  54. package/transform/compiler-cli/src/ngtsc/reflection/src/host.js +6 -2
  55. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.d.ts +2 -2
  56. package/transform/compiler-cli/src/ngtsc/reflection/src/type_to_value.js +36 -23
  57. package/transform/compiler-cli/src/ngtsc/reflection/src/typescript.js +18 -13
  58. package/transform/compiler-cli/src/ngtsc/reflection/src/util.js +5 -1
  59. package/transform/compiler-cli/src/ngtsc/transform/index.js +5 -1
  60. package/transform/compiler-cli/src/ngtsc/transform/src/utils.d.ts +2 -2
  61. package/transform/compiler-cli/src/ngtsc/transform/src/utils.js +13 -8
  62. package/transform/compiler-cli/src/ngtsc/translator/index.js +5 -1
  63. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.d.ts +3 -3
  64. package/transform/compiler-cli/src/ngtsc/translator/src/import_manager.js +7 -3
  65. package/transform/compiler-cli/src/ngtsc/translator/src/translator.d.ts +0 -4
  66. package/transform/compiler-cli/src/ngtsc/translator/src/translator.js +5 -17
  67. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.d.ts +3 -6
  68. package/transform/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.js +67 -55
  69. package/{import/es2015/decorator/interface/provider.js → transform/compiler-cli/src/ngtsc/ts_compatibility/index.d.ts} +1 -1
  70. package/transform/compiler-cli/src/ngtsc/ts_compatibility/index.js +24 -0
  71. package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.d.ts +151 -0
  72. package/transform/compiler-cli/src/ngtsc/ts_compatibility/src/ts_cross_version_utils.js +210 -0
  73. package/transform/compiler-cli/src/ngtsc/util/src/typescript.js +5 -1
  74. package/transform/index.js +5 -1
  75. package/transform/injectable-transform.js +33 -38
  76. package/import/typings/decorator/interface/provider.d.ts +0 -312
  77. package/transform/compiler/src/render3/partial/util.d.ts +0 -16
  78. package/transform/compiler/src/render3/partial/util.js +0 -44
@@ -0,0 +1,23 @@
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 { Injector } from './injector';
9
+ import { StaticProvider } from './interface/provider';
10
+ import { R3Injector } from './r3_injector';
11
+ import { InjectorScope } from './scope';
12
+ /**
13
+ * Create a new `Injector` which is configured using a `defType` of `InjectorType<any>`s.
14
+ *
15
+ * @publicApi
16
+ */
17
+ export declare function createInjector(defType: any, parent?: Injector | null, additionalProviders?: StaticProvider[] | null, name?: string): Injector;
18
+ /**
19
+ * Creates a new injector without eagerly resolving its injector types. Can be used in places
20
+ * where resolving the injector types immediately can lead to an infinite loop. The injector types
21
+ * should be resolved at a later point by calling `_resolveInjectorDefTypes`.
22
+ */
23
+ export declare function createInjectorWithoutInjectorInstances(defType: any, parent?: Injector | null, additionalProviders?: StaticProvider[] | null, name?: string, scopes?: Set<InjectorScope>): R3Injector;
@@ -0,0 +1,15 @@
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 { InjectionToken } from './injection_token';
9
+ /**
10
+ * A multi-provider token for initialization functions that will run upon construction of an
11
+ * environment injector.
12
+ *
13
+ * @publicApi
14
+ */
15
+ export declare const ENVIRONMENT_INITIALIZER: InjectionToken<() => void>;
@@ -5,8 +5,13 @@
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 { InjectFlags } from "./interface/injector";
9
- import { ProviderToken } from "./provider_token";
8
+ import { InjectFlags } from './interface/injector';
9
+ import { ProviderToken } from './provider_token';
10
+ export declare function getInjectImplementation(): <T>(token: ProviderToken<T>, flags?: InjectFlags) => T;
11
+ /**
12
+ * Sets the current inject implementation.
13
+ */
14
+ export declare function setInjectImplementation(impl: (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null) | undefined): (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null) | undefined;
10
15
  /**
11
16
  * Injects `root` tokens in limp mode.
12
17
  *
@@ -5,9 +5,9 @@
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 { ValueSansProvider, ExistingSansProvider, StaticClassSansProvider, ConstructorSansProvider, FactorySansProvider, ClassSansProvider } from './interface/provider';
8
9
  import { Type } from '../interface/type';
9
10
  import { TypeDecorator } from '../util/decorators';
10
- import { ClassSansProvider, ConstructorSansProvider, ExistingSansProvider, FactorySansProvider, StaticClassSansProvider, ValueSansProvider } from './interface/provider';
11
11
  /**
12
12
  * Injectable providers used in `@Injectable` decorator.
13
13
  *
@@ -53,5 +53,9 @@ export declare class InjectionToken<T> {
53
53
  providedIn?: Type<any> | 'root' | 'platform' | 'any' | null;
54
54
  factory: () => T;
55
55
  });
56
+ /**
57
+ * @internal
58
+ */
59
+ get multi(): InjectionToken<Array<T>>;
56
60
  toString(): string;
57
61
  }
@@ -6,11 +6,9 @@
6
6
  * found in the LICENSE file at https://angular.io/license
7
7
  */
8
8
  import { InjectorMarkers } from './injector_marker';
9
- import { InjectFlags } from './interface/injector';
9
+ import { InjectFlags, InjectOptions } from './interface/injector';
10
10
  import { StaticProvider } from './interface/provider';
11
11
  import { ProviderToken } from './provider_token';
12
- export declare function INJECTOR_IMPL__POST_R3__(providers: StaticProvider[], parent: Injector | undefined, name: string): Injector;
13
- export declare const INJECTOR_IMPL: typeof INJECTOR_IMPL__POST_R3__;
14
12
  /**
15
13
  * Concrete injectors implement this interface. Injectors are configured
16
14
  * with [providers](guide/glossary#provider) that associate
@@ -38,17 +36,32 @@ export declare const INJECTOR_IMPL: typeof INJECTOR_IMPL__POST_R3__;
38
36
  export declare abstract class Injector {
39
37
  static THROW_IF_NOT_FOUND: {};
40
38
  static NULL: Injector;
39
+ /**
40
+ * Internal note on the `options?: InjectOptions|InjectFlags` override of the `get`
41
+ * method: consider dropping the `InjectFlags` part in one of the major versions.
42
+ * It can **not** be done in minor/patch, since it's breaking for custom injectors
43
+ * that only implement the old `InjectorFlags` interface.
44
+ */
45
+ /**
46
+ * Retrieves an instance from the injector based on the provided token.
47
+ * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
48
+ * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
49
+ */
50
+ abstract get<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {
51
+ optional?: false;
52
+ }): T;
41
53
  /**
42
54
  * Retrieves an instance from the injector based on the provided token.
43
55
  * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
44
56
  * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
45
57
  */
46
- abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
58
+ abstract get<T>(token: ProviderToken<T>, notFoundValue: null | undefined, options: InjectOptions): T | null;
47
59
  /**
48
- * @deprecated from v4.0.0 use ProviderToken<T>
49
- * @suppress {duplicate}
60
+ * Retrieves an instance from the injector based on the provided token.
61
+ * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
62
+ * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
50
63
  */
51
- abstract get(token: any, notFoundValue?: any): any;
64
+ abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions | InjectFlags): T;
52
65
  /**
53
66
  * Creates a new injector instance that provides one or more dependencies,
54
67
  * according to a given type or types of `StaticProvider`.
@@ -5,9 +5,9 @@
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 { Injector } from "./injector";
9
- import { DecoratorFlags, InjectFlags, InternalInjectFlags } from "./interface/injector";
10
- import { ProviderToken } from "./provider_token";
8
+ import { Injector } from './injector';
9
+ import { DecoratorFlags, InjectFlags, InjectOptions, InternalInjectFlags } from './interface/injector';
10
+ import { ProviderToken } from './provider_token';
11
11
  export declare const THROW_IF_NOT_FOUND: {};
12
12
  export declare const NG_TEMP_TOKEN_PATH = "ngTempTokenPath";
13
13
  export declare const SOURCE = "__source";
@@ -31,30 +31,51 @@ export declare function injectInjectorOnly<T>(token: ProviderToken<T>, flags?: I
31
31
  export declare function ɵɵinject<T>(token: ProviderToken<T>): T;
32
32
  export declare function ɵɵinject<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
33
33
  /**
34
- * Injects a token from the currently active injector.
34
+ * @param token A token that represents a dependency that should be injected.
35
+ * @returns the injected value if operation is successful, `null` otherwise.
36
+ * @throws if called outside of a supported context.
35
37
  *
36
- * Must be used in the context of a factory function such as one defined for an
37
- * `InjectionToken`. Throws an error if not called from such a context.
38
- *
39
- * Within such a factory function, using this function to request injection of a dependency
40
- * is faster and more type-safe than providing an additional array of dependencies
41
- * (as has been common with `useFactory` providers).
42
- *
43
- * @param token The injection token for the dependency to be injected.
44
- * @param flags Optional flags that control how injection is executed.
45
- * The flags correspond to injection strategies that can be specified with
46
- * parameter decorators `@Host`, `@Self`, `@SkipSef`, and `@Optional`.
47
- * @returns the injected value if injection is successful, `null` otherwise.
48
- *
49
- * @usageNotes
38
+ * @publicApi
39
+ */
40
+ export declare function inject<T>(token: ProviderToken<T>): T;
41
+ /**
42
+ * @param token A token that represents a dependency that should be injected.
43
+ * @param flags Control how injection is executed. The flags correspond to injection strategies that
44
+ * can be specified with parameter decorators `@Host`, `@Self`, `@SkipSelf`, and `@Optional`.
45
+ * @returns the injected value if operation is successful, `null` otherwise.
46
+ * @throws if called outside of a supported context.
50
47
  *
51
- * ### Example
48
+ * @publicApi
49
+ * @deprecated prefer an options object instead of `InjectFlags`
50
+ */
51
+ export declare function inject<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
52
+ /**
53
+ * @param token A token that represents a dependency that should be injected.
54
+ * @param options Control how injection is executed. Options correspond to injection strategies
55
+ * that can be specified with parameter decorators `@Host`, `@Self`, `@SkipSelf`, and
56
+ * `@Optional`.
57
+ * @returns the injected value if operation is successful.
58
+ * @throws if called outside of a supported context, or if the token is not found.
52
59
  *
53
- * {@example core/di/ts/injector_spec.ts region='ShakableInjectionToken'}
60
+ * @publicApi
61
+ */
62
+ export declare function inject<T>(token: ProviderToken<T>, options: InjectOptions & {
63
+ optional?: false;
64
+ }): T;
65
+ /**
66
+ * @param token A token that represents a dependency that should be injected.
67
+ * @param options Control how injection is executed. Options correspond to injection strategies
68
+ * that can be specified with parameter decorators `@Host`, `@Self`, `@SkipSelf`, and
69
+ * `@Optional`.
70
+ * @returns the injected value if operation is successful, `null` if the token is not
71
+ * found and optional injection has been requested.
72
+ * @throws if called outside of a supported context, or if the token is not found and optional
73
+ * injection was not requested.
54
74
  *
55
75
  * @publicApi
56
76
  */
57
- export declare const inject: typeof ɵɵinject;
77
+ export declare function inject<T>(token: ProviderToken<T>, options: InjectOptions): T | null;
78
+ export declare function convertToBitFlags(flags: InjectOptions | InjectFlags | undefined): InjectFlags | undefined;
58
79
  export declare function injectArgs(types: (ProviderToken<any> | any[])[]): any[];
59
80
  /**
60
81
  * Attaches a given InjectFlag to a given decorator using monkey-patching.
@@ -153,3 +153,13 @@ export declare function ɵɵdefineInjector(options: {
153
153
  */
154
154
  export declare function getInjectableDef<T>(type: any): ɵɵInjectableDeclaration<T> | null;
155
155
  export declare const NG_PROV_DEF: string;
156
+ /**
157
+ * Read the injectable def (`ɵprov`) for `type` or read the `ɵprov` from one of its ancestors.
158
+ *
159
+ * @param type A type which may have `ɵprov`, via inheritance.
160
+ *
161
+ * @deprecated Will be removed in a future version of Angular, where an error will occur in the
162
+ * scenario if we find the `ɵprov` on an ancestor only.
163
+ */
164
+ export declare function getInheritedInjectableDef<T>(type: any): ɵɵInjectableDeclaration<T> | null;
165
+ export declare const NG_INJECTABLE_DEF: string;
@@ -17,6 +17,7 @@ export declare const enum DecoratorFlags {
17
17
  * Injection flags for DI.
18
18
  *
19
19
  * @publicApi
20
+ * @deprecated use an options object for `inject` instead.
20
21
  */
21
22
  export declare enum InjectFlags {
22
23
  /** Check self and check parent injector if needed */
@@ -46,3 +47,28 @@ export declare const enum InternalInjectFlags {
46
47
  /** Inject `defaultValue` instead if token not found. */
47
48
  Optional = 8
48
49
  }
50
+ /**
51
+ * Type of the options argument to `inject`.
52
+ *
53
+ * @publicApi
54
+ */
55
+ export interface InjectOptions {
56
+ /**
57
+ * Use optional injection, and return `null` if the requested token is not found.
58
+ */
59
+ optional?: boolean;
60
+ /**
61
+ * Start injection at the parent of the current injector.
62
+ */
63
+ skipSelf?: boolean;
64
+ /**
65
+ * Only query the current injector for the token, and don't fall back to the parent injector if
66
+ * it's not found.
67
+ */
68
+ self?: boolean;
69
+ /**
70
+ * Stop injection at the host component's injector. Only relevant when injecting from an element
71
+ * injector, and a no-op for environment injectors.
72
+ */
73
+ host?: boolean;
74
+ }
@@ -5,7 +5,7 @@
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 { Type } from "../../interface/type";
8
+ import { Type } from '../../interface/type';
9
9
  /**
10
10
  * Configures the `Injector` to return a value for a token.
11
11
  * Base for `ValueProvider` decorator.
@@ -305,8 +305,3 @@ export interface ClassProvider extends ClassSansProvider {
305
305
  * @publicApi
306
306
  */
307
307
  export declare type Provider = TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider | any[];
308
- /**
309
- * Describes a function that is used to process provider lists (such as provider
310
- * overrides).
311
- */
312
- export declare type ProcessProvidersFunction = (providers: Provider[]) => Provider[];
@@ -0,0 +1,10 @@
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 { Type } from '../interface/type';
9
+ import { InjectionToken } from './injection_token';
10
+ export declare const INJECTOR_DEF_TYPES: InjectionToken<Type<unknown>>;
@@ -0,0 +1,30 @@
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 { Type } from '../interface/type';
9
+ import { InjectorTypeWithProviders } from './interface/defs';
10
+ import { ClassProvider, ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, TypeProvider, ValueProvider } from './interface/provider';
11
+ /**
12
+ * Internal type for a single provider in a deep provider array.
13
+ */
14
+ export declare type SingleProvider = TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider | StaticClassProvider;
15
+ /**
16
+ * The logic visits an `InjectorType`, an `InjectorTypeWithProviders`, or a standalone
17
+ * `ComponentType`, and all of its transitive providers and collects providers.
18
+ *
19
+ * If an `InjectorTypeWithProviders` that declares providers besides the type is specified,
20
+ * the function will return "true" to indicate that the providers of the type definition need
21
+ * to be processed. This allows us to process providers of injector types after all imports of
22
+ * an injector definition are processed. (following View Engine semantics: see FW-1349)
23
+ */
24
+ export declare function walkProviderTree(container: Type<unknown> | InjectorTypeWithProviders<unknown>, providersOut: SingleProvider[], parents: Type<unknown>[], dedup: Set<Type<unknown>>): container is InjectorTypeWithProviders<unknown>;
25
+ export declare const USE_VALUE: string;
26
+ export declare function isValueProvider(value: SingleProvider): value is ValueProvider;
27
+ export declare function isExistingProvider(value: SingleProvider): value is ExistingProvider;
28
+ export declare function isFactoryProvider(value: SingleProvider): value is FactoryProvider;
29
+ export declare function isTypeProvider(value: SingleProvider): value is TypeProvider;
30
+ export declare function isClassProvider(value: SingleProvider): value is ClassProvider;
@@ -7,53 +7,91 @@
7
7
  */
8
8
  import { Injector } from './injector';
9
9
  import { InjectorType } from './interface/defs';
10
- import { InjectFlags } from './interface/injector';
11
- import { ClassProvider, ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, StaticProvider, TypeProvider, ValueProvider } from './interface/provider';
10
+ import { InjectFlags, InjectOptions } from './interface/injector';
11
+ import { ClassProvider, ConstructorProvider, ExistingProvider, FactoryProvider, Provider, StaticClassProvider, TypeProvider, ValueProvider } from './interface/provider';
12
12
  import { ProviderToken } from './provider_token';
13
+ import { InjectorScope } from './scope';
13
14
  /**
14
15
  * Internal type for a single provider in a deep provider array.
15
16
  */
16
17
  declare type SingleProvider = TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider | StaticClassProvider;
18
+ export declare function getNullInjector(): Injector;
17
19
  /**
18
- * Create a new `Injector` which is configured using a `defType` of `InjectorType<any>`s.
19
- *
20
- * @publicApi
21
- */
22
- export declare function createInjector(defType: any, parent?: Injector | null, additionalProviders?: StaticProvider[] | null, name?: string): Injector;
23
- /**
24
- * Creates a new injector without eagerly resolving its injector types. Can be used in places
25
- * where resolving the injector types immediately can lead to an infinite loop. The injector types
26
- * should be resolved at a later point by calling `_resolveInjectorDefTypes`.
20
+ * An `Injector` that's part of the environment injector hierarchy, which exists outside of the
21
+ * component tree.
27
22
  */
28
- export declare function createInjectorWithoutInjectorInstances(defType: any, parent?: Injector | null, additionalProviders?: StaticProvider[] | null, name?: string): R3Injector;
29
- export declare class R3Injector {
23
+ export declare abstract class EnvironmentInjector implements Injector {
24
+ /**
25
+ * Retrieves an instance from the injector based on the provided token.
26
+ * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
27
+ * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
28
+ */
29
+ abstract get<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & {
30
+ optional?: false;
31
+ }): T;
32
+ /**
33
+ * Retrieves an instance from the injector based on the provided token.
34
+ * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
35
+ * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
36
+ */
37
+ abstract get<T>(token: ProviderToken<T>, notFoundValue: null | undefined, options: InjectOptions): T | null;
38
+ /**
39
+ * Retrieves an instance from the injector based on the provided token.
40
+ * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
41
+ * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
42
+ */
43
+ abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions): T;
44
+ /**
45
+ * Retrieves an instance from the injector based on the provided token.
46
+ * @returns The instance from the injector if defined, otherwise the `notFoundValue`.
47
+ * @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
48
+ * @deprecated use object-based flags (`InjectOptions`) instead.
49
+ */
50
+ abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
51
+ /**
52
+ * @deprecated from v4.0.0 use ProviderToken<T>
53
+ * @suppress {duplicate}
54
+ */
55
+ abstract get(token: any, notFoundValue?: any): any;
56
+ /**
57
+ * Runs the given function in the context of this `EnvironmentInjector`.
58
+ *
59
+ * Within the function's stack frame, `inject` can be used to inject dependencies from this
60
+ * injector. Note that `inject` is only usable synchronously, and cannot be used in any
61
+ * asynchronous callbacks or after any `await` points.
62
+ *
63
+ * @param fn the closure to be run in the context of this injector
64
+ * @returns the return value of the function, if any
65
+ */
66
+ abstract runInContext<ReturnT>(fn: () => ReturnT): ReturnT;
67
+ abstract destroy(): void;
68
+ /**
69
+ * @internal
70
+ */
71
+ abstract onDestroy(callback: () => void): void;
72
+ }
73
+ export declare class R3Injector extends EnvironmentInjector {
30
74
  readonly parent: Injector;
75
+ readonly source: string | null;
76
+ readonly scopes: Set<InjectorScope>;
31
77
  /**
32
78
  * Map of tokens to records which contain the instances of those tokens.
33
79
  * - `null` value implies that we don't have the record. Used by tree-shakable injectors
34
80
  * to prevent further searches.
35
81
  */
36
82
  private records;
37
- /**
38
- * The transitive set of `InjectorType`s which define this injector.
39
- */
40
- private injectorDefTypes;
41
83
  /**
42
84
  * Set of values instantiated by this injector which contain `ngOnDestroy` lifecycle hooks.
43
85
  */
44
- private onDestroy;
45
- /**
46
- * Flag indicating this injector provides the APP_ROOT_SCOPE token, and thus counts as the
47
- * root scope.
48
- */
49
- private readonly scope;
50
- readonly source: string | null;
86
+ private _ngOnDestroyHooks;
87
+ private _onDestroyHooks;
51
88
  /**
52
89
  * Flag indicating that this injector was previously destroyed.
53
90
  */
54
91
  get destroyed(): boolean;
55
92
  private _destroyed;
56
- constructor(def: InjectorType<any>, additionalProviders: StaticProvider[] | null, parent: Injector, source?: string | null);
93
+ private injectorDefTypes;
94
+ constructor(providers: Array<Provider>, parent: Injector, source: string | null, scopes: Set<InjectorScope>);
57
95
  /**
58
96
  * Destroy the injector and release references to every instance or provider associated with it.
59
97
  *
@@ -61,20 +99,13 @@ export declare class R3Injector {
61
99
  * hook was found.
62
100
  */
63
101
  destroy(): void;
64
- get<T>(token: ProviderToken<T>, notFoundValue?: any, flags?: InjectFlags): T;
102
+ onDestroy(callback: () => void): void;
103
+ runInContext<ReturnT>(fn: () => ReturnT): ReturnT;
104
+ get<T>(token: ProviderToken<T>, notFoundValue?: any, flags?: InjectFlags | InjectOptions): T;
65
105
  /** @internal */
66
- _resolveInjectorDefTypes(): void;
106
+ resolveInjectorInitializers(): void;
67
107
  toString(): string;
68
108
  private assertNotDestroyed;
69
- /**
70
- * Add an `InjectorType` or `InjectorTypeWithProviders` and all of its transitive providers
71
- * to this injector.
72
- *
73
- * If an `InjectorTypeWithProviders` that declares providers besides the type is specified,
74
- * the function will return "true" to indicate that the providers of the type definition need
75
- * to be processed. This allows us to process providers of injector types after all imports of
76
- * an injector definition are processed. (following View Engine semantics: see FW-1349)
77
- */
78
109
  /**
79
110
  * Process a `SingleProvider` and add it.
80
111
  */
@@ -5,10 +5,11 @@
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 "./injection_token";
8
+ import { InjectionToken } from './injection_token';
9
+ export declare type InjectorScope = 'root' | 'platform' | 'environment';
9
10
  /**
10
11
  * An internal token whose presence in an injector indicates that the injector should treat itself
11
12
  * as a root scoped injector when processing requests for unknown tokens which may indicate
12
13
  * they are provided in the root scope.
13
14
  */
14
- export declare const INJECTOR_SCOPE: InjectionToken<"root" | "platform">;
15
+ export declare const INJECTOR_SCOPE: InjectionToken<InjectorScope>;
@@ -1,4 +1,4 @@
1
- export * from './decorator/injectable';
1
+ export * from './di/injectable';
2
2
  export * from './di/metadata';
3
3
  export * from './di/r3_injector';
4
4
  export * from './di/interface/defs';
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "static-injector",
3
- "version": "1.0.9",
3
+ "version": "2.0.0",
4
4
  "description": "Angular的静态注入器独立使用版本",
5
5
  "keywords": [
6
6
  "angular",
7
7
  "injector",
8
8
  "typescript",
9
- "injectable"
9
+ "typescript 4.8",
10
+ "injectable",
11
+ "static-inject"
10
12
  ],
11
13
  "repository": {
12
14
  "type": "git",
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -5,49 +5,20 @@
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 * as o from "./output/output_ast";
9
- import { R3DependencyMetadata } from "./render3/r3_factory";
10
- import { R3CompiledExpression, R3Reference } from "./render3/util";
8
+ import * as o from './output/output_ast';
9
+ import { R3DependencyMetadata } from './render3/r3_factory';
10
+ import { MaybeForwardRefExpression, R3CompiledExpression, R3Reference } from './render3/util';
11
11
  export interface R3InjectableMetadata {
12
12
  name: string;
13
13
  type: R3Reference;
14
14
  internalType: o.Expression;
15
15
  typeArgumentCount: number;
16
- providedIn: R3ProviderExpression;
17
- useClass?: R3ProviderExpression;
16
+ providedIn: MaybeForwardRefExpression;
17
+ useClass?: MaybeForwardRefExpression;
18
18
  useFactory?: o.Expression;
19
- useExisting?: R3ProviderExpression;
20
- useValue?: R3ProviderExpression;
19
+ useExisting?: MaybeForwardRefExpression;
20
+ useValue?: MaybeForwardRefExpression;
21
21
  deps?: R3DependencyMetadata[];
22
22
  }
23
- /**
24
- * An expression used when instantiating an injectable.
25
- *
26
- * This is the type of the `useClass`, `useExisting` and `useValue` properties of
27
- * `R3InjectableMetadata` since those can refer to types that may eagerly reference types that have
28
- * not yet been defined.
29
- */
30
- export interface R3ProviderExpression<T extends o.Expression = o.Expression> {
31
- /**
32
- * The expression that is used to instantiate the Injectable.
33
- */
34
- expression: T;
35
- /**
36
- * If true, then the `expression` contains a reference to something that has not yet been
37
- * defined.
38
- *
39
- * This means that the expression must not be eagerly evaluated. Instead it must be wrapped in a
40
- * function closure that will be evaluated lazily to allow the definition of the expression to be
41
- * evaluated first.
42
- *
43
- * In some cases the expression will naturally be placed inside such a function closure, such as
44
- * in a fully compiled factory function. In those case nothing more needs to be done.
45
- *
46
- * But in other cases, such as partial-compilation the expression will be located in top level
47
- * code so will need to be wrapped in a function that is passed to a `forwardRef()` call.
48
- */
49
- isForwardRef: boolean;
50
- }
51
- export declare function createR3ProviderExpression<T extends o.Expression>(expression: T, isForwardRef: boolean): R3ProviderExpression<T>;
52
23
  export declare function compileInjectable(meta: R3InjectableMetadata, resolveForwardRefs: boolean): R3CompiledExpression;
53
24
  export declare function createInjectableType(meta: R3InjectableMetadata): o.ExpressionType;