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

View File

@ -48,4 +48,6 @@ class Reflector {
} }
}, growable:false); }, growable:false);
} }
} }
final Reflector reflector = new Reflector();

View File

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