refactor(di): move all binding resolution logic into injector.js
This commit is contained in:
parent
c5c1c9e38e
commit
3667854a8f
25
modules/angular2/src/di/binding.js
vendored
25
modules/angular2/src/di/binding.js
vendored
@ -3,7 +3,7 @@ import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
|||||||
import {reflector} from 'angular2/src/reflection/reflection';
|
import {reflector} from 'angular2/src/reflection/reflection';
|
||||||
import {Key} from './key';
|
import {Key} from './key';
|
||||||
import {Inject, InjectLazy, InjectPromise, Optional, DependencyAnnotation} from './annotations';
|
import {Inject, InjectLazy, InjectPromise, Optional, DependencyAnnotation} from './annotations';
|
||||||
import {NoAnnotationError, InvalidBindingError} from './exceptions';
|
import {NoAnnotationError} from './exceptions';
|
||||||
|
|
||||||
export class Dependency {
|
export class Dependency {
|
||||||
key:Key;
|
key:Key;
|
||||||
@ -88,29 +88,6 @@ export class Binding {
|
|||||||
isAsync
|
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.
|
/// Dependency binding with resolved keys and dependencies.
|
||||||
|
30
modules/angular2/src/di/injector.js
vendored
30
modules/angular2/src/di/injector.js
vendored
@ -1,7 +1,7 @@
|
|||||||
import {Map, List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
import {Map, List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
||||||
import {ResolvedBinding, Binding, BindingBuilder, bind} from './binding';
|
import {ResolvedBinding, Binding, BindingBuilder, bind} from './binding';
|
||||||
import {ProviderError, NoProviderError,
|
import {ProviderError, NoProviderError, AsyncBindingError, CyclicDependencyError,
|
||||||
AsyncBindingError, CyclicDependencyError, InstantiationError} from './exceptions';
|
InstantiationError, InvalidBindingError} from './exceptions';
|
||||||
import {FunctionWrapper, Type, isPresent, isBlank} from 'angular2/src/facade/lang';
|
import {FunctionWrapper, Type, isPresent, isBlank} from 'angular2/src/facade/lang';
|
||||||
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
||||||
import {Key} from './key';
|
import {Key} from './key';
|
||||||
@ -38,7 +38,8 @@ export class Injector {
|
|||||||
* [fromResolvedBindings] and [createChildFromResolved].
|
* [fromResolvedBindings] and [createChildFromResolved].
|
||||||
*/
|
*/
|
||||||
static resolve(bindings:List/*<ResolvedBinding|Binding|Type|List>*/):List<ResolvedBinding> {
|
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);
|
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 {
|
function _createListOfBindings(flattenedBindings):List {
|
||||||
var bindings = ListWrapper.createFixedSize(Key.numberOfKeys + 1);
|
var bindings = ListWrapper.createFixedSize(Key.numberOfKeys + 1);
|
||||||
MapWrapper.forEach(flattenedBindings, (v, keyId) => bindings[keyId] = v);
|
MapWrapper.forEach(flattenedBindings, (v, keyId) => bindings[keyId] = v);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user