2015-06-11 19:32:55 +02:00
|
|
|
import {
|
|
|
|
Type,
|
|
|
|
isBlank,
|
|
|
|
isPresent,
|
|
|
|
CONST,
|
2015-06-17 10:12:06 +02:00
|
|
|
CONST_EXPR,
|
2015-06-11 19:32:55 +02:00
|
|
|
BaseException,
|
|
|
|
stringify,
|
|
|
|
isArray
|
|
|
|
} from 'angular2/src/facade/lang';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {reflector} from 'angular2/src/reflection/reflection';
|
2014-10-09 10:55:18 -04:00
|
|
|
import {Key} from './key';
|
2015-04-24 15:19:11 -07:00
|
|
|
import {
|
|
|
|
Inject,
|
2015-06-26 15:59:18 -07:00
|
|
|
Injectable,
|
|
|
|
Visibility,
|
2015-04-24 15:19:11 -07:00
|
|
|
Optional,
|
2015-06-26 15:59:18 -07:00
|
|
|
unbounded,
|
2015-04-24 15:19:11 -07:00
|
|
|
DependencyAnnotation
|
|
|
|
} from './annotations_impl';
|
2015-04-13 14:29:32 -07:00
|
|
|
import {NoAnnotationError} from './exceptions';
|
2015-05-13 15:54:46 -07:00
|
|
|
import {resolveForwardRef} from './forward_ref';
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2014-10-09 10:55:18 -04:00
|
|
|
export class Dependency {
|
2015-06-26 15:59:18 -07:00
|
|
|
constructor(public key: Key, public optional: boolean, public visibility: Visibility,
|
|
|
|
public properties: List<any>) {}
|
2015-02-27 07:42:51 -08:00
|
|
|
|
2015-06-26 11:10:52 -07:00
|
|
|
static fromKey(key: Key): Dependency {
|
|
|
|
return new Dependency(key, false, _defaulVisiblity(key.token), []);
|
|
|
|
}
|
2014-10-09 10:55:18 -04:00
|
|
|
}
|
2014-10-09 11:35:13 -04:00
|
|
|
|
2015-06-17 10:12:06 +02:00
|
|
|
const _EMPTY_LIST = CONST_EXPR([]);
|
2015-04-09 23:17:05 -07:00
|
|
|
|
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Describes how the {@link Injector} should instantiate a given token.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-04-17 13:01:07 -07:00
|
|
|
* See {@link bind}.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* new Binding(String, { toValue: 'Hello' })
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(String)).toEqual('Hello');
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/di
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
@CONST()
|
2014-09-30 14:56:33 -04:00
|
|
|
export class Binding {
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Token used when retrieving this binding. Usually the `Type`.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-04-09 23:17:05 -07:00
|
|
|
token;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds an interface to an implementation / subclass.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* Becuse `toAlias` and `toClass` are often confused, the example contains both use cases for easy
|
|
|
|
* comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
*
|
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* new Binding(Vehicle, { toClass: Car })
|
|
|
|
* ]);
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* new Binding(Vehicle, { toAlias: Car })
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
toClass: Type;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds a key to a value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* new Binding(String, { toValue: 'Hello' })
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(String)).toEqual('Hello');
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-09 23:17:05 -07:00
|
|
|
toValue;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds a key to the alias for an existing key.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* An alias means that {@link Injector} returns the same instance as if the alias token was used.
|
|
|
|
* This is in contrast to `toClass` where a separate instance of `toClass` is returned.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* Becuse `toAlias` and `toClass` are often confused the example contains both use cases for easy
|
|
|
|
* comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
*
|
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* new Binding(Vehicle, { toAlias: Car })
|
|
|
|
* ]);
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* new Binding(Vehicle, { toClass: Car })
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-09 23:17:05 -07:00
|
|
|
toAlias;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds a key to a function which computes the value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* new Binding(Number, { toFactory: () => { return 1+2; }}),
|
|
|
|
* new Binding(String, { toFactory: (value) => { return "Value: " + value; },
|
2015-05-14 14:28:47 +02:00
|
|
|
* dependencies: [Number] })
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Number)).toEqual(3);
|
|
|
|
* expect(injector.get(String)).toEqual('Value: 3');
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
toFactory: Function;
|
2015-04-15 22:35:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-26 15:59:18 -07:00
|
|
|
* Used in conjunction with `toFactory` and specifies a set of dependencies
|
2015-04-24 15:19:11 -07:00
|
|
|
* (as `token`s) which should be injected into the factory function.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* new Binding(Number, { toFactory: () => { return 1+2; }}),
|
|
|
|
* new Binding(String, { toFactory: (value) => { return "Value: " + value; },
|
2015-05-14 14:28:47 +02:00
|
|
|
* dependencies: [Number] })
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Number)).toEqual(3);
|
|
|
|
* expect(injector.get(String)).toEqual('Value: 3');
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
dependencies: List<any>;
|
|
|
|
|
2015-06-26 15:59:18 -07:00
|
|
|
constructor(token, {toClass, toValue, toAlias, toFactory, deps}: {
|
2015-05-27 11:52:35 -07:00
|
|
|
toClass?: Type,
|
|
|
|
toValue?: any,
|
|
|
|
toAlias?: any,
|
|
|
|
toFactory?: Function,
|
|
|
|
deps?: List<any>
|
|
|
|
}) {
|
2015-04-09 23:17:05 -07:00
|
|
|
this.token = token;
|
|
|
|
this.toClass = toClass;
|
|
|
|
this.toValue = toValue;
|
|
|
|
this.toAlias = toAlias;
|
|
|
|
this.toFactory = toFactory;
|
|
|
|
this.dependencies = deps;
|
|
|
|
}
|
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Converts the {@link Binding} into {@link ResolvedBinding}.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* {@link Injector} internally only uses {@link ResolvedBinding}, {@link Binding} contains
|
|
|
|
* convenience binding syntax.
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-04-09 23:17:05 -07:00
|
|
|
resolve(): ResolvedBinding {
|
2015-04-24 15:19:11 -07:00
|
|
|
var factoryFn: Function;
|
2015-04-09 23:17:05 -07:00
|
|
|
var resolvedDeps;
|
|
|
|
if (isPresent(this.toClass)) {
|
2015-05-13 15:54:46 -07:00
|
|
|
var toClass = resolveForwardRef(this.toClass);
|
|
|
|
factoryFn = reflector.factory(toClass);
|
|
|
|
resolvedDeps = _dependenciesFor(toClass);
|
2015-04-09 23:17:05 -07:00
|
|
|
} else if (isPresent(this.toAlias)) {
|
|
|
|
factoryFn = (aliasInstance) => aliasInstance;
|
|
|
|
resolvedDeps = [Dependency.fromKey(Key.get(this.toAlias))];
|
|
|
|
} else if (isPresent(this.toFactory)) {
|
|
|
|
factoryFn = this.toFactory;
|
|
|
|
resolvedDeps = _constructDependencies(this.toFactory, this.dependencies);
|
|
|
|
} else {
|
|
|
|
factoryFn = () => this.toValue;
|
|
|
|
resolvedDeps = _EMPTY_LIST;
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 -07:00
|
|
|
return new ResolvedBinding(Key.get(this.token), factoryFn, resolvedDeps);
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* An internal resolved representation of a {@link Binding} used by the {@link Injector}.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* A {@link Binding} is resolved when it has a factory function. Binding to a class, alias, or
|
|
|
|
* value, are just convenience methods, as {@link Injector} only operates on calling factory
|
|
|
|
* functions.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
|
|
|
* @exportedAs angular2/di
|
2015-04-15 22:35:38 +00:00
|
|
|
*/
|
2015-04-09 23:17:05 -07:00
|
|
|
export class ResolvedBinding {
|
2015-05-11 10:28:07 -07:00
|
|
|
constructor(
|
|
|
|
/**
|
|
|
|
* A key, usually a `Type`.
|
|
|
|
*/
|
|
|
|
public key: Key,
|
2015-04-17 03:29:05 -07:00
|
|
|
|
2015-05-11 10:28:07 -07:00
|
|
|
/**
|
|
|
|
* Factory function which can return an instance of an object represented by a key.
|
|
|
|
*/
|
|
|
|
public factory: Function,
|
2015-04-17 03:29:05 -07:00
|
|
|
|
2015-05-11 10:28:07 -07:00
|
|
|
/**
|
|
|
|
* Arguments (dependencies) to the `factory` function.
|
|
|
|
*/
|
2015-06-26 15:59:18 -07:00
|
|
|
public dependencies: List<Dependency>) {}
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Provides an API for imperatively constructing {@link Binding}s.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-20 16:37:49 +00:00
|
|
|
* This is only relevant for JavaScript. See {@link BindingBuilder}.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* bind(MyInterface).toClass(MyClass)
|
|
|
|
*
|
|
|
|
* ```
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* @exportedAs angular2/di
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
export function bind(token): BindingBuilder {
|
2014-09-30 14:56:33 -04:00
|
|
|
return new BindingBuilder(token);
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
2015-04-17 13:01:07 -07:00
|
|
|
* Helper class for the {@link bind} function.
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* @exportedAs angular2/di
|
2015-04-09 23:17:05 -07:00
|
|
|
*/
|
2014-09-30 14:56:33 -04:00
|
|
|
export class BindingBuilder {
|
2015-05-11 10:28:07 -07:00
|
|
|
constructor(public token) {}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds an interface to an implementation / subclass.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* Because `toAlias` and `toClass` are often confused, the example contains both use cases for
|
|
|
|
* easy comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
*
|
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* bind(Vehicle).toClass(Car)
|
|
|
|
* ]);
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* bind(Vehicle).toAlias(Car)
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
toClass(type: Type): Binding { return new Binding(this.token, {toClass: type}); }
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds a key to a value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
|
|
|
* bind(String).toValue('Hello')
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(String)).toEqual('Hello');
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
toValue(value): Binding { return new Binding(this.token, {toValue: value}); }
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds a key to the alias for an existing key.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* An alias means that we will return the same instance as if the alias token was used. (This is
|
|
|
|
* in contrast to `toClass` where a separet instance of `toClass` will be returned.)
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
2015-04-24 15:19:11 -07:00
|
|
|
* Becuse `toAlias` and `toClass` are often confused, the example contains both use cases for easy
|
|
|
|
* comparison.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
*
|
|
|
|
* class Vehicle {}
|
|
|
|
*
|
|
|
|
* class Car extends Vehicle {}
|
|
|
|
*
|
|
|
|
* var injectorAlias = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* bind(Vehicle).toAlias(Car)
|
|
|
|
* ]);
|
|
|
|
* var injectorClass = Injector.resolveAndCreate([
|
|
|
|
* Car,
|
|
|
|
* bind(Vehicle).toClass(Car)
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car));
|
|
|
|
* expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);
|
2015-04-17 03:29:05 -07:00
|
|
|
*
|
2015-04-15 22:35:38 +00:00
|
|
|
* expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car));
|
|
|
|
* expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);
|
|
|
|
* ```
|
|
|
|
*/
|
2015-05-26 10:55:12 +02:00
|
|
|
toAlias(aliasToken): Binding {
|
|
|
|
if (isBlank(aliasToken)) {
|
|
|
|
throw new BaseException(`Can not alias ${stringify(this.token)} to a blank value!`);
|
|
|
|
}
|
|
|
|
return new Binding(this.token, {toAlias: aliasToken});
|
|
|
|
}
|
2015-02-21 15:18:06 +01:00
|
|
|
|
2015-04-15 22:35:38 +00:00
|
|
|
/**
|
2015-04-17 03:29:05 -07:00
|
|
|
* Binds a key to a function which computes the value.
|
2015-04-15 22:35:38 +00:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-05-28 17:53:13 +02:00
|
|
|
* bind(Number).toFactory(() => { return 1+2; }),
|
|
|
|
* bind(String).toFactory((v) => { return "Value: " + v; }, [Number])
|
2015-04-15 22:35:38 +00:00
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* expect(injector.get(Number)).toEqual(3);
|
|
|
|
* expect(injector.get(String)).toEqual('Value: 3');
|
|
|
|
* ```
|
|
|
|
*/
|
2015-04-24 15:19:11 -07:00
|
|
|
toFactory(factoryFunction: Function, dependencies?: List<any>): Binding {
|
|
|
|
return new Binding(this.token, {toFactory: factoryFunction, deps: dependencies});
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
2015-04-09 23:17:05 -07:00
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-05-18 17:19:54 -07:00
|
|
|
function _constructDependencies(factoryFunction: Function,
|
|
|
|
dependencies: List<any>): List<Dependency> {
|
|
|
|
if (isBlank(dependencies)) {
|
|
|
|
return _dependenciesFor(factoryFunction);
|
|
|
|
} else {
|
|
|
|
var params: List<List<any>> = ListWrapper.map(dependencies, (t) => [t]);
|
|
|
|
return ListWrapper.map(dependencies, (t) => _extractToken(factoryFunction, t, params));
|
|
|
|
}
|
2014-10-09 12:09:50 -04:00
|
|
|
}
|
2014-11-20 12:07:48 -08:00
|
|
|
|
2015-05-18 17:19:54 -07:00
|
|
|
function _dependenciesFor(typeOrFunc): List<Dependency> {
|
2014-11-20 12:07:48 -08:00
|
|
|
var params = reflector.parameters(typeOrFunc);
|
|
|
|
if (isBlank(params)) return [];
|
2015-05-01 14:05:19 -07:00
|
|
|
if (ListWrapper.any(params, (p) => isBlank(p))) {
|
2015-05-18 17:19:54 -07:00
|
|
|
throw new NoAnnotationError(typeOrFunc, params);
|
2015-05-01 14:05:19 -07:00
|
|
|
}
|
2015-05-18 17:19:54 -07:00
|
|
|
return ListWrapper.map(params, (p: List<any>) => _extractToken(typeOrFunc, p, params));
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|
|
|
|
|
2015-05-18 17:19:54 -07:00
|
|
|
function _extractToken(typeOrFunc, annotations /*List<any> | any*/,
|
|
|
|
params: List<List<any>>): Dependency {
|
2014-11-20 12:07:48 -08:00
|
|
|
var depProps = [];
|
2015-02-27 07:42:51 -08:00
|
|
|
var token = null;
|
|
|
|
var optional = false;
|
2014-11-20 12:07:48 -08:00
|
|
|
|
2015-06-11 19:32:55 +02:00
|
|
|
if (!isArray(annotations)) {
|
2015-06-26 15:59:18 -07:00
|
|
|
return _createDependency(annotations, optional, _defaulVisiblity(annotations), depProps);
|
2015-05-21 08:34:48 -07:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 -07:00
|
|
|
var visibility = null;
|
|
|
|
var defaultVisibility = unbounded;
|
|
|
|
|
2014-11-20 12:07:48 -08:00
|
|
|
for (var i = 0; i < annotations.length; ++i) {
|
|
|
|
var paramAnnotation = annotations[i];
|
|
|
|
|
|
|
|
if (paramAnnotation instanceof Type) {
|
2015-02-27 07:42:51 -08:00
|
|
|
token = paramAnnotation;
|
2015-06-26 15:59:18 -07:00
|
|
|
defaultVisibility = _defaulVisiblity(token);
|
2014-11-20 12:07:48 -08:00
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof Inject) {
|
2015-02-27 07:42:51 -08:00
|
|
|
token = paramAnnotation.token;
|
2014-11-20 12:07:48 -08:00
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
} else if (paramAnnotation instanceof Optional) {
|
|
|
|
optional = true;
|
2014-11-20 12:07:48 -08:00
|
|
|
|
2015-06-26 15:59:18 -07:00
|
|
|
} else if (paramAnnotation instanceof Visibility) {
|
|
|
|
visibility = paramAnnotation;
|
|
|
|
|
2014-11-20 12:07:48 -08:00
|
|
|
} else if (paramAnnotation instanceof DependencyAnnotation) {
|
2015-03-29 14:56:18 +02:00
|
|
|
if (isPresent(paramAnnotation.token)) {
|
2015-04-24 15:19:11 -07:00
|
|
|
token = paramAnnotation.token;
|
2015-03-29 14:56:18 +02:00
|
|
|
}
|
2015-06-17 11:17:21 -07:00
|
|
|
depProps.push(paramAnnotation);
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 -07:00
|
|
|
if (isBlank(visibility)) {
|
|
|
|
visibility = defaultVisibility;
|
|
|
|
}
|
|
|
|
|
2015-05-13 15:54:46 -07:00
|
|
|
token = resolveForwardRef(token);
|
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
if (isPresent(token)) {
|
2015-06-26 15:59:18 -07:00
|
|
|
return _createDependency(token, optional, visibility, depProps);
|
2014-11-20 12:07:48 -08:00
|
|
|
} else {
|
2015-05-18 17:19:54 -07:00
|
|
|
throw new NoAnnotationError(typeOrFunc, params);
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 15:59:18 -07:00
|
|
|
function _defaulVisiblity(typeOrFunc) {
|
|
|
|
try {
|
|
|
|
if (!(typeOrFunc instanceof Type)) return unbounded;
|
|
|
|
var f = ListWrapper.filter(reflector.annotations(typeOrFunc), s => s instanceof Injectable);
|
|
|
|
return f.length === 0 ? unbounded : f[0].visibility;
|
|
|
|
} catch (e) {
|
|
|
|
return unbounded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _createDependency(token, optional, visibility, depProps): Dependency {
|
|
|
|
return new Dependency(Key.get(token), optional, visibility, depProps);
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|