style(reflector): formatting
This commit is contained in:
parent
187c4aa33c
commit
7d566adea0
|
@ -2,21 +2,21 @@ import {CONST} from "facade/lang";
|
|||
|
||||
export class Inject {
|
||||
@CONST()
|
||||
constructor(token){
|
||||
constructor(token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
|
||||
export class InjectFuture {
|
||||
@CONST()
|
||||
constructor(token){
|
||||
constructor(token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
|
||||
export class InjectLazy {
|
||||
@CONST()
|
||||
constructor(token){
|
||||
constructor(token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ import {ListWrapper, List} from 'facade/collection';
|
|||
import {stringify} from 'facade/lang';
|
||||
import {Key} from './key';
|
||||
|
||||
function constructResolvingPath(keys: List) {
|
||||
function constructResolvingPath(keys:List) {
|
||||
if (keys.length > 1) {
|
||||
var reversed = ListWrapper.reversed(keys);
|
||||
var tokenStrs = ListWrapper.map(reversed, (k) => stringify(k.token));
|
||||
|
@ -13,13 +13,13 @@ function constructResolvingPath(keys: List) {
|
|||
}
|
||||
|
||||
export class ProviderError extends Error {
|
||||
constructor(key:Key, constructResolvingMessage:Function){
|
||||
constructor(key:Key, constructResolvingMessage:Function) {
|
||||
this.keys = [key];
|
||||
this.constructResolvingMessage = constructResolvingMessage;
|
||||
this.message = this.constructResolvingMessage(this.keys);
|
||||
}
|
||||
|
||||
addKey(key: Key) {
|
||||
addKey(key:Key) {
|
||||
ListWrapper.push(this.keys, key);
|
||||
this.message = this.constructResolvingMessage(this.keys);
|
||||
}
|
||||
|
@ -30,8 +30,8 @@ export class ProviderError extends Error {
|
|||
}
|
||||
|
||||
export class NoProviderError extends ProviderError {
|
||||
constructor(key:Key){
|
||||
super(key, function(keys:List) {
|
||||
constructor(key:Key) {
|
||||
super(key, function (keys:List) {
|
||||
var first = stringify(ListWrapper.first(keys).token);
|
||||
return `No provider for ${first}!${constructResolvingPath(keys)}`;
|
||||
});
|
||||
|
@ -39,8 +39,8 @@ export class NoProviderError extends ProviderError {
|
|||
}
|
||||
|
||||
export class AsyncBindingError extends ProviderError {
|
||||
constructor(key:Key){
|
||||
super(key, function(keys:List) {
|
||||
constructor(key:Key) {
|
||||
super(key, function (keys:List) {
|
||||
var first = stringify(ListWrapper.first(keys).token);
|
||||
return `Cannot instantiate ${first} synchronously. ` +
|
||||
`It is provided as a future!${constructResolvingPath(keys)}`;
|
||||
|
@ -49,25 +49,25 @@ export class AsyncBindingError extends ProviderError {
|
|||
}
|
||||
|
||||
export class CyclicDependencyError extends ProviderError {
|
||||
constructor(key:Key){
|
||||
super(key, function(keys:List) {
|
||||
constructor(key:Key) {
|
||||
super(key, function (keys:List) {
|
||||
return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class InstantiationError extends ProviderError {
|
||||
constructor(originalException, key:Key){
|
||||
super(key, function(keys:List) {
|
||||
constructor(originalException, key:Key) {
|
||||
super(key, function (keys:List) {
|
||||
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}`;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidBindingError extends Error {
|
||||
constructor(binding){
|
||||
constructor(binding) {
|
||||
this.message = `Invalid binding ${binding}`;
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,7 @@ export class InvalidBindingError extends Error {
|
|||
}
|
||||
|
||||
export class NoAnnotationError extends Error {
|
||||
constructor(type){
|
||||
constructor(type) {
|
||||
this.message = `Cannot resolve all parameters for ${stringify(type)}`;
|
||||
}
|
||||
|
||||
|
|
|
@ -81,16 +81,16 @@ export class Injector {
|
|||
throw new NoProviderError(key);
|
||||
}
|
||||
|
||||
_getInstance(key:Key){
|
||||
_getInstance(key:Key) {
|
||||
if (this._instances.length <= key.id) return null;
|
||||
return ListWrapper.get(this._instances, key.id);
|
||||
}
|
||||
|
||||
_setInstance(key:Key, obj){
|
||||
_setInstance(key:Key, obj) {
|
||||
ListWrapper.set(this._instances, key.id, obj);
|
||||
}
|
||||
|
||||
_getBinding(key:Key){
|
||||
_getBinding(key:Key) {
|
||||
if (this._bindings.length <= key.id) return null;
|
||||
return ListWrapper.get(this._bindings, key.id);
|
||||
}
|
||||
|
@ -154,7 +154,6 @@ class _SyncInjectorStrategy {
|
|||
}
|
||||
|
||||
|
||||
|
||||
class _AsyncInjectorStrategy {
|
||||
constructor(injector:Injector) {
|
||||
this.injector = injector;
|
||||
|
@ -212,13 +211,13 @@ class _AsyncInjectorStrategy {
|
|||
return FutureWrapper.error(e);
|
||||
}
|
||||
|
||||
_findOrCreate(key:Key, binding: Binding, deps:List) {
|
||||
_findOrCreate(key:Key, binding:Binding, deps:List) {
|
||||
try {
|
||||
var instance = this.injector._getInstance(key);
|
||||
if (!_isWaiting(instance)) return instance;
|
||||
return binding.factory(deps);
|
||||
} catch (e) {
|
||||
throw new InstantiationError(e, key);
|
||||
throw new InstantiationError(e, key);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -229,10 +228,9 @@ class _AsyncInjectorStrategy {
|
|||
}
|
||||
|
||||
|
||||
|
||||
function _flattenBindings(bindings:List) {
|
||||
var res = {};
|
||||
ListWrapper.forEach(bindings, function (b){
|
||||
ListWrapper.forEach(bindings, function (b) {
|
||||
if (b instanceof Binding) {
|
||||
MapWrapper.set(res, b.key.id, b);
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ var _id:int = 0;
|
|||
@FIELD('final asFuture:bool')
|
||||
@FIELD('final lazy:bool')
|
||||
export class Dependency {
|
||||
constructor(key:Key, asFuture:bool, lazy:bool){
|
||||
constructor(key:Key, asFuture:bool, lazy:bool) {
|
||||
this.key = key;
|
||||
this.asFuture = asFuture;
|
||||
this.lazy = lazy;
|
||||
|
|
|
@ -37,12 +37,16 @@ class Reflector {
|
|||
|
||||
if (inject != null) {
|
||||
return new Dependency(Key.get(inject.token), false, false);
|
||||
|
||||
} else if (injectFuture != null) {
|
||||
return new Dependency(Key.get(injectFuture.token), true, false);
|
||||
|
||||
} else if (injectLazy != null) {
|
||||
return new Dependency(Key.get(injectLazy.token), false, true);
|
||||
|
||||
} else if (p.type.qualifiedName != #dynamic) {
|
||||
return new Dependency(Key.get(p.type.reflectedType), false, false);
|
||||
|
||||
} else {
|
||||
throw new NoAnnotationError(type);
|
||||
}
|
||||
|
|
|
@ -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 {Future, FutureWrapper} from 'facade/async';
|
||||
|
||||
class UserList {}
|
||||
class UserList {
|
||||
}
|
||||
|
||||
function fetchUsers() {
|
||||
return FutureWrapper.value(new UserList());
|
||||
}
|
||||
|
||||
class SynchronousUserList {}
|
||||
class SynchronousUserList {
|
||||
}
|
||||
|
||||
class UserController {
|
||||
constructor(list:UserList) {
|
||||
|
@ -22,11 +24,11 @@ class AsyncUserController {
|
|||
}
|
||||
}
|
||||
|
||||
export function main () {
|
||||
export function main() {
|
||||
describe("async injection", function () {
|
||||
|
||||
describe("asyncGet", function () {
|
||||
it('should return a future', function() {
|
||||
it('should return a future', function () {
|
||||
var injector = new Injector([
|
||||
bind(UserList).toAsyncFactory([], fetchUsers)
|
||||
]);
|
||||
|
@ -34,7 +36,7 @@ export function main () {
|
|||
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([
|
||||
SynchronousUserList
|
||||
]);
|
||||
|
@ -42,23 +44,23 @@ export function main () {
|
|||
expect(p).toBeFuture();
|
||||
});
|
||||
|
||||
it('should return the injector', function(done) {
|
||||
it('should return the injector', function (done) {
|
||||
var injector = new Injector([]);
|
||||
var p = injector.asyncGet(Injector);
|
||||
p.then(function(injector) {
|
||||
p.then(function (injector) {
|
||||
expect(injector).toBe(injector);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
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([
|
||||
bind(UserList).toAsyncFactory([], fetchUsers),
|
||||
UserController
|
||||
]);
|
||||
|
||||
injector.asyncGet(UserController).then(function(userController) {
|
||||
injector.asyncGet(UserController).then(function (userController) {
|
||||
expect(userController).toBeAnInstanceOf(UserController);
|
||||
expect(userController.list).toBeAnInstanceOf(UserList);
|
||||
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([
|
||||
UserController,
|
||||
bind(UserList).toAsyncFactory([], function(){throw "Broken UserList";})
|
||||
bind(UserList).toAsyncFactory([], function () {
|
||||
throw "Broken UserList";
|
||||
})
|
||||
]);
|
||||
|
||||
var future = injector.asyncGet(UserController);
|
||||
|
@ -119,7 +123,7 @@ export function main () {
|
|||
});
|
||||
|
||||
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([
|
||||
bind(UserList).toAsyncFactory([], fetchUsers)
|
||||
]);
|
||||
|
@ -128,7 +132,7 @@ export function main () {
|
|||
.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([
|
||||
bind(UserList).toAsyncFactory([], fetchUsers),
|
||||
UserController
|
||||
|
@ -138,7 +142,7 @@ export function main () {
|
|||
.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([
|
||||
bind(UserList).toAsyncFactory([], fetchUsers),
|
||||
AsyncUserController
|
||||
|
@ -149,7 +153,7 @@ export function main () {
|
|||
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([
|
||||
bind(UserList).toFactory([], () => new UserList()),
|
||||
AsyncUserController
|
||||
|
|
|
@ -1,17 +1,24 @@
|
|||
import {describe, ddescribe, it, iit, expect, beforeEach} from 'test_lib/test_lib';
|
||||
import {Injector, Inject, InjectLazy, bind} from 'di/di';
|
||||
|
||||
class Engine {}
|
||||
class Engine {
|
||||
}
|
||||
|
||||
class BrokenEngine {
|
||||
constructor() {
|
||||
throw "Broken Engine";
|
||||
}
|
||||
}
|
||||
class DashboardSoftware {}
|
||||
class Dashboard {
|
||||
constructor(software: DashboardSoftware){}
|
||||
|
||||
class DashboardSoftware {
|
||||
}
|
||||
|
||||
class Dashboard {
|
||||
constructor(software: DashboardSoftware) {}
|
||||
}
|
||||
|
||||
class TurboEngine extends Engine {
|
||||
}
|
||||
class TurboEngine extends Engine{}
|
||||
|
||||
class Car {
|
||||
constructor(engine:Engine) {
|
||||
|
@ -45,23 +52,23 @@ class CarWithInject {
|
|||
}
|
||||
|
||||
class CyclicEngine {
|
||||
constructor(car:Car){}
|
||||
constructor(car:Car) {}
|
||||
}
|
||||
|
||||
class NoAnnotations {
|
||||
constructor(secretDependency){}
|
||||
constructor(secretDependency) {}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('injector', function() {
|
||||
it('should instantiate a class without dependencies', function() {
|
||||
describe('injector', function () {
|
||||
it('should instantiate a class without dependencies', function () {
|
||||
var injector = new Injector([Engine]);
|
||||
var engine = injector.get(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 car = injector.get(Car);
|
||||
|
||||
|
@ -69,7 +76,7 @@ export function main() {
|
|||
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 car = injector.get(CarWithInject);
|
||||
|
||||
|
@ -82,7 +89,7 @@ export function main() {
|
|||
'Cannot resolve all parameters for NoAnnotations');
|
||||
});
|
||||
|
||||
it('should cache instances', function() {
|
||||
it('should cache instances', function () {
|
||||
var injector = new Injector([Engine]);
|
||||
|
||||
var e1 = injector.get(Engine);
|
||||
|
@ -91,7 +98,7 @@ export function main() {
|
|||
expect(e1).toBe(e2);
|
||||
});
|
||||
|
||||
it('should bind to a value', function() {
|
||||
it('should bind to a value', function () {
|
||||
var injector = new Injector([
|
||||
bind(Engine).toValue("fake engine")
|
||||
]);
|
||||
|
@ -100,7 +107,7 @@ export function main() {
|
|||
expect(engine).toEqual("fake engine");
|
||||
});
|
||||
|
||||
it('should bind to a factory', function() {
|
||||
it('should bind to a factory', function () {
|
||||
var injector = new Injector([
|
||||
Engine,
|
||||
bind(Car).toFactory([Engine], (e) => new SportsCar(e))
|
||||
|
@ -111,7 +118,7 @@ export function main() {
|
|||
expect(car.engine).toBeAnInstanceOf(Engine);
|
||||
});
|
||||
|
||||
it('should use non-type tokens', function() {
|
||||
it('should use non-type tokens', function () {
|
||||
var injector = new Injector([
|
||||
bind('token').toValue('value')
|
||||
]);
|
||||
|
@ -119,30 +126,30 @@ export function main() {
|
|||
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([bind("blah")])).toThrowError('Invalid binding blah');
|
||||
});
|
||||
|
||||
it('should provide itself', function() {
|
||||
it('should provide itself', function () {
|
||||
var parent = new Injector([]);
|
||||
var child = parent.createChild([]);
|
||||
|
||||
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([]);
|
||||
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]);
|
||||
expect(() => injector.get(CarWithDashboard)).
|
||||
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([
|
||||
Car,
|
||||
bind(Engine).toClass(CyclicEngine)
|
||||
|
@ -155,7 +162,7 @@ export function main() {
|
|||
.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([
|
||||
Car,
|
||||
bind(Engine).toClass(BrokenEngine)
|
||||
|
@ -171,7 +178,7 @@ export function main() {
|
|||
|
||||
|
||||
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 child = parent.createChild([]);
|
||||
|
||||
|
@ -181,7 +188,7 @@ export function main() {
|
|||
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 child = parent.createChild([
|
||||
bind(Engine).toClass(TurboEngine)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {describe, it, expect} from 'test_lib/test_lib';
|
||||
import {Key} from 'di/di';
|
||||
|
||||
export function main () {
|
||||
export function main() {
|
||||
describe("key", function () {
|
||||
it('should be equal to another key if type is the same', function () {
|
||||
expect(Key.get('car')).toBe(Key.get('car'));
|
||||
|
|
Loading…
Reference in New Issue