2014-10-07 09:37:45 -04:00
|
|
|
import {Type, bool} 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-05 16:25:42 -04:00
|
|
|
import {Key, Dependency} from './key';
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
toFactory(dependencies:List, factoryFunction:Function):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-07 10:17:51 -04:00
|
|
|
reflector.convertToFactory(factoryFunction),
|
2014-10-05 16:25:42 -04:00
|
|
|
this._constructDependencies(dependencies),
|
2014-09-30 14:56:33 -04:00
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
toAsyncFactory(dependencies:List, factoryFunction:Function):Binding {
|
|
|
|
return new Binding(
|
|
|
|
Key.get(this.token),
|
2014-10-07 10:17:51 -04:00
|
|
|
reflector.convertToFactory(factoryFunction),
|
2014-10-05 16:25:42 -04:00
|
|
|
this._constructDependencies(dependencies),
|
2014-09-30 14:56:33 -04:00
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
_constructDependencies(deps:List) {
|
2014-10-06 13:45:24 -04:00
|
|
|
return ListWrapper.map(deps, (t) => new Dependency(Key.get(t), false, false));
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
}
|