refactor(injector): cleanup

This commit is contained in:
vsavkin 2014-10-09 11:08:00 -04:00
parent 9b411372df
commit 06a221671c
1 changed files with 13 additions and 24 deletions

View File

@ -81,6 +81,17 @@ export class Injector {
throw new NoProviderError(key); throw new NoProviderError(key);
} }
_resolveDependencies(key:Key, binding:Binding, forceAsync:bool):List {
try {
var getDependency = d => this._getByKey(d.key, forceAsync || d.asFuture, d.lazy);
return ListWrapper.map(binding.dependencies, getDependency);
} catch (e) {
this._clear(key);
if (e instanceof ProviderError) e.addKey(key);
throw e;
}
}
_getInstance(key:Key) { _getInstance(key:Key) {
if (this._instances.length <= key.id) return null; if (this._instances.length <= key.id) return null;
return ListWrapper.get(this._instances, key.id); return ListWrapper.get(this._instances, key.id);
@ -135,21 +146,10 @@ class _SyncInjectorStrategy {
//add a marker so we can detect cyclic dependencies //add a marker so we can detect cyclic dependencies
this.injector._markAsConstructing(key); this.injector._markAsConstructing(key);
var deps = this._resolveDependencies(key, binding); var deps = this.injector._resolveDependencies(key, binding, false);
return this._createInstance(key, binding, deps); return this._createInstance(key, binding, deps);
} }
_resolveDependencies(key:Key, binding:Binding) {
try {
var getDependency = d => this.injector._getByKey(d.key, d.asFuture, d.lazy);
return ListWrapper.map(binding.dependencies, getDependency);
} catch (e) {
this.injector._clear(key);
if (e instanceof ProviderError) e.addKey(key);
throw e;
}
}
_createInstance(key:Key, binding:Binding, deps:List) { _createInstance(key:Key, binding:Binding, deps:List) {
try { try {
var instance = binding.factory(deps); var instance = binding.factory(deps);
@ -193,7 +193,7 @@ class _AsyncInjectorStrategy {
//add a marker so we can detect cyclic dependencies //add a marker so we can detect cyclic dependencies
this.injector._markAsConstructing(key); this.injector._markAsConstructing(key);
var deps = this._resolveDependencies(key, binding); var deps = this.injector._resolveDependencies(key, binding, true);
var depsFuture = FutureWrapper.wait(deps); var depsFuture = FutureWrapper.wait(deps);
var future = FutureWrapper.catchError(depsFuture, (e) => this._errorHandler(key, e)). var future = FutureWrapper.catchError(depsFuture, (e) => this._errorHandler(key, e)).
@ -204,17 +204,6 @@ class _AsyncInjectorStrategy {
return future; return future;
} }
_resolveDependencies(key:Key, binding:Binding):List {
try {
var getDependency = d => this.injector._getByKey(d.key, true, d.lazy);
return ListWrapper.map(binding.dependencies, getDependency);
} catch (e) {
this.injector._clear(key);
if (e instanceof ProviderError) e.addKey(key);
throw e;
}
}
_errorHandler(key:Key, e):Future { _errorHandler(key:Key, e):Future {
if (e instanceof ProviderError) e.addKey(key); if (e instanceof ProviderError) e.addKey(key);
return FutureWrapper.error(e); return FutureWrapper.error(e);