2014-10-09 11:35:13 -04:00
|
|
|
import {FIELD, Type, bool, isBlank} from 'facade/lang';
|
2014-09-30 14:56:33 -04:00
|
|
|
import {List, MapWrapper, ListWrapper} from 'facade/collection';
|
2014-10-07 10:17:51 -04:00
|
|
|
import {reflector} from './reflector';
|
2014-10-09 10:55:18 -04:00
|
|
|
import {Key} from './key';
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2014-10-09 10:55:18 -04:00
|
|
|
@FIELD('final key:Key')
|
|
|
|
@FIELD('final asFuture:bool')
|
|
|
|
@FIELD('final lazy:bool')
|
|
|
|
export class Dependency {
|
|
|
|
constructor(key:Key, asFuture:bool, lazy:bool) {
|
|
|
|
this.key = key;
|
|
|
|
this.asFuture = asFuture;
|
|
|
|
this.lazy = lazy;
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 11:35:13 -04:00
|
|
|
|
2014-09-30 14:56:33 -04:00
|
|
|
export class Binding {
|
2014-10-07 09:37:45 -04:00
|
|
|
constructor(key:Key, factory:Function, dependencies:List, providedAsFuture:bool) {
|
2014-09-30 14:56:33 -04:00
|
|
|
this.key = key;
|
|
|
|
this.factory = factory;
|
|
|
|
this.dependencies = dependencies;
|
2014-10-05 16:25:42 -04:00
|
|
|
this.providedAsFuture = providedAsFuture;
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function bind(token):BindingBuilder {
|
|
|
|
return new BindingBuilder(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BindingBuilder {
|
|
|
|
constructor(token) {
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
toClass(type:Type):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-07 10:17:51 -04:00
|
|
|
reflector.factoryFor(type),
|
|
|
|
reflector.dependencies(type),
|
2014-09-30 14:56:33 -04:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
toValue(value):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
|
|
|
(_) => value,
|
|
|
|
[],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-09 11:35:13 -04:00
|
|
|
toFactory(factoryFunction:Function, {dependencies=null}={}):Binding {
|
2014-09-30 14:56:33 -04:00
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-07 10:17:51 -04:00
|
|
|
reflector.convertToFactory(factoryFunction),
|
2014-10-09 11:35:13 -04:00
|
|
|
this._constructDependencies(factoryFunction, dependencies),
|
2014-09-30 14:56:33 -04:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-09 11:35:13 -04:00
|
|
|
toAsyncFactory(factoryFunction:Function, {dependencies=null}={}):Binding {
|
2014-09-30 14:56:33 -04:00
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-07 10:17:51 -04:00
|
|
|
reflector.convertToFactory(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) ?
|
|
|
|
reflector.dependencies(factoryFunction) :
|
|
|
|
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
|
|
|
}
|