From 3667854a8f7a9f84c56c2e69aad0a804a0b52739 Mon Sep 17 00:00:00 2001 From: Yegor Jbanov Date: Mon, 13 Apr 2015 14:29:32 -0700 Subject: [PATCH] refactor(di): move all binding resolution logic into injector.js --- modules/angular2/src/di/binding.js | 25 +----------------------- modules/angular2/src/di/injector.js | 30 ++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/modules/angular2/src/di/binding.js b/modules/angular2/src/di/binding.js index 2f4f37ef3a..10a284cf73 100644 --- a/modules/angular2/src/di/binding.js +++ b/modules/angular2/src/di/binding.js @@ -3,7 +3,7 @@ import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection'; import {reflector} from 'angular2/src/reflection/reflection'; import {Key} from './key'; import {Inject, InjectLazy, InjectPromise, Optional, DependencyAnnotation} from './annotations'; -import {NoAnnotationError, InvalidBindingError} from './exceptions'; +import {NoAnnotationError} from './exceptions'; export class Dependency { key:Key; @@ -88,29 +88,6 @@ export class Binding { isAsync ); } - - static resolveAll(bindings:List): List { - var resolvedList = ListWrapper.createFixedSize(bindings.length); - for (var i = 0; i < bindings.length; i++) { - var unresolved = bindings[i]; - var resolved; - if (unresolved instanceof ResolvedBinding) { - resolved = unresolved; // ha-ha! I'm easily amused - } else if (unresolved instanceof Type) { - resolved = bind(unresolved).toClass(unresolved).resolve(); - } else if (unresolved instanceof Binding) { - resolved = unresolved.resolve(); - } else if (unresolved instanceof List) { - resolved = Binding.resolveAll(unresolved); - } else if (unresolved instanceof BindingBuilder) { - throw new InvalidBindingError(unresolved.token); - } else { - throw new InvalidBindingError(unresolved); - } - resolvedList[i] = resolved; - } - return resolvedList; - } } /// Dependency binding with resolved keys and dependencies. diff --git a/modules/angular2/src/di/injector.js b/modules/angular2/src/di/injector.js index e9f6c3bb5f..b5c00fed23 100644 --- a/modules/angular2/src/di/injector.js +++ b/modules/angular2/src/di/injector.js @@ -1,7 +1,7 @@ import {Map, List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection'; import {ResolvedBinding, Binding, BindingBuilder, bind} from './binding'; -import {ProviderError, NoProviderError, - AsyncBindingError, CyclicDependencyError, InstantiationError} from './exceptions'; +import {ProviderError, NoProviderError, AsyncBindingError, CyclicDependencyError, + InstantiationError, InvalidBindingError} from './exceptions'; import {FunctionWrapper, Type, isPresent, isBlank} from 'angular2/src/facade/lang'; import {Promise, PromiseWrapper} from 'angular2/src/facade/async'; import {Key} from './key'; @@ -38,7 +38,8 @@ export class Injector { * [fromResolvedBindings] and [createChildFromResolved]. */ static resolve(bindings:List/**/):List { - var flatten = _flattenBindings(Binding.resolveAll(bindings), MapWrapper.create()); + var resolvedBindings = _resolveBindings(bindings); + var flatten = _flattenBindings(resolvedBindings, MapWrapper.create()); return _createListOfBindings(flatten); } @@ -272,6 +273,29 @@ class _AsyncInjectorStrategy { } } +function _resolveBindings(bindings:List): List { + var resolvedList = ListWrapper.createFixedSize(bindings.length); + for (var i = 0; i < bindings.length; i++) { + var unresolved = bindings[i]; + var resolved; + if (unresolved instanceof ResolvedBinding) { + resolved = unresolved; // ha-ha! I'm easily amused + } else if (unresolved instanceof Type) { + resolved = bind(unresolved).toClass(unresolved).resolve(); + } else if (unresolved instanceof Binding) { + resolved = unresolved.resolve(); + } else if (unresolved instanceof List) { + resolved = _resolveBindings(unresolved); + } else if (unresolved instanceof BindingBuilder) { + throw new InvalidBindingError(unresolved.token); + } else { + throw new InvalidBindingError(unresolved); + } + resolvedList[i] = resolved; + } + return resolvedList; +} + function _createListOfBindings(flattenedBindings):List { var bindings = ListWrapper.createFixedSize(Key.numberOfKeys + 1); MapWrapper.forEach(flattenedBindings, (v, keyId) => bindings[keyId] = v);