| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  | import {CONST, CONST_EXPR, stringify, isBlank, isPresent} from "angular2/src/facade/lang"; | 
					
						
							| 
									
										
										
										
											2014-10-03 20:34:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  |  * A parameter metadata that specifies a dependency. | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * ```
 | 
					
						
							|  |  |  |  * class AComponent { | 
					
						
							| 
									
										
										
										
											2015-04-15 22:35:38 +00:00
										 |  |  |  *   constructor(@Inject(MyService) aService:MyService) {} | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class InjectMetadata { | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  |   constructor(public token) {} | 
					
						
							| 
									
										
										
										
											2015-06-17 10:12:06 +02:00
										 |  |  |   toString(): string { return `@Inject(${stringify(this.token)})`; } | 
					
						
							| 
									
										
										
										
											2014-10-05 16:25:42 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 07:42:51 -08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  |  * A parameter metadata that marks a dependency as optional. {@link Injector} provides `null` if | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  |  * the dependency is not found. | 
					
						
							| 
									
										
										
										
											2015-04-17 13:01:07 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-02-27 07:42:51 -08:00
										 |  |  |  * ```
 | 
					
						
							|  |  |  |  * class AComponent { | 
					
						
							| 
									
										
										
										
											2015-04-15 22:35:38 +00:00
										 |  |  |  *   constructor(@Optional() aService:MyService) { | 
					
						
							|  |  |  |  *     this.aService = aService; | 
					
						
							| 
									
										
										
										
											2015-02-27 07:42:51 -08:00
										 |  |  |  *   } | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class OptionalMetadata { | 
					
						
							| 
									
										
										
										
											2015-06-17 10:12:06 +02:00
										 |  |  |   toString(): string { return `@Optional()`; } | 
					
						
							| 
									
										
										
										
											2015-02-27 07:42:51 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  |  * `DependencyMetadata is used by the framework to extend DI.
 | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  |  * Only metadata implementing `DependencyMetadata` are added to the list of dependency | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  |  * properties. | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * For example: | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |  * class Exclude extends DependencyMetadata {} | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * class NotDependencyProperty {} | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * class AComponent { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |  *   constructor(@Exclude @NotDependencyProperty aService:AService) {} | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * | 
					
						
							|  |  |  |  * will create the following dependency: | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |  * new Dependency(Key.get(AService), [new Exclude()]) | 
					
						
							| 
									
										
										
										
											2014-12-04 11:01:19 +01:00
										 |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |  * The framework can use `new Exclude()` to handle the `aService` dependency | 
					
						
							| 
									
										
										
										
											2014-10-14 16:00:35 -04:00
										 |  |  |  * in a specific way. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class DependencyMetadata { | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  |   get token() { return null; } | 
					
						
							| 
									
										
										
										
											2015-03-16 14:43:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  |  * A marker metadata that marks a class as available to `Injector` for creation. Used by tooling | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  |  * for generating constructor stubs. | 
					
						
							| 
									
										
										
										
											2015-03-16 14:43:22 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * class NeedsService { | 
					
						
							|  |  |  |  *   constructor(svc:UsefulService) {} | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @Injectable | 
					
						
							|  |  |  |  * class UsefulService {} | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-24 15:19:11 -07:00
										 |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class InjectableMetadata { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:48:28 -07:00
										 |  |  |   constructor() {} | 
					
						
							| 
									
										
										
										
											2015-03-16 14:43:22 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Specifies how injector should resolve a dependency. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |  * See {@link Self}, {@link Ancestor}, {@link Unbounded}. | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class VisibilityMetadata { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |   constructor(public crossBoundaries: boolean, public _includeSelf: boolean) {} | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   get includeSelf(): boolean { return isBlank(this._includeSelf) ? false : this._includeSelf; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   toString(): string { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |     return `@Visibility(crossBoundaries: ${this.crossBoundaries}, includeSelf: ${this.includeSelf}})`; | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Specifies that an injector should retrieve a dependency from itself. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * class Dependency { | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * class NeedsDependency { | 
					
						
							|  |  |  |  *   constructor(public @Self() dependency:Dependency) {} | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * var inj = Injector.resolveAndCreate([Dependency, NeedsDependency]); | 
					
						
							|  |  |  |  * var nd = inj.get(NeedsDependency); | 
					
						
							|  |  |  |  * expect(nd.dependency).toBeAnInstanceOf(Dependency); | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class SelfMetadata extends VisibilityMetadata { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |   constructor() { super(false, true); } | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |   toString(): string { return `@Self()`; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Specifies that an injector should retrieve a dependency from any ancestor from the same boundary. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * class Dependency { | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * class NeedsDependency { | 
					
						
							|  |  |  |  *   constructor(public @Ancestor() dependency:Dependency) {} | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * var parent = Injector.resolveAndCreate([ | 
					
						
							|  |  |  |  *   bind(Dependency).toClass(AncestorDependency) | 
					
						
							|  |  |  |  * ]); | 
					
						
							|  |  |  |  * var child = parent.resolveAndCreateChild([]); | 
					
						
							|  |  |  |  * var grandChild = child.resolveAndCreateChild([NeedsDependency, Depedency]); | 
					
						
							|  |  |  |  * var nd = grandChild.get(NeedsDependency); | 
					
						
							|  |  |  |  * expect(nd.dependency).toBeAnInstanceOf(AncestorDependency); | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You can make an injector to retrive a dependency either from itself or its ancestor by setting | 
					
						
							|  |  |  |  * self to true. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * class NeedsDependency { | 
					
						
							|  |  |  |  *   constructor(public @Ancestor({self:true}) dependency:Dependency) {} | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class AncestorMetadata extends VisibilityMetadata { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |   constructor({self}: {self?: boolean} = {}) { super(false, self); } | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |   toString(): string { return `@Ancestor(self: ${this.includeSelf}})`; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Specifies that an injector should retrieve a dependency from any ancestor, crossing boundaries. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ## Example | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * class Dependency { | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * class NeedsDependency { | 
					
						
							|  |  |  |  *   constructor(public @Ancestor() dependency:Dependency) {} | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * var parent = Injector.resolveAndCreate([ | 
					
						
							|  |  |  |  *   bind(Dependency).toClass(AncestorDependency) | 
					
						
							|  |  |  |  * ]); | 
					
						
							|  |  |  |  * var child = parent.resolveAndCreateChild([]); | 
					
						
							|  |  |  |  * var grandChild = child.resolveAndCreateChild([NeedsDependency, Depedency]); | 
					
						
							|  |  |  |  * var nd = grandChild.get(NeedsDependency); | 
					
						
							|  |  |  |  * expect(nd.dependency).toBeAnInstanceOf(AncestorDependency); | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You can make an injector to retrive a dependency either from itself or its ancestor by setting | 
					
						
							|  |  |  |  * self to true. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * class NeedsDependency { | 
					
						
							|  |  |  |  *   constructor(public @Ancestor({self:true}) dependency:Dependency) {} | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | @CONST() | 
					
						
							| 
									
										
										
										
											2015-07-08 12:04:24 -07:00
										 |  |  | export class UnboundedMetadata extends VisibilityMetadata { | 
					
						
							| 
									
										
										
										
											2015-07-13 15:57:06 -07:00
										 |  |  |   constructor({self}: {self?: boolean} = {}) { super(true, self); } | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  |   toString(): string { return `@Unbounded(self: ${this.includeSelf}})`; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 15:48:28 -07:00
										 |  |  | export const DEFAULT_VISIBILITY = CONST_EXPR(new UnboundedMetadata({self: true})); |