From 187c4aa33ce95098fe0fbda362976cbdf503c6a8 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 7 Oct 2014 10:17:51 -0400 Subject: [PATCH] refactor(injector): uses one instance of reflector instead of creating a new instance every time --- modules/di/src/binding.js | 11 +++++------ modules/di/src/reflector.dart | 4 +++- modules/di/src/reflector.es6 | 13 ++++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/di/src/binding.js b/modules/di/src/binding.js index c07f6cde73..04b1050247 100644 --- a/modules/di/src/binding.js +++ b/modules/di/src/binding.js @@ -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 ); diff --git a/modules/di/src/reflector.dart b/modules/di/src/reflector.dart index 0de5ba57b1..c0247e3257 100644 --- a/modules/di/src/reflector.dart +++ b/modules/di/src/reflector.dart @@ -48,4 +48,6 @@ class Reflector { } }, growable:false); } -} \ No newline at end of file +} + +final Reflector reflector = new Reflector(); diff --git a/modules/di/src/reflector.es6 b/modules/di/src/reflector.es6 index ec6db41bc9..1e9675cbfe 100644 --- a/modules/di/src/reflector.es6 +++ b/modules/di/src/reflector.es6 @@ -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); @@ -47,4 +48,6 @@ export class Reflector { _createDependency(token, asFuture, lazy):Dependency { return new Dependency(Key.get(token), asFuture, lazy); } -} \ No newline at end of file +} + +export var reflector:Reflector = new Reflector(); \ No newline at end of file