2015-04-09 23:17:05 -07:00
|
|
|
import {Type, isBlank, isPresent, CONST} 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-02-27 07:42:51 -08:00
|
|
|
import {Inject, InjectLazy, InjectPromise, Optional, DependencyAnnotation} from './annotations';
|
2015-04-13 14:29:32 -07:00
|
|
|
import {NoAnnotationError} from './exceptions';
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2014-10-09 10:55:18 -04:00
|
|
|
export class Dependency {
|
2014-11-21 21:19:23 -08:00
|
|
|
key:Key;
|
|
|
|
asPromise:boolean;
|
|
|
|
lazy:boolean;
|
2015-02-27 07:42:51 -08:00
|
|
|
optional:boolean;
|
2014-11-21 21:19:23 -08:00
|
|
|
properties:List;
|
2015-04-09 23:17:05 -07:00
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
constructor(key:Key, asPromise:boolean, lazy:boolean, optional:boolean, properties:List) {
|
2014-10-09 10:55:18 -04:00
|
|
|
this.key = key;
|
2014-10-10 15:44:56 -04:00
|
|
|
this.asPromise = asPromise;
|
2014-10-09 10:55:18 -04:00
|
|
|
this.lazy = lazy;
|
2015-02-27 07:42:51 -08:00
|
|
|
this.optional = optional;
|
2014-10-14 16:00:35 -04:00
|
|
|
this.properties = properties;
|
2014-10-09 10:55:18 -04:00
|
|
|
}
|
2015-02-27 07:42:51 -08:00
|
|
|
|
|
|
|
static fromKey(key:Key) {
|
|
|
|
return new Dependency(key, false, false, false, []);
|
|
|
|
}
|
2014-10-09 10:55:18 -04:00
|
|
|
}
|
2014-10-09 11:35:13 -04:00
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
var _EMPTY_LIST = []; // TODO: make const when supported
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declaration of a dependency binding.
|
|
|
|
*/
|
2014-09-30 14:56:33 -04:00
|
|
|
export class Binding {
|
2015-04-09 23:17:05 -07:00
|
|
|
token;
|
|
|
|
toClass:Type;
|
|
|
|
toValue;
|
|
|
|
toAlias;
|
|
|
|
toFactory:Function;
|
|
|
|
toAsyncFactory:Function;
|
|
|
|
dependencies:List;
|
|
|
|
|
|
|
|
@CONST()
|
|
|
|
constructor(
|
|
|
|
token,
|
|
|
|
{
|
|
|
|
toClass,
|
|
|
|
toValue,
|
|
|
|
toAlias,
|
|
|
|
toFactory,
|
|
|
|
toAsyncFactory,
|
|
|
|
deps
|
|
|
|
}) {
|
|
|
|
this.token = token;
|
|
|
|
this.toClass = toClass;
|
|
|
|
this.toValue = toValue;
|
|
|
|
this.toAlias = toAlias;
|
|
|
|
this.toFactory = toFactory;
|
|
|
|
this.toAsyncFactory = toAsyncFactory;
|
|
|
|
this.dependencies = deps;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(): ResolvedBinding {
|
|
|
|
var factoryFn:Function;
|
|
|
|
var resolvedDeps;
|
|
|
|
var isAsync = false;
|
|
|
|
if (isPresent(this.toClass)) {
|
|
|
|
factoryFn = reflector.factory(this.toClass);
|
|
|
|
resolvedDeps = _dependenciesFor(this.toClass);
|
|
|
|
} 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 if (isPresent(this.toAsyncFactory)) {
|
|
|
|
factoryFn = this.toAsyncFactory;
|
|
|
|
resolvedDeps = _constructDependencies(this.toAsyncFactory, this.dependencies);
|
|
|
|
isAsync = true;
|
|
|
|
} else {
|
|
|
|
factoryFn = () => this.toValue;
|
|
|
|
resolvedDeps = _EMPTY_LIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ResolvedBinding(
|
|
|
|
Key.get(this.token),
|
|
|
|
factoryFn,
|
|
|
|
resolvedDeps,
|
|
|
|
isAsync
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dependency binding with resolved keys and dependencies.
|
|
|
|
export class ResolvedBinding {
|
2014-11-21 21:19:23 -08:00
|
|
|
key:Key;
|
|
|
|
factory:Function;
|
2015-04-09 23:17:05 -07:00
|
|
|
dependencies:List<Dependency>;
|
2014-11-21 21:19:23 -08:00
|
|
|
providedAsPromise:boolean;
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
constructor(key:Key, factory:Function, dependencies:List<Dependency>, providedAsPromise:boolean) {
|
2014-09-30 14:56:33 -04:00
|
|
|
this.key = key;
|
|
|
|
this.factory = factory;
|
|
|
|
this.dependencies = dependencies;
|
2014-10-10 15:44:56 -04:00
|
|
|
this.providedAsPromise = providedAsPromise;
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
|
|
|
* Provides fluent API for imperative construction of [Binding] objects.
|
|
|
|
*/
|
2014-09-30 14:56:33 -04:00
|
|
|
export function bind(token):BindingBuilder {
|
|
|
|
return new BindingBuilder(token);
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:17:05 -07:00
|
|
|
/**
|
|
|
|
* Helper class for [bind] function.
|
|
|
|
*/
|
2014-09-30 14:56:33 -04:00
|
|
|
export class BindingBuilder {
|
2014-11-21 21:19:23 -08:00
|
|
|
token;
|
2015-04-09 23:17:05 -07:00
|
|
|
|
2014-09-30 14:56:33 -04:00
|
|
|
constructor(token) {
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
toClass(type:Type):Binding {
|
2015-04-09 23:17:05 -07:00
|
|
|
return new Binding(this.token, {toClass: type});
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
toValue(value):Binding {
|
2015-04-09 23:17:05 -07:00
|
|
|
return new Binding(this.token, {toValue: value});
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2015-02-21 15:18:06 +01:00
|
|
|
toAlias(aliasToken):Binding {
|
2015-04-09 23:17:05 -07:00
|
|
|
return new Binding(this.token, {toAlias: aliasToken});
|
2015-02-21 15:18:06 +01:00
|
|
|
}
|
|
|
|
|
2014-10-10 09:56:43 -04:00
|
|
|
toFactory(factoryFunction:Function, dependencies:List = null):Binding {
|
2015-04-09 23:17:05 -07:00
|
|
|
return new Binding(this.token, {
|
|
|
|
toFactory: factoryFunction,
|
|
|
|
deps: dependencies
|
|
|
|
});
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2014-10-10 09:56:43 -04:00
|
|
|
toAsyncFactory(factoryFunction:Function, dependencies:List = null):Binding {
|
2015-04-09 23:17:05 -07:00
|
|
|
return new Binding(this.token, {
|
|
|
|
toAsyncFactory: 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-04-09 23:17:05 -07:00
|
|
|
function _constructDependencies(factoryFunction:Function, dependencies:List) {
|
|
|
|
return isBlank(dependencies) ?
|
|
|
|
_dependenciesFor(factoryFunction) :
|
|
|
|
ListWrapper.map(dependencies, (t) => Dependency.fromKey(Key.get(t)));
|
2014-10-09 12:09:50 -04:00
|
|
|
}
|
2014-11-20 12:07:48 -08:00
|
|
|
|
|
|
|
function _dependenciesFor(typeOrFunc):List {
|
|
|
|
var params = reflector.parameters(typeOrFunc);
|
|
|
|
if (isBlank(params)) return [];
|
|
|
|
if (ListWrapper.any(params, (p) => isBlank(p))) throw new NoAnnotationError(typeOrFunc);
|
|
|
|
return ListWrapper.map(params, (p) => _extractToken(typeOrFunc, p));
|
|
|
|
}
|
|
|
|
|
|
|
|
function _extractToken(typeOrFunc, annotations) {
|
|
|
|
var depProps = [];
|
2015-02-27 07:42:51 -08:00
|
|
|
var token = null;
|
|
|
|
var optional = false;
|
|
|
|
var lazy = false;
|
|
|
|
var asPromise = false;
|
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;
|
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
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof InjectPromise) {
|
2015-02-27 07:42:51 -08:00
|
|
|
token = paramAnnotation.token;
|
|
|
|
asPromise = true;
|
2014-11-20 12:07:48 -08:00
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof InjectLazy) {
|
2015-02-27 07:42:51 -08:00
|
|
|
token = paramAnnotation.token;
|
|
|
|
lazy = true;
|
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof Optional) {
|
|
|
|
optional = true;
|
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)) {
|
|
|
|
token = paramAnnotation.token;
|
|
|
|
}
|
2014-11-20 12:07:48 -08:00
|
|
|
ListWrapper.push(depProps, paramAnnotation);
|
2015-03-29 14:56:18 +02:00
|
|
|
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
if (isPresent(token)) {
|
|
|
|
return _createDependency(token, asPromise, lazy, optional, depProps);
|
2014-11-20 12:07:48 -08:00
|
|
|
} else {
|
|
|
|
throw new NoAnnotationError(typeOrFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 07:42:51 -08:00
|
|
|
function _createDependency(token, asPromise, lazy, optional, depProps):Dependency {
|
|
|
|
return new Dependency(Key.get(token), asPromise, lazy, optional, depProps);
|
2014-11-20 12:07:48 -08:00
|
|
|
}
|