style(reflector): formatting

This commit is contained in:
vsavkin 2014-10-07 10:34:07 -04:00
parent 187c4aa33c
commit 7d566adea0
8 changed files with 78 additions and 65 deletions

View File

@ -2,21 +2,21 @@ import {CONST} from "facade/lang";
export class Inject { export class Inject {
@CONST() @CONST()
constructor(token){ constructor(token) {
this.token = token; this.token = token;
} }
} }
export class InjectFuture { export class InjectFuture {
@CONST() @CONST()
constructor(token){ constructor(token) {
this.token = token; this.token = token;
} }
} }
export class InjectLazy { export class InjectLazy {
@CONST() @CONST()
constructor(token){ constructor(token) {
this.token = token; this.token = token;
} }
} }

View File

@ -2,7 +2,7 @@ import {ListWrapper, List} from 'facade/collection';
import {stringify} from 'facade/lang'; import {stringify} from 'facade/lang';
import {Key} from './key'; import {Key} from './key';
function constructResolvingPath(keys: List) { function constructResolvingPath(keys:List) {
if (keys.length > 1) { if (keys.length > 1) {
var reversed = ListWrapper.reversed(keys); var reversed = ListWrapper.reversed(keys);
var tokenStrs = ListWrapper.map(reversed, (k) => stringify(k.token)); var tokenStrs = ListWrapper.map(reversed, (k) => stringify(k.token));
@ -13,13 +13,13 @@ function constructResolvingPath(keys: List) {
} }
export class ProviderError extends Error { export class ProviderError extends Error {
constructor(key:Key, constructResolvingMessage:Function){ constructor(key:Key, constructResolvingMessage:Function) {
this.keys = [key]; this.keys = [key];
this.constructResolvingMessage = constructResolvingMessage; this.constructResolvingMessage = constructResolvingMessage;
this.message = this.constructResolvingMessage(this.keys); this.message = this.constructResolvingMessage(this.keys);
} }
addKey(key: Key) { addKey(key:Key) {
ListWrapper.push(this.keys, key); ListWrapper.push(this.keys, key);
this.message = this.constructResolvingMessage(this.keys); this.message = this.constructResolvingMessage(this.keys);
} }
@ -30,8 +30,8 @@ export class ProviderError extends Error {
} }
export class NoProviderError extends ProviderError { export class NoProviderError extends ProviderError {
constructor(key:Key){ constructor(key:Key) {
super(key, function(keys:List) { super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token); var first = stringify(ListWrapper.first(keys).token);
return `No provider for ${first}!${constructResolvingPath(keys)}`; return `No provider for ${first}!${constructResolvingPath(keys)}`;
}); });
@ -39,8 +39,8 @@ export class NoProviderError extends ProviderError {
} }
export class AsyncBindingError extends ProviderError { export class AsyncBindingError extends ProviderError {
constructor(key:Key){ constructor(key:Key) {
super(key, function(keys:List) { super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token); var first = stringify(ListWrapper.first(keys).token);
return `Cannot instantiate ${first} synchronously. ` + return `Cannot instantiate ${first} synchronously. ` +
`It is provided as a future!${constructResolvingPath(keys)}`; `It is provided as a future!${constructResolvingPath(keys)}`;
@ -49,25 +49,25 @@ export class AsyncBindingError extends ProviderError {
} }
export class CyclicDependencyError extends ProviderError { export class CyclicDependencyError extends ProviderError {
constructor(key:Key){ constructor(key:Key) {
super(key, function(keys:List) { super(key, function (keys:List) {
return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`; return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`;
}); });
} }
} }
export class InstantiationError extends ProviderError { export class InstantiationError extends ProviderError {
constructor(originalException, key:Key){ constructor(originalException, key:Key) {
super(key, function(keys:List) { super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token); var first = stringify(ListWrapper.first(keys).token);
return `Error during instantiation of ${first}!${constructResolvingPath(keys)}.`+ return `Error during instantiation of ${first}!${constructResolvingPath(keys)}.` +
` ORIGINAL ERROR: ${originalException}`; ` ORIGINAL ERROR: ${originalException}`;
}); });
} }
} }
export class InvalidBindingError extends Error { export class InvalidBindingError extends Error {
constructor(binding){ constructor(binding) {
this.message = `Invalid binding ${binding}`; this.message = `Invalid binding ${binding}`;
} }
@ -77,7 +77,7 @@ export class InvalidBindingError extends Error {
} }
export class NoAnnotationError extends Error { export class NoAnnotationError extends Error {
constructor(type){ constructor(type) {
this.message = `Cannot resolve all parameters for ${stringify(type)}`; this.message = `Cannot resolve all parameters for ${stringify(type)}`;
} }

View File

@ -81,16 +81,16 @@ export class Injector {
throw new NoProviderError(key); throw new NoProviderError(key);
} }
_getInstance(key:Key){ _getInstance(key:Key) {
if (this._instances.length <= key.id) return null; if (this._instances.length <= key.id) return null;
return ListWrapper.get(this._instances, key.id); return ListWrapper.get(this._instances, key.id);
} }
_setInstance(key:Key, obj){ _setInstance(key:Key, obj) {
ListWrapper.set(this._instances, key.id, obj); ListWrapper.set(this._instances, key.id, obj);
} }
_getBinding(key:Key){ _getBinding(key:Key) {
if (this._bindings.length <= key.id) return null; if (this._bindings.length <= key.id) return null;
return ListWrapper.get(this._bindings, key.id); return ListWrapper.get(this._bindings, key.id);
} }
@ -154,7 +154,6 @@ class _SyncInjectorStrategy {
} }
class _AsyncInjectorStrategy { class _AsyncInjectorStrategy {
constructor(injector:Injector) { constructor(injector:Injector) {
this.injector = injector; this.injector = injector;
@ -212,13 +211,13 @@ class _AsyncInjectorStrategy {
return FutureWrapper.error(e); return FutureWrapper.error(e);
} }
_findOrCreate(key:Key, binding: Binding, deps:List) { _findOrCreate(key:Key, binding:Binding, deps:List) {
try { try {
var instance = this.injector._getInstance(key); var instance = this.injector._getInstance(key);
if (!_isWaiting(instance)) return instance; if (!_isWaiting(instance)) return instance;
return binding.factory(deps); return binding.factory(deps);
} catch (e) { } catch (e) {
throw new InstantiationError(e, key); throw new InstantiationError(e, key);
} }
} }
@ -229,10 +228,9 @@ class _AsyncInjectorStrategy {
} }
function _flattenBindings(bindings:List) { function _flattenBindings(bindings:List) {
var res = {}; var res = {};
ListWrapper.forEach(bindings, function (b){ ListWrapper.forEach(bindings, function (b) {
if (b instanceof Binding) { if (b instanceof Binding) {
MapWrapper.set(res, b.key.id, b); MapWrapper.set(res, b.key.id, b);

View File

@ -9,7 +9,7 @@ var _id:int = 0;
@FIELD('final asFuture:bool') @FIELD('final asFuture:bool')
@FIELD('final lazy:bool') @FIELD('final lazy:bool')
export class Dependency { export class Dependency {
constructor(key:Key, asFuture:bool, lazy:bool){ constructor(key:Key, asFuture:bool, lazy:bool) {
this.key = key; this.key = key;
this.asFuture = asFuture; this.asFuture = asFuture;
this.lazy = lazy; this.lazy = lazy;

View File

@ -37,12 +37,16 @@ class Reflector {
if (inject != null) { if (inject != null) {
return new Dependency(Key.get(inject.token), false, false); return new Dependency(Key.get(inject.token), false, false);
} else if (injectFuture != null) { } else if (injectFuture != null) {
return new Dependency(Key.get(injectFuture.token), true, false); return new Dependency(Key.get(injectFuture.token), true, false);
} else if (injectLazy != null) { } else if (injectLazy != null) {
return new Dependency(Key.get(injectLazy.token), false, true); return new Dependency(Key.get(injectLazy.token), false, true);
} else if (p.type.qualifiedName != #dynamic) { } else if (p.type.qualifiedName != #dynamic) {
return new Dependency(Key.get(p.type.reflectedType), false, false); return new Dependency(Key.get(p.type.reflectedType), false, false);
} else { } else {
throw new NoAnnotationError(type); throw new NoAnnotationError(type);
} }

View File

@ -2,13 +2,15 @@ import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/te
import {Injector, Inject, InjectFuture, bind, Key} from 'di/di'; import {Injector, Inject, InjectFuture, bind, Key} from 'di/di';
import {Future, FutureWrapper} from 'facade/async'; import {Future, FutureWrapper} from 'facade/async';
class UserList {} class UserList {
}
function fetchUsers() { function fetchUsers() {
return FutureWrapper.value(new UserList()); return FutureWrapper.value(new UserList());
} }
class SynchronousUserList {} class SynchronousUserList {
}
class UserController { class UserController {
constructor(list:UserList) { constructor(list:UserList) {
@ -22,11 +24,11 @@ class AsyncUserController {
} }
} }
export function main () { export function main() {
describe("async injection", function () { describe("async injection", function () {
describe("asyncGet", function () { describe("asyncGet", function () {
it('should return a future', function() { it('should return a future', function () {
var injector = new Injector([ var injector = new Injector([
bind(UserList).toAsyncFactory([], fetchUsers) bind(UserList).toAsyncFactory([], fetchUsers)
]); ]);
@ -34,7 +36,7 @@ export function main () {
expect(p).toBeFuture(); expect(p).toBeFuture();
}); });
it('should return a future if the binding is sync', function() { it('should return a future if the binding is sync', function () {
var injector = new Injector([ var injector = new Injector([
SynchronousUserList SynchronousUserList
]); ]);
@ -42,23 +44,23 @@ export function main () {
expect(p).toBeFuture(); expect(p).toBeFuture();
}); });
it('should return the injector', function(done) { it('should return the injector', function (done) {
var injector = new Injector([]); var injector = new Injector([]);
var p = injector.asyncGet(Injector); var p = injector.asyncGet(Injector);
p.then(function(injector) { p.then(function (injector) {
expect(injector).toBe(injector); expect(injector).toBe(injector);
done(); done();
}); });
}); });
it('should return a future when instantiating a sync binding ' + it('should return a future when instantiating a sync binding ' +
'with an async dependency', function(done) { 'with an async dependency', function (done) {
var injector = new Injector([ var injector = new Injector([
bind(UserList).toAsyncFactory([], fetchUsers), bind(UserList).toAsyncFactory([], fetchUsers),
UserController UserController
]); ]);
injector.asyncGet(UserController).then(function(userController) { injector.asyncGet(UserController).then(function (userController) {
expect(userController).toBeAnInstanceOf(UserController); expect(userController).toBeAnInstanceOf(UserController);
expect(userController.list).toBeAnInstanceOf(UserList); expect(userController.list).toBeAnInstanceOf(UserList);
done(); done();
@ -104,10 +106,12 @@ export function main () {
}); });
}); });
it('should show the full path when error happens in a constructor', function(done) { it('should show the full path when error happens in a constructor', function (done) {
var injector = new Injector([ var injector = new Injector([
UserController, UserController,
bind(UserList).toAsyncFactory([], function(){throw "Broken UserList";}) bind(UserList).toAsyncFactory([], function () {
throw "Broken UserList";
})
]); ]);
var future = injector.asyncGet(UserController); var future = injector.asyncGet(UserController);
@ -119,7 +123,7 @@ export function main () {
}); });
describe("get", function () { describe("get", function () {
it('should throw when instantiating an async binding', function() { it('should throw when instantiating an async binding', function () {
var injector = new Injector([ var injector = new Injector([
bind(UserList).toAsyncFactory([], fetchUsers) bind(UserList).toAsyncFactory([], fetchUsers)
]); ]);
@ -128,7 +132,7 @@ export function main () {
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a future!'); .toThrowError('Cannot instantiate UserList synchronously. It is provided as a future!');
}); });
it('should throw when instantiating a sync binding with an dependency', function() { it('should throw when instantiating a sync binding with an dependency', function () {
var injector = new Injector([ var injector = new Injector([
bind(UserList).toAsyncFactory([], fetchUsers), bind(UserList).toAsyncFactory([], fetchUsers),
UserController UserController
@ -138,7 +142,7 @@ export function main () {
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a future! (UserController -> UserList)'); .toThrowError('Cannot instantiate UserList synchronously. It is provided as a future! (UserController -> UserList)');
}); });
it('should resolve synchronously when an async dependency requested as a future', function() { it('should resolve synchronously when an async dependency requested as a future', function () {
var injector = new Injector([ var injector = new Injector([
bind(UserList).toAsyncFactory([], fetchUsers), bind(UserList).toAsyncFactory([], fetchUsers),
AsyncUserController AsyncUserController
@ -149,7 +153,7 @@ export function main () {
expect(controller.userList).toBeFuture(); expect(controller.userList).toBeFuture();
}); });
it('should wrap sync dependencies into futures if required', function() { it('should wrap sync dependencies into futures if required', function () {
var injector = new Injector([ var injector = new Injector([
bind(UserList).toFactory([], () => new UserList()), bind(UserList).toFactory([], () => new UserList()),
AsyncUserController AsyncUserController

View File

@ -1,17 +1,24 @@
import {describe, ddescribe, it, iit, expect, beforeEach} from 'test_lib/test_lib'; import {describe, ddescribe, it, iit, expect, beforeEach} from 'test_lib/test_lib';
import {Injector, Inject, InjectLazy, bind} from 'di/di'; import {Injector, Inject, InjectLazy, bind} from 'di/di';
class Engine {} class Engine {
}
class BrokenEngine { class BrokenEngine {
constructor() { constructor() {
throw "Broken Engine"; throw "Broken Engine";
} }
} }
class DashboardSoftware {}
class Dashboard { class DashboardSoftware {
constructor(software: DashboardSoftware){} }
class Dashboard {
constructor(software: DashboardSoftware) {}
}
class TurboEngine extends Engine {
} }
class TurboEngine extends Engine{}
class Car { class Car {
constructor(engine:Engine) { constructor(engine:Engine) {
@ -45,23 +52,23 @@ class CarWithInject {
} }
class CyclicEngine { class CyclicEngine {
constructor(car:Car){} constructor(car:Car) {}
} }
class NoAnnotations { class NoAnnotations {
constructor(secretDependency){} constructor(secretDependency) {}
} }
export function main() { export function main() {
describe('injector', function() { describe('injector', function () {
it('should instantiate a class without dependencies', function() { it('should instantiate a class without dependencies', function () {
var injector = new Injector([Engine]); var injector = new Injector([Engine]);
var engine = injector.get(Engine); var engine = injector.get(Engine);
expect(engine).toBeAnInstanceOf(Engine); expect(engine).toBeAnInstanceOf(Engine);
}); });
it('should resolve dependencies based on type information', function() { it('should resolve dependencies based on type information', function () {
var injector = new Injector([Engine, Car]); var injector = new Injector([Engine, Car]);
var car = injector.get(Car); var car = injector.get(Car);
@ -69,7 +76,7 @@ export function main() {
expect(car.engine).toBeAnInstanceOf(Engine); expect(car.engine).toBeAnInstanceOf(Engine);
}); });
it('should resolve dependencies based on @Inject annotation', function() { it('should resolve dependencies based on @Inject annotation', function () {
var injector = new Injector([TurboEngine, Engine, CarWithInject]); var injector = new Injector([TurboEngine, Engine, CarWithInject]);
var car = injector.get(CarWithInject); var car = injector.get(CarWithInject);
@ -82,7 +89,7 @@ export function main() {
'Cannot resolve all parameters for NoAnnotations'); 'Cannot resolve all parameters for NoAnnotations');
}); });
it('should cache instances', function() { it('should cache instances', function () {
var injector = new Injector([Engine]); var injector = new Injector([Engine]);
var e1 = injector.get(Engine); var e1 = injector.get(Engine);
@ -91,7 +98,7 @@ export function main() {
expect(e1).toBe(e2); expect(e1).toBe(e2);
}); });
it('should bind to a value', function() { it('should bind to a value', function () {
var injector = new Injector([ var injector = new Injector([
bind(Engine).toValue("fake engine") bind(Engine).toValue("fake engine")
]); ]);
@ -100,7 +107,7 @@ export function main() {
expect(engine).toEqual("fake engine"); expect(engine).toEqual("fake engine");
}); });
it('should bind to a factory', function() { it('should bind to a factory', function () {
var injector = new Injector([ var injector = new Injector([
Engine, Engine,
bind(Car).toFactory([Engine], (e) => new SportsCar(e)) bind(Car).toFactory([Engine], (e) => new SportsCar(e))
@ -111,7 +118,7 @@ export function main() {
expect(car.engine).toBeAnInstanceOf(Engine); expect(car.engine).toBeAnInstanceOf(Engine);
}); });
it('should use non-type tokens', function() { it('should use non-type tokens', function () {
var injector = new Injector([ var injector = new Injector([
bind('token').toValue('value') bind('token').toValue('value')
]); ]);
@ -119,30 +126,30 @@ export function main() {
expect(injector.get('token')).toEqual('value'); expect(injector.get('token')).toEqual('value');
}); });
it('should throw when given invalid bindings', function() { it('should throw when given invalid bindings', function () {
expect(() => new Injector(["blah"])).toThrowError('Invalid binding blah'); expect(() => new Injector(["blah"])).toThrowError('Invalid binding blah');
expect(() => new Injector([bind("blah")])).toThrowError('Invalid binding blah'); expect(() => new Injector([bind("blah")])).toThrowError('Invalid binding blah');
}); });
it('should provide itself', function() { it('should provide itself', function () {
var parent = new Injector([]); var parent = new Injector([]);
var child = parent.createChild([]); var child = parent.createChild([]);
expect(child.get(Injector)).toBe(child); expect(child.get(Injector)).toBe(child);
}); });
it('should throw when no provider defined', function() { it('should throw when no provider defined', function () {
var injector = new Injector([]); var injector = new Injector([]);
expect(() => injector.get('NonExisting')).toThrowError('No provider for NonExisting!'); expect(() => injector.get('NonExisting')).toThrowError('No provider for NonExisting!');
}); });
it('should show the full path when no provider', function() { it('should show the full path when no provider', function () {
var injector = new Injector([CarWithDashboard, Engine, Dashboard]); var injector = new Injector([CarWithDashboard, Engine, Dashboard]);
expect(() => injector.get(CarWithDashboard)). expect(() => injector.get(CarWithDashboard)).
toThrowError('No provider for DashboardSoftware! (CarWithDashboard -> Dashboard -> DashboardSoftware)'); toThrowError('No provider for DashboardSoftware! (CarWithDashboard -> Dashboard -> DashboardSoftware)');
}); });
it('should throw when trying to instantiate a cyclic dependency', function() { it('should throw when trying to instantiate a cyclic dependency', function () {
var injector = new Injector([ var injector = new Injector([
Car, Car,
bind(Engine).toClass(CyclicEngine) bind(Engine).toClass(CyclicEngine)
@ -155,7 +162,7 @@ export function main() {
.toThrowError('Cannot instantiate cyclic dependency! (Car -> Engine -> Car)'); .toThrowError('Cannot instantiate cyclic dependency! (Car -> Engine -> Car)');
}); });
it('should show the full path when error happens in a constructor', function() { it('should show the full path when error happens in a constructor', function () {
var injector = new Injector([ var injector = new Injector([
Car, Car,
bind(Engine).toClass(BrokenEngine) bind(Engine).toClass(BrokenEngine)
@ -171,7 +178,7 @@ export function main() {
describe("child", function () { describe("child", function () {
it('should load instances from parent injector', function() { it('should load instances from parent injector', function () {
var parent = new Injector([Engine]); var parent = new Injector([Engine]);
var child = parent.createChild([]); var child = parent.createChild([]);
@ -181,7 +188,7 @@ export function main() {
expect(engineFromChild).toBe(engineFromParent); expect(engineFromChild).toBe(engineFromParent);
}); });
it('should create new instance in a child injector', function() { it('should create new instance in a child injector', function () {
var parent = new Injector([Engine]); var parent = new Injector([Engine]);
var child = parent.createChild([ var child = parent.createChild([
bind(Engine).toClass(TurboEngine) bind(Engine).toClass(TurboEngine)

View File

@ -1,7 +1,7 @@
import {describe, it, expect} from 'test_lib/test_lib'; import {describe, it, expect} from 'test_lib/test_lib';
import {Key} from 'di/di'; import {Key} from 'di/di';
export function main () { export function main() {
describe("key", function () { describe("key", function () {
it('should be equal to another key if type is the same', function () { it('should be equal to another key if type is the same', function () {
expect(Key.get('car')).toBe(Key.get('car')); expect(Key.get('car')).toBe(Key.get('car'));