refactor(injector): change reflector to collect the resolving path only when an error occurs
This commit is contained in:
@ -1,23 +1,27 @@
|
||||
import {ListWrapper, List} from 'facade/collection';
|
||||
import {stringify} from 'facade/lang';
|
||||
import {Key} from './key';
|
||||
|
||||
function constructResolvingPath(keys: List) {
|
||||
if (keys.length > 1) {
|
||||
var tokenStrs = ListWrapper.map(keys, (k) => stringify(k.token));
|
||||
var reversed = ListWrapper.reversed(keys);
|
||||
var tokenStrs = ListWrapper.map(reversed, (k) => stringify(k.token));
|
||||
return " (" + tokenStrs.join(' -> ') + ")";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export class NoProviderError extends Error {
|
||||
constructor(keys:List){
|
||||
this.message = this._constructResolvingMessage(keys);
|
||||
export class ProviderError extends Error {
|
||||
constructor(key:Key, constructResolvingMessage:Function){
|
||||
this.keys = [key];
|
||||
this.constructResolvingMessage = constructResolvingMessage;
|
||||
this.message = this.constructResolvingMessage(this.keys);
|
||||
}
|
||||
|
||||
_constructResolvingMessage(keys:List) {
|
||||
var last = stringify(ListWrapper.last(keys).token);
|
||||
return `No provider for ${last}!${constructResolvingPath(keys)}`;
|
||||
addKey(key: Key) {
|
||||
ListWrapper.push(this.keys, key);
|
||||
this.message = this.constructResolvingMessage(this.keys);
|
||||
}
|
||||
|
||||
toString() {
|
||||
@ -25,19 +29,22 @@ export class NoProviderError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class AsyncProviderError extends Error {
|
||||
constructor(keys:List){
|
||||
this.message = this._constructResolvingMessage(keys);
|
||||
export class NoProviderError extends ProviderError {
|
||||
constructor(key:Key){
|
||||
super(key, function(keys:List) {
|
||||
var first = stringify(ListWrapper.first(keys).token);
|
||||
return `No provider for ${first}!${constructResolvingPath(keys)}`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_constructResolvingMessage(keys:List) {
|
||||
var last = stringify(ListWrapper.last(keys).token);
|
||||
return `Cannot instantiate ${last} synchronously. ` +
|
||||
`It is provided as a future!${constructResolvingPath(keys)}`;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.message;
|
||||
export class AsyncProviderError extends ProviderError {
|
||||
constructor(key:Key){
|
||||
super(key, function(keys:List) {
|
||||
var first = stringify(ListWrapper.first(keys).token);
|
||||
return `Cannot instantiate ${first} synchronously. ` +
|
||||
`It is provided as a future!${constructResolvingPath(keys)}`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import {Map, List, MapWrapper, ListWrapper} from 'facade/collection';
|
||||
import {Binding, BindingBuilder, bind} from './binding';
|
||||
import {NoProviderError, InvalidBindingError, AsyncProviderError} from './exceptions';
|
||||
import {ProviderError, NoProviderError, InvalidBindingError, AsyncProviderError} from './exceptions';
|
||||
import {Type, isPresent, isBlank} from 'facade/lang';
|
||||
import {Future, FutureWrapper} from 'facade/async';
|
||||
import {Key} from './key';
|
||||
@ -32,19 +32,15 @@ export class Injector {
|
||||
}
|
||||
|
||||
getByKey(key:Key) {
|
||||
return this._getByKey(key, [], false);
|
||||
return this._getByKey(key, false);
|
||||
}
|
||||
|
||||
asyncGetByKey(key:Key) {
|
||||
return this._getByKey(key, [], true);
|
||||
return this._getByKey(key, true);
|
||||
}
|
||||
|
||||
_getByKey(key:Key, resolving:List, async) {
|
||||
_getByKey(key:Key, async) {
|
||||
var keyId = key.id;
|
||||
//TODO: vsavkin: use LinkedList to remove clone
|
||||
resolving = ListWrapper.clone(resolving)
|
||||
ListWrapper.push(resolving, key);
|
||||
|
||||
if (key.token === Injector) return this._injector(async);
|
||||
|
||||
var instance = this._get(this._instances, keyId);
|
||||
@ -53,14 +49,14 @@ export class Injector {
|
||||
var binding = this._get(this._bindings, keyId);
|
||||
|
||||
if (isPresent(binding)) {
|
||||
return this._instantiate(key, binding, resolving, async);
|
||||
return this._instantiate(key, binding, async);
|
||||
}
|
||||
|
||||
if (isPresent(this._parent)) {
|
||||
return this._parent._getByKey(key, resolving, async);
|
||||
return this._parent._getByKey(key, async);
|
||||
}
|
||||
|
||||
throw new NoProviderError(resolving);
|
||||
throw new NoProviderError(key);
|
||||
}
|
||||
|
||||
createChild(bindings:List):Injector {
|
||||
@ -78,31 +74,37 @@ export class Injector {
|
||||
return ListWrapper.get(list, index);
|
||||
}
|
||||
|
||||
_instantiate(key:Key, binding:Binding, resolving:List, async) {
|
||||
_instantiate(key:Key, binding:Binding, async) {
|
||||
if (binding.async && !async) {
|
||||
throw new AsyncProviderError(resolving);
|
||||
throw new AsyncProviderError(key);
|
||||
}
|
||||
|
||||
if (async) {
|
||||
return this._instantiateAsync(key, binding, resolving, async);
|
||||
return this._instantiateAsync(key, binding, async);
|
||||
} else {
|
||||
return this._instantiateSync(key, binding, resolving, async);
|
||||
return this._instantiateSync(key, binding, async);
|
||||
}
|
||||
}
|
||||
|
||||
_instantiateSync(key:Key, binding:Binding, resolving:List, async) {
|
||||
var deps = ListWrapper.map(binding.dependencies, d => this._getByKey(d, resolving, false));
|
||||
var instance = binding.factory(deps);
|
||||
ListWrapper.set(this._instances, key.id, instance);
|
||||
if (!binding.async && async) {
|
||||
return FutureWrapper.value(instance);
|
||||
_instantiateSync(key:Key, binding:Binding, async) {
|
||||
try {
|
||||
var deps = ListWrapper.map(binding.dependencies, d => this._getByKey(d, false));
|
||||
var instance = binding.factory(deps);
|
||||
ListWrapper.set(this._instances, key.id, instance);
|
||||
if (!binding.async && async) {
|
||||
return FutureWrapper.value(instance);
|
||||
}
|
||||
return instance;
|
||||
|
||||
} catch (e) {
|
||||
if (e instanceof ProviderError) e.addKey(key);
|
||||
throw e;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
_instantiateAsync(key:Key, binding:Binding, resolving:List, async):Future {
|
||||
_instantiateAsync(key:Key, binding:Binding, async):Future {
|
||||
var instances = this._createInstances();
|
||||
var futures = ListWrapper.map(binding.dependencies, d => this._getByKey(d, resolving, true));
|
||||
var futures = ListWrapper.map(binding.dependencies, d => this._getByKey(d, true));
|
||||
return FutureWrapper.wait(futures).
|
||||
then(binding.factory).
|
||||
then(function(instance) {
|
||||
|
Reference in New Issue
Block a user