From 06a221671c3b5a1ee4de65051e1373f19c78aaa4 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 9 Oct 2014 11:08:00 -0400 Subject: [PATCH] refactor(injector): cleanup --- modules/di/src/injector.js | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/modules/di/src/injector.js b/modules/di/src/injector.js index 1f7dcba372..52111edd58 100644 --- a/modules/di/src/injector.js +++ b/modules/di/src/injector.js @@ -81,6 +81,17 @@ export class Injector { 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) { if (this._instances.length <= key.id) return null; return ListWrapper.get(this._instances, key.id); @@ -135,21 +146,10 @@ class _SyncInjectorStrategy { //add a marker so we can detect cyclic dependencies this.injector._markAsConstructing(key); - var deps = this._resolveDependencies(key, binding); + var deps = this.injector._resolveDependencies(key, binding, false); 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) { try { var instance = binding.factory(deps); @@ -193,7 +193,7 @@ class _AsyncInjectorStrategy { //add a marker so we can detect cyclic dependencies this.injector._markAsConstructing(key); - var deps = this._resolveDependencies(key, binding); + var deps = this.injector._resolveDependencies(key, binding, true); var depsFuture = FutureWrapper.wait(deps); var future = FutureWrapper.catchError(depsFuture, (e) => this._errorHandler(key, e)). @@ -204,17 +204,6 @@ class _AsyncInjectorStrategy { 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 { if (e instanceof ProviderError) e.addKey(key); return FutureWrapper.error(e);