2015-02-05 13:08:05 -08:00
|
|
|
import {FIELD, Type, isBlank, isPresent} from 'angular2/src/facade/lang';
|
|
|
|
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';
|
2014-11-20 12:07:48 -08:00
|
|
|
import {Inject, InjectLazy, InjectPromise, DependencyAnnotation} from './annotations';
|
|
|
|
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;
|
|
|
|
properties:List;
|
2014-10-14 16:00:35 -04:00
|
|
|
constructor(key:Key, asPromise:boolean, lazy: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;
|
2014-10-14 16:00:35 -04:00
|
|
|
this.properties = properties;
|
2014-10-09 10:55:18 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-09 11:35:13 -04:00
|
|
|
|
2014-09-30 14:56:33 -04:00
|
|
|
export class Binding {
|
2014-11-21 21:19:23 -08:00
|
|
|
key:Key;
|
|
|
|
factory:Function;
|
|
|
|
dependencies:List;
|
|
|
|
providedAsPromise:boolean;
|
|
|
|
|
2014-10-10 15:44:56 -04:00
|
|
|
constructor(key:Key, factory:Function, dependencies:List, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function bind(token):BindingBuilder {
|
|
|
|
return new BindingBuilder(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BindingBuilder {
|
2014-11-21 21:19:23 -08:00
|
|
|
token;
|
2014-09-30 14:56:33 -04:00
|
|
|
constructor(token) {
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
toClass(type:Type):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-11-20 12:07:48 -08:00
|
|
|
reflector.factory(type),
|
|
|
|
_dependenciesFor(type),
|
2014-09-30 14:56:33 -04:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
toValue(value):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-14 16:00:35 -04:00
|
|
|
() => value,
|
2014-09-30 14:56:33 -04:00
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-21 15:18:06 +01:00
|
|
|
toAlias(aliasToken):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
|
|
|
(aliasInstance) => aliasInstance,
|
|
|
|
[new Dependency(Key.get(aliasToken), false, false, [])],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-10 09:56:43 -04:00
|
|
|
toFactory(factoryFunction:Function, dependencies:List = null):Binding {
|
2014-09-30 14:56:33 -04:00
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-14 16:00:35 -04:00
|
|
|
factoryFunction,
|
2014-10-09 11:35:13 -04:00
|
|
|
this._constructDependencies(factoryFunction, dependencies),
|
2014-09-30 14:56:33 -04:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-10 09:56:43 -04:00
|
|
|
toAsyncFactory(factoryFunction:Function, dependencies:List = null):Binding {
|
2014-09-30 14:56:33 -04:00
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-14 16:00:35 -04:00
|
|
|
factoryFunction,
|
2014-10-09 11:35:13 -04:00
|
|
|
this._constructDependencies(factoryFunction, dependencies),
|
2014-09-30 14:56:33 -04:00
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-09 11:35:13 -04:00
|
|
|
_constructDependencies(factoryFunction:Function, dependencies:List) {
|
|
|
|
return isBlank(dependencies) ?
|
2014-11-20 12:07:48 -08:00
|
|
|
_dependenciesFor(factoryFunction) :
|
2014-10-14 16:00:35 -04:00
|
|
|
ListWrapper.map(dependencies, (t) => new Dependency(Key.get(t), false, false, []));
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
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 type;
|
|
|
|
var depProps = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < annotations.length; ++i) {
|
|
|
|
var paramAnnotation = annotations[i];
|
|
|
|
|
|
|
|
if (paramAnnotation instanceof Type) {
|
|
|
|
type = paramAnnotation;
|
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof Inject) {
|
|
|
|
return _createDependency(paramAnnotation.token, false, false, []);
|
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof InjectPromise) {
|
|
|
|
return _createDependency(paramAnnotation.token, true, false, []);
|
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof InjectLazy) {
|
|
|
|
return _createDependency(paramAnnotation.token, false, true, []);
|
|
|
|
|
|
|
|
} else if (paramAnnotation instanceof DependencyAnnotation) {
|
|
|
|
ListWrapper.push(depProps, paramAnnotation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPresent(type)) {
|
|
|
|
return _createDependency(type, false, false, depProps);
|
|
|
|
} else {
|
|
|
|
throw new NoAnnotationError(typeOrFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _createDependency(token, asPromise, lazy, depProps):Dependency {
|
|
|
|
return new Dependency(Key.get(token), asPromise, lazy, depProps);
|
|
|
|
}
|