refactor(di): move all binding resolution logic into injector.js

This commit is contained in:
Yegor Jbanov 2015-04-13 14:29:32 -07:00
parent c5c1c9e38e
commit 3667854a8f
2 changed files with 28 additions and 27 deletions

View File

@ -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.

View File

@ -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/*<ResolvedBinding|Binding|Type|List>*/):List<ResolvedBinding> {
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);