1
1
import { protocol } from 'aurelia-metadata' ;
2
2
import { Container } from './container' ;
3
+ import { autoinject } from './injection' ;
3
4
4
5
/**
5
6
* Decorator: Indicates that the decorated class/object is a custom resolver.
@@ -25,6 +26,53 @@ export interface Resolver {
25
26
get ( container : Container , key : any ) : any ;
26
27
}
27
28
29
+ /**
30
+ * Used to resolve instances, singletons, transients, aliases
31
+ */
32
+ @resolver ( )
33
+ export class StrategyResolver {
34
+ strategy : StrategyResolver | number ;
35
+ state : any ;
36
+
37
+ /**
38
+ * Creates an instance of the StrategyResolver class.
39
+ * @param strategy The type of resolution strategy.
40
+ * @param state The state associated with the resolution strategy.
41
+ */
42
+ constructor ( strategy , state ) {
43
+ this . strategy = strategy ;
44
+ this . state = state ;
45
+ }
46
+
47
+ /**
48
+ * Called by the container to allow custom resolution of dependencies for a function/class.
49
+ * @param container The container to resolve from.
50
+ * @param key The key that the resolver was registered as.
51
+ * @return Returns the resolved object.
52
+ */
53
+ get ( container : Container , key : any ) : any {
54
+ switch ( this . strategy ) {
55
+ case 0 : //instance
56
+ return this . state ;
57
+ case 1 : //singleton
58
+ let singleton = container . invoke ( this . state ) ;
59
+ this . state = singleton ;
60
+ this . strategy = 0 ;
61
+ return singleton ;
62
+ case 2 : //transient
63
+ return container . invoke ( this . state ) ;
64
+ case 3 : //function
65
+ return this . state ( container , key , this ) ;
66
+ case 4 : //array
67
+ return this . state [ 0 ] . get ( container , key ) ;
68
+ case 5 : //alias
69
+ return container . get ( this . state ) ;
70
+ default :
71
+ throw new Error ( 'Invalid strategy: ' + this . strategy ) ;
72
+ }
73
+ }
74
+ }
75
+
28
76
/**
29
77
* Used to allow functions/classes to specify lazy resolution logic.
30
78
*/
@@ -140,7 +188,6 @@ export class Optional {
140
188
}
141
189
}
142
190
143
-
144
191
/**
145
192
* Used to inject the dependency from the parent container instead of the current one.
146
193
*/
@@ -178,50 +225,6 @@ export class Parent {
178
225
}
179
226
}
180
227
181
- @resolver ( )
182
- export class StrategyResolver {
183
- strategy : StrategyResolver | number ;
184
- state : any ;
185
-
186
- /**
187
- * Creates an instance of the StrategyResolver class.
188
- * @param strategy The type of resolution strategy.
189
- * @param state The state associated with the resolution strategy.
190
- */
191
- constructor ( strategy , state ) {
192
- this . strategy = strategy ;
193
- this . state = state ;
194
- }
195
-
196
- /**
197
- * Called by the container to allow custom resolution of dependencies for a function/class.
198
- * @param container The container to resolve from.
199
- * @param key The key that the resolver was registered as.
200
- * @return Returns the resolved object.
201
- */
202
- get ( container : Container , key : any ) : any {
203
- switch ( this . strategy ) {
204
- case 0 : //instance
205
- return this . state ;
206
- case 1 : //singleton
207
- let singleton = container . invoke ( this . state ) ;
208
- this . state = singleton ;
209
- this . strategy = 0 ;
210
- return singleton ;
211
- case 2 : //transient
212
- return container . invoke ( this . state ) ;
213
- case 3 : //function
214
- return this . state ( container , key , this ) ;
215
- case 4 : //array
216
- return this . state [ 0 ] . get ( container , key ) ;
217
- case 5 : //alias
218
- return container . get ( this . state ) ;
219
- default :
220
- throw new Error ( 'Invalid strategy: ' + this . strategy ) ;
221
- }
222
- }
223
- }
224
-
225
228
/**
226
229
* Used to allow injecting dependencies but also passing data to the constructor.
227
230
*/
@@ -270,8 +273,12 @@ export class Factory {
270
273
*/
271
274
@resolver ( )
272
275
export class NewInstance {
273
- key ;
274
- asKey ;
276
+ /** @internal */
277
+ key : any ;
278
+ /** @internal */
279
+ asKey : any ;
280
+ /** @internal */
281
+ dynamicDependencies : any [ ] ;
275
282
276
283
/**
277
284
* Creates an instance of the NewInstance class.
@@ -327,26 +334,24 @@ export class NewInstance {
327
334
}
328
335
}
329
336
330
- export function getDecoratorDependencies ( target , name ) {
331
- let dependencies = target . inject ;
332
- if ( typeof dependencies === 'function' ) {
333
- throw new Error ( 'Decorator ' + name + ' cannot be used with "inject()". Please use an array instead.' ) ;
334
- }
335
- if ( ! dependencies ) {
336
- dependencies = metadata . getOwn ( metadata . paramTypes , target ) . slice ( ) ;
337
- target . inject = dependencies ;
338
- }
337
+ /**
338
+ * Used by parameter decorators to call autoinject for the target and retrieve the target's inject property.
339
+ * @param target The target class.
340
+ * @return Returns the target's own inject property.
341
+ */
342
+ export function getDecoratorDependencies ( target ) {
343
+ autoinject ( target ) ;
339
344
340
- return dependencies ;
345
+ return target . inject ;
341
346
}
342
347
343
348
/**
344
349
* Decorator: Specifies the dependency should be lazy loaded
345
350
*/
346
351
export function lazy ( keyValue : any ) {
347
352
return function ( target , key , index ) {
348
- let params = getDecoratorDependencies ( target , 'lazy' ) ;
349
- params [ index ] = Lazy . of ( keyValue ) ;
353
+ let inject = getDecoratorDependencies ( target ) ;
354
+ inject [ index ] = Lazy . of ( keyValue ) ;
350
355
} ;
351
356
}
352
357
@@ -355,8 +360,8 @@ export function lazy(keyValue: any) {
355
360
*/
356
361
export function all ( keyValue : any ) {
357
362
return function ( target , key , index ) {
358
- let params = getDecoratorDependencies ( target , 'all' ) ;
359
- params [ index ] = All . of ( keyValue ) ;
363
+ let inject = getDecoratorDependencies ( target ) ;
364
+ inject [ index ] = All . of ( keyValue ) ;
360
365
} ;
361
366
}
362
367
@@ -366,8 +371,8 @@ export function all(keyValue: any) {
366
371
export function optional ( checkParentOrTarget : boolean = true ) {
367
372
let deco = function ( checkParent : boolean ) {
368
373
return function ( target , key , index ) {
369
- let params = getDecoratorDependencies ( target , 'optional' ) ;
370
- params [ index ] = Optional . of ( params [ index ] , checkParent ) ;
374
+ let inject = getDecoratorDependencies ( target ) ;
375
+ inject [ index ] = Optional . of ( inject [ index ] , checkParent ) ;
371
376
} ;
372
377
} ;
373
378
if ( typeof checkParentOrTarget === 'boolean' ) {
@@ -380,18 +385,18 @@ export function optional(checkParentOrTarget: boolean = true) {
380
385
* Decorator: Specifies the dependency to look at the parent container for resolution
381
386
*/
382
387
export function parent ( target , key , index ) {
383
- let params = getDecoratorDependencies ( target , 'parent' ) ;
384
- params [ index ] = Parent . of ( params [ index ] ) ;
388
+ let inject = getDecoratorDependencies ( target ) ;
389
+ inject [ index ] = Parent . of ( inject [ index ] ) ;
385
390
}
386
391
387
392
/**
388
393
* Decorator: Specifies the dependency to create a factory method, that can accept optional arguments
389
394
*/
390
395
export function factory ( keyValue : any , asValue ?: any ) {
391
396
return function ( target , key , index ) {
392
- let params = getDecoratorDependencies ( target , 'factory' ) ;
397
+ let inject = getDecoratorDependencies ( target ) ;
393
398
let factory = Factory . of ( keyValue ) ;
394
- params [ index ] = asValue ? factory . as ( asValue ) : factory ;
399
+ inject [ index ] = asValue ? factory . as ( asValue ) : factory ;
395
400
} ;
396
401
}
397
402
@@ -401,10 +406,10 @@ export function factory(keyValue: any, asValue?: any) {
401
406
export function newInstance ( asKeyOrTarget ?: any , ...dynamicDependencies : any [ ] ) {
402
407
let deco = function ( asKey ?: any ) {
403
408
return function ( target , key , index ) {
404
- let params = getDecoratorDependencies ( target , 'newInstance' ) ;
405
- params [ index ] = NewInstance . of ( params [ index ] , ...dynamicDependencies ) ;
409
+ let inject = getDecoratorDependencies ( target ) ;
410
+ inject [ index ] = NewInstance . of ( inject [ index ] , ...dynamicDependencies ) ;
406
411
if ( ! ! asKey ) {
407
- params [ index ] . as ( asKey ) ;
412
+ inject [ index ] . as ( asKey ) ;
408
413
}
409
414
} ;
410
415
} ;
0 commit comments