-
Notifications
You must be signed in to change notification settings - Fork 26.2k
/
Copy pathmetadata.ts
250 lines (232 loc) Β· 6.4 KB
/
metadata.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
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
/**
* @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.dev/license
*/
import {makeParamDecorator} from '../util/decorators';
import {attachInjectFlag} from './injector_compatibility';
import {DecoratorFlags, InternalInjectFlags} from './interface/injector';
/**
* Type of the Inject decorator / constructor function.
*
* @publicApi
*/
export interface InjectDecorator {
/**
* Warning: String tokens are not recommended.
*
* Use an InjectionToken or a class as a token instead.
*/
(token: string): any;
new (token: string): Inject;
/**
* Parameter decorator on a dependency parameter of a class constructor
* that specifies a custom provider of the dependency.
*
* @usageNotes
* The following example shows a class constructor that specifies a
* custom provider of a dependency using the parameter decorator.
*
* When `@Inject()` is not present, the injector uses the type annotation of the
* parameter as the provider.
*
* {@example core/di/ts/metadata_spec.ts region='InjectWithoutDecorator'}
*
* @see [Dependency Injection Guide](guide/di/dependency-injection
*
*/
(token: any): any;
new (token: any): Inject;
}
/**
* Type of the Inject metadata.
*
* @publicApi
*/
export interface Inject {
/**
* A DI token that maps to the dependency to be injected.
*/
token: any;
}
/**
* Inject decorator and metadata.
*
* @Annotation
* @publicApi
*/
export const Inject: InjectDecorator = attachInjectFlag(
// Disable tslint because `DecoratorFlags` is a const enum which gets inlined.
makeParamDecorator('Inject', (token: any) => ({token})),
// tslint:disable-next-line: no-toplevel-property-access
DecoratorFlags.Inject,
);
/**
* Type of the Optional decorator / constructor function.
*
* @publicApi
*/
export interface OptionalDecorator {
/**
* Parameter decorator to be used on constructor parameters,
* which marks the parameter as being an optional dependency.
* The DI framework provides `null` if the dependency is not found.
*
* Can be used together with other parameter decorators
* that modify how dependency injection operates.
*
* @usageNotes
*
* The following code allows the possibility of a `null` result:
*
* {@example core/di/ts/metadata_spec.ts region='Optional'}
*
* @see [Dependency Injection Guide](guide/di/dependency-injection.
*/
(): any;
new (): Optional;
}
/**
* Type of the Optional metadata.
*
* @publicApi
*/
export interface Optional {}
/**
* Optional decorator and metadata.
*
* @Annotation
* @publicApi
*/
export const Optional: OptionalDecorator =
// Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
// tslint:disable-next-line: no-toplevel-property-access
attachInjectFlag(makeParamDecorator('Optional'), InternalInjectFlags.Optional);
/**
* Type of the Self decorator / constructor function.
*
* @publicApi
*/
export interface SelfDecorator {
/**
* Parameter decorator to be used on constructor parameters,
* which tells the DI framework to start dependency resolution from the local injector.
*
* Resolution works upward through the injector hierarchy, so the children
* of this class must configure their own providers or be prepared for a `null` result.
*
* @usageNotes
*
* In the following example, the dependency can be resolved
* by the local injector when instantiating the class itself, but not
* when instantiating a child.
*
* {@example core/di/ts/metadata_spec.ts region='Self'}
*
* @see {@link SkipSelf}
* @see {@link Optional}
*
*/
(): any;
new (): Self;
}
/**
* Type of the Self metadata.
*
* @publicApi
*/
export interface Self {}
/**
* Self decorator and metadata.
*
* @Annotation
* @publicApi
*/
export const Self: SelfDecorator =
// Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
// tslint:disable-next-line: no-toplevel-property-access
attachInjectFlag(makeParamDecorator('Self'), InternalInjectFlags.Self);
/**
* Type of the `SkipSelf` decorator / constructor function.
*
* @publicApi
*/
export interface SkipSelfDecorator {
/**
* Parameter decorator to be used on constructor parameters,
* which tells the DI framework to start dependency resolution from the parent injector.
* Resolution works upward through the injector hierarchy, so the local injector
* is not checked for a provider.
*
* @usageNotes
*
* In the following example, the dependency can be resolved when
* instantiating a child, but not when instantiating the class itself.
*
* {@example core/di/ts/metadata_spec.ts region='SkipSelf'}
*
* @see [Dependency Injection guide](guide/di/di-in-action#skip).
* @see {@link Self}
* @see {@link Optional}
*
*/
(): any;
new (): SkipSelf;
}
/**
* Type of the `SkipSelf` metadata.
*
* @publicApi
*/
export interface SkipSelf {}
/**
* `SkipSelf` decorator and metadata.
*
* @Annotation
* @publicApi
*/
export const SkipSelf: SkipSelfDecorator =
// Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
// tslint:disable-next-line: no-toplevel-property-access
attachInjectFlag(makeParamDecorator('SkipSelf'), InternalInjectFlags.SkipSelf);
/**
* Type of the `Host` decorator / constructor function.
*
* @publicApi
*/
export interface HostDecorator {
/**
* Parameter decorator on a view-provider parameter of a class constructor
* that tells the DI framework to resolve the view by checking injectors of child
* elements, and stop when reaching the host element of the current component.
*
* @usageNotes
*
* The following shows use with the `@Optional` decorator, and allows for a `null` result.
*
* {@example core/di/ts/metadata_spec.ts region='Host'}
*
* For an extended example, see ["Dependency Injection
* Guide"](guide/di/di-in-action#optional).
*/
(): any;
new (): Host;
}
/**
* Type of the Host metadata.
*
* @publicApi
*/
export interface Host {}
/**
* Host decorator and metadata.
*
* @Annotation
* @publicApi
*/
export const Host: HostDecorator =
// Disable tslint because `InternalInjectFlags` is a const enum which gets inlined.
// tslint:disable-next-line: no-toplevel-property-access
attachInjectFlag(makeParamDecorator('Host'), InternalInjectFlags.Host);