refactor(injector): uses one instance of reflector instead of creating a new instance every time

This commit is contained in:
vsavkin 2014-10-07 10:17:51 -04:00
parent ab4f86a0cb
commit 187c4aa33c
3 changed files with 16 additions and 12 deletions

View File

@ -1,6 +1,6 @@
import {Type, bool} from 'facade/lang';
import {List, MapWrapper, ListWrapper} from 'facade/collection';
import {Reflector} from './reflector';
import {reflector} from './reflector';
import {Key, Dependency} from './key';
export class Binding {
@ -19,14 +19,13 @@ export function bind(token):BindingBuilder {
export class BindingBuilder {
constructor(token) {
this.token = token;
this.reflector = new Reflector();
}
toClass(type:Type):Binding {
return new Binding(
Key.get(this.token),
this.reflector.factoryFor(type),
this.reflector.dependencies(type),
reflector.factoryFor(type),
reflector.dependencies(type),
false
);
}
@ -43,7 +42,7 @@ export class BindingBuilder {
toFactory(dependencies:List, factoryFunction:Function):Binding {
return new Binding(
Key.get(this.token),
this.reflector.convertToFactory(factoryFunction),
reflector.convertToFactory(factoryFunction),
this._constructDependencies(dependencies),
false
);
@ -52,7 +51,7 @@ export class BindingBuilder {
toAsyncFactory(dependencies:List, factoryFunction:Function):Binding {
return new Binding(
Key.get(this.token),
this.reflector.convertToFactory(factoryFunction),
reflector.convertToFactory(factoryFunction),
this._constructDependencies(dependencies),
true
);

View File

@ -49,3 +49,5 @@ class Reflector {
}, growable:false);
}
}
final Reflector reflector = new Reflector();

View File

@ -1,18 +1,19 @@
import {Type, isPresent} from 'facade/lang';
import {List} from 'facade/collection';
import {Inject, InjectFuture, InjectLazy} from './annotations';
import {Dependency, Key} from './key';
import {NoAnnotationError} from './exceptions';
export class Reflector {
factoryFor(type:Type) {
class Reflector {
factoryFor(type:Type):Function {
return (args) => new type(...args);
}
convertToFactory(factoryFunction:Function) {
convertToFactory(factoryFunction:Function):Function {
return (args) => factoryFunction(...args);
}
dependencies(type:Type) {
dependencies(type:Type):List {
var p = type.parameters;
if (p == undefined && type.length == 0) return [];
if (p == undefined) throw new NoAnnotationError(type);
@ -48,3 +49,5 @@ export class Reflector {
return new Dependency(Key.get(token), asFuture, lazy);
}
}
export var reflector:Reflector = new Reflector();