2014-09-30 14:56:33 -04:00
|
|
|
import {Map, List, MapWrapper, ListWrapper} from 'facade/collection';
|
|
|
|
import {Binding, BindingBuilder, bind} from './binding';
|
2014-10-06 10:13:33 -04:00
|
|
|
import {ProviderError, NoProviderError, InvalidBindingError,
|
|
|
|
AsyncBindingError, CyclicDependencyError, InstantiationError} from './exceptions';
|
2014-09-30 14:56:33 -04:00
|
|
|
import {Type, isPresent, isBlank} from 'facade/lang';
|
|
|
|
import {Future, FutureWrapper} from 'facade/async';
|
|
|
|
import {Key} from './key';
|
|
|
|
|
2014-10-06 10:13:33 -04:00
|
|
|
var _creating = new Object();
|
|
|
|
|
2014-09-30 14:56:33 -04:00
|
|
|
export class Injector {
|
|
|
|
constructor(bindings:List) {
|
|
|
|
var flatten = _flattenBindings(bindings);
|
|
|
|
this._bindings = this._createListOfBindings(flatten);
|
|
|
|
this._instances = this._createInstances();
|
|
|
|
this._parent = null; //TODO: vsavkin make a parameter
|
|
|
|
}
|
|
|
|
|
|
|
|
_createListOfBindings(flattenBindings):List {
|
|
|
|
var bindings = ListWrapper.createFixedSize(Key.numberOfKeys() + 1);
|
|
|
|
MapWrapper.forEach(flattenBindings, (keyId, v) => bindings[keyId] = v);
|
|
|
|
return bindings;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createInstances():List {
|
|
|
|
return ListWrapper.createFixedSize(Key.numberOfKeys() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
get(token) {
|
|
|
|
return this.getByKey(Key.get(token));
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncGet(token) {
|
|
|
|
return this.asyncGetByKey(Key.get(token));
|
|
|
|
}
|
|
|
|
|
|
|
|
getByKey(key:Key) {
|
2014-10-03 17:26:49 -04:00
|
|
|
return this._getByKey(key, false);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
asyncGetByKey(key:Key) {
|
2014-10-03 17:26:49 -04:00
|
|
|
return this._getByKey(key, true);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
_getByKey(key:Key, returnFuture) {
|
2014-09-30 14:56:33 -04:00
|
|
|
var keyId = key.id;
|
2014-10-05 16:25:42 -04:00
|
|
|
if (key.token === Injector) return this._injector(returnFuture);
|
2014-09-30 14:56:33 -04:00
|
|
|
|
|
|
|
var instance = this._get(this._instances, keyId);
|
2014-10-06 10:13:33 -04:00
|
|
|
if (instance === _creating) {
|
|
|
|
throw new CyclicDependencyError(key);
|
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
if (isPresent(instance)) return instance;
|
|
|
|
|
|
|
|
var binding = this._get(this._bindings, keyId);
|
|
|
|
if (isPresent(binding)) {
|
2014-10-05 16:25:42 -04:00
|
|
|
return this._instantiate(key, binding, returnFuture);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
if (isPresent(this._parent)) {
|
2014-10-05 16:25:42 -04:00
|
|
|
return this._parent._getByKey(key, returnFuture);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2014-10-03 17:26:49 -04:00
|
|
|
throw new NoProviderError(key);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
createChild(bindings:List):Injector {
|
|
|
|
var inj = new Injector(bindings);
|
|
|
|
inj._parent = this; //TODO: vsavkin: change it when optional parameters are working
|
|
|
|
return inj;
|
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
_injector(returnFuture){
|
|
|
|
return returnFuture ? FutureWrapper.value(this) : this;
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_get(list:List, index){
|
|
|
|
if (list.length <= index) return null;
|
|
|
|
return ListWrapper.get(list, index);
|
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
_instantiate(key:Key, binding:Binding, returnFuture) {
|
2014-10-06 10:13:33 -04:00
|
|
|
ListWrapper.set(this._instances, key.id, _creating);
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
if (binding.providedAsFuture && !returnFuture) {
|
|
|
|
throw new AsyncBindingError(key);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
if (returnFuture) {
|
|
|
|
return this._instantiateAsync(key, binding);
|
2014-09-30 14:56:33 -04:00
|
|
|
} else {
|
2014-10-05 16:25:42 -04:00
|
|
|
return this._instantiateSync(key, binding);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
_instantiateSync(key:Key, binding:Binding) {
|
2014-10-06 10:13:33 -04:00
|
|
|
var deps;
|
|
|
|
try {
|
|
|
|
deps = ListWrapper.map(binding.dependencies, d => this._getByKey(d.key, d.asFuture));
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof ProviderError) e.addKey(key);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2014-10-03 17:26:49 -04:00
|
|
|
try {
|
|
|
|
var instance = binding.factory(deps);
|
|
|
|
ListWrapper.set(this._instances, key.id, instance);
|
|
|
|
return instance;
|
|
|
|
} catch (e) {
|
2014-10-06 10:13:33 -04:00
|
|
|
throw new InstantiationError(e, key);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 16:25:42 -04:00
|
|
|
_instantiateAsync(key:Key, binding:Binding):Future {
|
2014-09-30 14:56:33 -04:00
|
|
|
var instances = this._createInstances();
|
2014-10-05 16:25:42 -04:00
|
|
|
var futures = ListWrapper.map(binding.dependencies, d => this._getByKey(d.key, true));
|
2014-09-30 14:56:33 -04:00
|
|
|
return FutureWrapper.wait(futures).
|
|
|
|
then(binding.factory).
|
|
|
|
then(function(instance) {
|
|
|
|
ListWrapper.set(instances, key.id, instance);
|
|
|
|
return instance
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _flattenBindings(bindings:List) {
|
|
|
|
var res = {};
|
|
|
|
ListWrapper.forEach(bindings, function (b){
|
|
|
|
if (b instanceof Binding) {
|
|
|
|
MapWrapper.set(res, b.key.id, b);
|
|
|
|
|
|
|
|
} else if (b instanceof Type) {
|
|
|
|
var s = bind(b).toClass(b);
|
|
|
|
MapWrapper.set(res, s.key.id, s);
|
|
|
|
|
|
|
|
} else if (b instanceof BindingBuilder) {
|
|
|
|
throw new InvalidBindingError(b.token);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new InvalidBindingError(b);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|