-
Notifications
You must be signed in to change notification settings - Fork 25k
/
injector.ts
110 lines (93 loc) · 3.3 KB
/
injector.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Special flag indicating that a decorator is of type `Inject`. It's used to make `Inject`
* decorator tree-shakable (so we don't have to rely on the `instanceof` checks).
* Note: this flag is not included into the `InjectFlags` since it's an internal-only API.
*/
export const enum DecoratorFlags {
Inject = -1,
}
/**
* Injection flags for DI.
*
* @publicApi
* @deprecated use an options object for [`inject`](api/core/inject) instead.
*/
export enum InjectFlags {
// TODO(alxhub): make this 'const' (and remove `InternalInjectFlags` enum) when ngc no longer
// writes exports of it into ngfactory files.
/** Check self and check parent injector if needed */
Default = 0b0000,
/**
* Specifies that an injector should retrieve a dependency from any injector until reaching the
* host element of the current component. (Only used with Element Injector)
*/
Host = 0b0001,
/** Don't ascend to ancestors of the node requesting injection. */
Self = 0b0010,
/** Skip the node that is requesting injection. */
SkipSelf = 0b0100,
/** Inject `defaultValue` instead if token not found. */
Optional = 0b1000,
}
/**
* This enum is an exact copy of the `InjectFlags` enum above, but the difference is that this is a
* const enum, so actual enum values would be inlined in generated code. The `InjectFlags` enum can
* be turned into a const enum when ViewEngine is removed (see TODO at the `InjectFlags` enum
* above). The benefit of inlining is that we can use these flags at the top level without affecting
* tree-shaking (see "no-toplevel-property-access" tslint rule for more info).
* Keep this enum in sync with `InjectFlags` enum above.
*/
export const enum InternalInjectFlags {
/** Check self and check parent injector if needed */
Default = 0b0000,
/**
* Specifies that an injector should retrieve a dependency from any injector until reaching the
* host element of the current component. (Only used with Element Injector)
*/
Host = 0b0001,
/** Don't ascend to ancestors of the node requesting injection. */
Self = 0b0010,
/** Skip the node that is requesting injection. */
SkipSelf = 0b0100,
/** Inject `defaultValue` instead if token not found. */
Optional = 0b1000,
/**
* This token is being injected into a pipe.
*
* This flag is intentionally not in the public facing `InjectFlags` because it is only added by
* the compiler and is not a developer applicable flag.
*/
ForPipe = 0b10000,
}
/**
* Type of the options argument to [`inject`](api/core/inject).
*
* @publicApi
*/
export interface InjectOptions {
/**
* Use optional injection, and return `null` if the requested token is not found.
*/
optional?: boolean;
/**
* Start injection at the parent of the current injector.
*/
skipSelf?: boolean;
/**
* Only query the current injector for the token, and don't fall back to the parent injector if
* it's not found.
*/
self?: boolean;
/**
* Stop injection at the host component's injector. Only relevant when injecting from an element
* injector, and a no-op for environment injectors.
*/
host?: boolean;
}