angular-cn/modules/di/src/binding.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

import {FIELD, Type, isBlank} from 'facade/lang';
import {List, MapWrapper, ListWrapper} from 'facade/collection';
import {reflector} from './reflector';
import {Key} from './key';
export class Dependency {
2014-10-12 17:03:22 -04:00
@FIELD('final key:Key')
2014-10-10 15:44:56 -04:00
@FIELD('final asPromise:bool')
2014-10-12 17:03:22 -04:00
@FIELD('final lazy:bool')
constructor(key:Key, asPromise:boolean, lazy:boolean, properties:List) {
this.key = key;
2014-10-10 15:44:56 -04:00
this.asPromise = asPromise;
this.lazy = lazy;
this.properties = properties;
}
}
export class Binding {
2014-10-10 15:44:56 -04:00
constructor(key:Key, factory:Function, dependencies:List, providedAsPromise:boolean) {
this.key = key;
this.factory = factory;
this.dependencies = dependencies;
2014-10-10 15:44:56 -04:00
this.providedAsPromise = providedAsPromise;
}
}
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),
reflector.factoryFor(type),
reflector.dependencies(type),
false
);
}
toValue(value):Binding {
return new Binding(
Key.get(this.token),
() => value,
[],
false
);
}
toFactory(factoryFunction:Function, dependencies:List = null):Binding {
return new Binding(
Key.get(this.token),
factoryFunction,
this._constructDependencies(factoryFunction, dependencies),
false
);
}
toAsyncFactory(factoryFunction:Function, dependencies:List = null):Binding {
return new Binding(
Key.get(this.token),
factoryFunction,
this._constructDependencies(factoryFunction, dependencies),
true
);
}
_constructDependencies(factoryFunction:Function, dependencies:List) {
return isBlank(dependencies) ?
reflector.dependencies(factoryFunction) :
ListWrapper.map(dependencies, (t) => new Dependency(Key.get(t), false, false, []));
}
2014-10-09 12:09:50 -04:00
}