use Promise instead of Future

This commit is contained in:
vsavkin
2014-10-10 15:44:56 -04:00
parent f524a89cb6
commit 1a7d5160f2
14 changed files with 97 additions and 82 deletions

View File

@ -7,7 +7,7 @@ export class Inject {
}
}
export class InjectFuture {
export class InjectPromise {
@CONST()
constructor(token) {
this.token = token;

View File

@ -5,21 +5,21 @@ import {Key} from './key';
export class Dependency {
@FIELD('final key:Key')
@FIELD('final asFuture:bool')
@FIELD('final asPromise:bool')
@FIELD('final lazy:bool')
constructor(key:Key, asFuture:boolean, lazy:boolean) {
constructor(key:Key, asPromise:boolean, lazy:boolean) {
this.key = key;
this.asFuture = asFuture;
this.asPromise = asPromise;
this.lazy = lazy;
}
}
export class Binding {
constructor(key:Key, factory:Function, dependencies:List, providedAsFuture:boolean) {
constructor(key:Key, factory:Function, dependencies:List, providedAsPromise:boolean) {
this.key = key;
this.factory = factory;
this.dependencies = dependencies;
this.providedAsFuture = providedAsFuture;
this.providedAsPromise = providedAsPromise;
}
}

View File

@ -43,7 +43,7 @@ export class AsyncBindingError extends ProviderError {
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)}`;
`It is provided as a promise!${constructResolvingPath(keys)}`;
});
}
}

View File

@ -3,14 +3,14 @@ import {Binding, BindingBuilder, bind} from './binding';
import {ProviderError, NoProviderError, InvalidBindingError,
AsyncBindingError, CyclicDependencyError, InstantiationError} from './exceptions';
import {Type, isPresent, isBlank} from 'facade/lang';
import {Future, FutureWrapper} from 'facade/async';
import {Promise, PromiseWrapper} from 'facade/async';
import {Key} from './key';
var _constructing = new Object();
class _Waiting {
constructor(future:Future) {
this.future = future;
constructor(promise:Promise) {
this.promise = promise;
}
}
function _isWaiting(obj):boolean {
@ -53,12 +53,12 @@ export class Injector {
return ListWrapper.createFixedSize(Key.numberOfKeys() + 1);
}
_getByKey(key:Key, returnFuture:boolean, returnLazy:boolean) {
_getByKey(key:Key, returnPromise:boolean, returnLazy:boolean) {
if (returnLazy) {
return () => this._getByKey(key, returnFuture, false);
return () => this._getByKey(key, returnPromise, false);
}
var strategy = returnFuture ? this._asyncStrategy : this._syncStrategy;
var strategy = returnPromise ? this._asyncStrategy : this._syncStrategy;
var instance = strategy.readFromCache(key);
if (isPresent(instance)) return instance;
@ -67,14 +67,14 @@ export class Injector {
if (isPresent(instance)) return instance;
if (isPresent(this._parent)) {
return this._parent._getByKey(key, returnFuture, returnLazy);
return this._parent._getByKey(key, returnPromise, returnLazy);
}
throw new NoProviderError(key);
}
_resolveDependencies(key:Key, binding:Binding, forceAsync:boolean):List {
try {
var getDependency = d => this._getByKey(d.key, forceAsync || d.asFuture, d.lazy);
var getDependency = d => this._getByKey(d.key, forceAsync || d.asPromise, d.lazy);
return ListWrapper.map(binding.dependencies, getDependency);
} catch (e) {
this._clear(key);
@ -139,7 +139,7 @@ class _SyncInjectorStrategy {
var binding = this.injector._getBinding(key);
if (isBlank(binding)) return null;
if (binding.providedAsFuture) throw new AsyncBindingError(key);
if (binding.providedAsPromise) throw new AsyncBindingError(key);
//add a marker so we can detect cyclic dependencies
this.injector._markAsConstructing(key);
@ -168,7 +168,7 @@ class _AsyncInjectorStrategy {
readFromCache(key:Key) {
if (key.token === Injector) {
return FutureWrapper.value(this.injector);
return PromiseWrapper.resolve(this.injector);
}
var instance = this.injector._getInstance(key);
@ -176,9 +176,9 @@ class _AsyncInjectorStrategy {
if (instance === _constructing) {
throw new CyclicDependencyError(key);
} else if (_isWaiting(instance)) {
return instance.future;
return instance.promise;
} else if (isPresent(instance)) {
return FutureWrapper.value(instance);
return PromiseWrapper.resolve(instance);
} else {
return null;
}
@ -192,19 +192,19 @@ class _AsyncInjectorStrategy {
this.injector._markAsConstructing(key);
var deps = this.injector._resolveDependencies(key, binding, true);
var depsFuture = FutureWrapper.wait(deps);
var depsPromise = PromiseWrapper.all(deps);
var future = FutureWrapper.catchError(depsFuture, (e) => this._errorHandler(key, e)).
var promise = PromiseWrapper.then(depsPromise, null, (e) => this._errorHandler(key, e)).
then(deps => this._findOrCreate(key, binding, deps)).
then(instance => this._cacheInstance(key, instance));
this.injector._setInstance(key, new _Waiting(future));
return future;
this.injector._setInstance(key, new _Waiting(promise));
return promise;
}
_errorHandler(key:Key, e):Future {
_errorHandler(key:Key, e):Promise {
if (e instanceof ProviderError) e.addKey(key);
return FutureWrapper.error(e);
return PromiseWrapper.reject(e);
}
_findOrCreate(key:Key, binding:Binding, deps:List) {

View File

@ -1,7 +1,7 @@
library facade.di.reflector;
import 'dart:mirrors';
import 'annotations.dart' show Inject, InjectFuture, InjectLazy;
import 'annotations.dart' show Inject, InjectPromise, InjectLazy;
import 'key.dart' show Key;
import 'binding.dart' show Dependency;
import 'exceptions.dart' show NoAnnotationError;
@ -34,14 +34,14 @@ class Reflector {
final metadata = p.metadata.map((m) => m.reflectee);
var inject = metadata.firstWhere((m) => m is Inject, orElse: () => null);
var injectFuture = metadata.firstWhere((m) => m is InjectFuture, orElse: () => null);
var injectPromise = metadata.firstWhere((m) => m is InjectPromise, orElse: () => null);
var injectLazy = metadata.firstWhere((m) => m is InjectLazy, orElse: () => null);
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 (injectPromise != null) {
return new Dependency(Key.get(injectPromise.token), true, false);
} else if (injectLazy != null) {
return new Dependency(Key.get(injectLazy.token), false, true);

View File

@ -1,6 +1,6 @@
import {Type, isPresent} from 'facade/lang';
import {List} from 'facade/collection';
import {Inject, InjectFuture, InjectLazy} from './annotations';
import {Inject, InjectPromise, InjectLazy} from './annotations';
import {Key} from './key';
import {Dependency} from './binding';
import {NoAnnotationError} from './exceptions';
@ -31,7 +31,7 @@ class Reflector {
} else if (paramAnnotation instanceof Inject) {
return this._createDependency(paramAnnotation.token, false, false);
} else if (paramAnnotation instanceof InjectFuture) {
} else if (paramAnnotation instanceof InjectPromise) {
return this._createDependency(paramAnnotation.token, true, false);
} else if (paramAnnotation instanceof InjectLazy) {
@ -46,8 +46,8 @@ class Reflector {
}
}
_createDependency(token, asFuture, lazy):Dependency {
return new Dependency(Key.get(token), asFuture, lazy);
_createDependency(token, asPromise, lazy):Dependency {
return new Dependency(Key.get(token), asPromise, lazy);
}
}