From 3f3fb7017e64aa237edad58447d464adc2cfeab9 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 9 Oct 2014 12:18:35 -0400 Subject: [PATCH] refactor(injector): implement support for nested bindings --- modules/di/src/injector.js | 8 +++++--- modules/di/test/di/injector_spec.js | 9 +++++++++ modules/facade/src/collection.es6 | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modules/di/src/injector.js b/modules/di/src/injector.js index 52111edd58..4698e92f86 100644 --- a/modules/di/src/injector.js +++ b/modules/di/src/injector.js @@ -20,7 +20,7 @@ function _isWaiting(obj):bool { export class Injector { constructor(bindings:List) { - var flatten = _flattenBindings(bindings); + var flatten = _flattenBindings(bindings, MapWrapper.create()); this._bindings = this._createListOfBindings(flatten); this._instances = this._createInstances(); this._parent = null; //TODO: vsavkin make a parameter @@ -227,8 +227,7 @@ class _AsyncInjectorStrategy { } -function _flattenBindings(bindings:List) { - var res = {}; +function _flattenBindings(bindings:List, res:Map) { ListWrapper.forEach(bindings, function (b) { if (b instanceof Binding) { MapWrapper.set(res, b.key.id, b); @@ -237,6 +236,9 @@ function _flattenBindings(bindings:List) { var s = bind(b).toClass(b); MapWrapper.set(res, s.key.id, s); + } else if (b instanceof List) { + _flattenBindings(b, res); + } else if (b instanceof BindingBuilder) { throw new InvalidBindingError(b.token); diff --git a/modules/di/test/di/injector_spec.js b/modules/di/test/di/injector_spec.js index 72a92c4b9d..da07617e55 100644 --- a/modules/di/test/di/injector_spec.js +++ b/modules/di/test/di/injector_spec.js @@ -133,6 +133,15 @@ export function main() { expect(car.engine).toBeAnInstanceOf(Engine); }); + it("should flatten passed-in bindings", function () { + var injector = new Injector([ + [[Engine, Car]] + ]); + + var car = injector.get(Car); + expect(car).toBeAnInstanceOf(Car); + }); + it('should use non-type tokens', function () { var injector = new Injector([ bind('token').toValue('value') diff --git a/modules/facade/src/collection.es6 b/modules/facade/src/collection.es6 index 67659bcee3..d1dcdd4cac 100644 --- a/modules/facade/src/collection.es6 +++ b/modules/facade/src/collection.es6 @@ -3,7 +3,7 @@ export var Map = window.Map; export var Set = window.Set; export class MapWrapper { - static create():HashMap { return new HashMap(); } + static create():Map { return new Map(); } static get(m, k) { return m[k]; } static set(m, k, v) { m[k] = v; } static contains(m, k) { return m[k] != undefined; }