Skip to content

Commit 1a7d516

Browse files
committed
use Promise instead of Future
1 parent f524a89 commit 1a7d516

File tree

14 files changed

+97
-82
lines changed

14 files changed

+97
-82
lines changed

modules/core/src/compiler/compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Type} from 'facade/lang';
2-
import {Future} from 'facade/async';
2+
import {Promise} from 'facade/async';
33
import {Element} from 'facade/dom';
44
//import {ProtoView} from './view';
55
import {TemplateLoader} from './template_loader';
@@ -13,14 +13,14 @@ export class Compiler {
1313
}
1414

1515
/**
16-
* # Why future?
16+
* # Why promise?
1717
* - compilation will load templates. Instantiating views before templates are loaded will
1818
* complicate the Directive code. BENEFIT: view instantiation become synchrnous.
1919
* # Why result that is independent of injector?
2020
* - don't know about injector in deserialization
2121
* - compile does not need the injector, only the ViewFactory does
2222
*/
23-
compile(component:Type, element:Element/* = null*/):Future/*<ProtoView>*/ {
23+
compile(component:Type, element:Element/* = null*/):Promise/*<ProtoView>*/ {
2424
return null;
2525
}
2626

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {Future} from 'facade/async';
1+
import {Promise} from 'facade/async';
22
//import {Document} from 'facade/dom';
33

44
export class TemplateLoader {
55

66
constructor() {}
77

8-
load(url:String):Future/*<Document>*/ {
8+
load(url:String):Promise/*<Document>*/ {
99
return null;
1010
}
1111
}

modules/di/src/annotations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Inject {
77
}
88
}
99

10-
export class InjectFuture {
10+
export class InjectPromise {
1111
@CONST()
1212
constructor(token) {
1313
this.token = token;

modules/di/src/binding.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import {Key} from './key';
55

66
export class Dependency {
77
@FIELD('final key:Key')
8-
@FIELD('final asFuture:bool')
8+
@FIELD('final asPromise:bool')
99
@FIELD('final lazy:bool')
10-
constructor(key:Key, asFuture:boolean, lazy:boolean) {
10+
constructor(key:Key, asPromise:boolean, lazy:boolean) {
1111
this.key = key;
12-
this.asFuture = asFuture;
12+
this.asPromise = asPromise;
1313
this.lazy = lazy;
1414
}
1515
}
1616

1717
export class Binding {
18-
constructor(key:Key, factory:Function, dependencies:List, providedAsFuture:boolean) {
18+
constructor(key:Key, factory:Function, dependencies:List, providedAsPromise:boolean) {
1919
this.key = key;
2020
this.factory = factory;
2121
this.dependencies = dependencies;
22-
this.providedAsFuture = providedAsFuture;
22+
this.providedAsPromise = providedAsPromise;
2323
}
2424
}
2525

modules/di/src/exceptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class AsyncBindingError extends ProviderError {
4343
super(key, function (keys:List) {
4444
var first = stringify(ListWrapper.first(keys).token);
4545
return `Cannot instantiate ${first} synchronously. ` +
46-
`It is provided as a future!${constructResolvingPath(keys)}`;
46+
`It is provided as a promise!${constructResolvingPath(keys)}`;
4747
});
4848
}
4949
}

modules/di/src/injector.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {Binding, BindingBuilder, bind} from './binding';
33
import {ProviderError, NoProviderError, InvalidBindingError,
44
AsyncBindingError, CyclicDependencyError, InstantiationError} from './exceptions';
55
import {Type, isPresent, isBlank} from 'facade/lang';
6-
import {Future, FutureWrapper} from 'facade/async';
6+
import {Promise, PromiseWrapper} from 'facade/async';
77
import {Key} from './key';
88

99
var _constructing = new Object();
1010

1111
class _Waiting {
12-
constructor(future:Future) {
13-
this.future = future;
12+
constructor(promise:Promise) {
13+
this.promise = promise;
1414
}
1515
}
1616
function _isWaiting(obj):boolean {
@@ -53,12 +53,12 @@ export class Injector {
5353
return ListWrapper.createFixedSize(Key.numberOfKeys() + 1);
5454
}
5555

56-
_getByKey(key:Key, returnFuture:boolean, returnLazy:boolean) {
56+
_getByKey(key:Key, returnPromise:boolean, returnLazy:boolean) {
5757
if (returnLazy) {
58-
return () => this._getByKey(key, returnFuture, false);
58+
return () => this._getByKey(key, returnPromise, false);
5959
}
6060

61-
var strategy = returnFuture ? this._asyncStrategy : this._syncStrategy;
61+
var strategy = returnPromise ? this._asyncStrategy : this._syncStrategy;
6262

6363
var instance = strategy.readFromCache(key);
6464
if (isPresent(instance)) return instance;
@@ -67,14 +67,14 @@ export class Injector {
6767
if (isPresent(instance)) return instance;
6868

6969
if (isPresent(this._parent)) {
70-
return this._parent._getByKey(key, returnFuture, returnLazy);
70+
return this._parent._getByKey(key, returnPromise, returnLazy);
7171
}
7272
throw new NoProviderError(key);
7373
}
7474

7575
_resolveDependencies(key:Key, binding:Binding, forceAsync:boolean):List {
7676
try {
77-
var getDependency = d => this._getByKey(d.key, forceAsync || d.asFuture, d.lazy);
77+
var getDependency = d => this._getByKey(d.key, forceAsync || d.asPromise, d.lazy);
7878
return ListWrapper.map(binding.dependencies, getDependency);
7979
} catch (e) {
8080
this._clear(key);
@@ -139,7 +139,7 @@ class _SyncInjectorStrategy {
139139
var binding = this.injector._getBinding(key);
140140
if (isBlank(binding)) return null;
141141

142-
if (binding.providedAsFuture) throw new AsyncBindingError(key);
142+
if (binding.providedAsPromise) throw new AsyncBindingError(key);
143143

144144
//add a marker so we can detect cyclic dependencies
145145
this.injector._markAsConstructing(key);
@@ -168,17 +168,17 @@ class _AsyncInjectorStrategy {
168168

169169
readFromCache(key:Key) {
170170
if (key.token === Injector) {
171-
return FutureWrapper.value(this.injector);
171+
return PromiseWrapper.resolve(this.injector);
172172
}
173173

174174
var instance = this.injector._getInstance(key);
175175

176176
if (instance === _constructing) {
177177
throw new CyclicDependencyError(key);
178178
} else if (_isWaiting(instance)) {
179-
return instance.future;
179+
return instance.promise;
180180
} else if (isPresent(instance)) {
181-
return FutureWrapper.value(instance);
181+
return PromiseWrapper.resolve(instance);
182182
} else {
183183
return null;
184184
}
@@ -192,19 +192,19 @@ class _AsyncInjectorStrategy {
192192
this.injector._markAsConstructing(key);
193193

194194
var deps = this.injector._resolveDependencies(key, binding, true);
195-
var depsFuture = FutureWrapper.wait(deps);
195+
var depsPromise = PromiseWrapper.all(deps);
196196

197-
var future = FutureWrapper.catchError(depsFuture, (e) => this._errorHandler(key, e)).
197+
var promise = PromiseWrapper.then(depsPromise, null, (e) => this._errorHandler(key, e)).
198198
then(deps => this._findOrCreate(key, binding, deps)).
199199
then(instance => this._cacheInstance(key, instance));
200200

201-
this.injector._setInstance(key, new _Waiting(future));
202-
return future;
201+
this.injector._setInstance(key, new _Waiting(promise));
202+
return promise;
203203
}
204204

205-
_errorHandler(key:Key, e):Future {
205+
_errorHandler(key:Key, e):Promise {
206206
if (e instanceof ProviderError) e.addKey(key);
207-
return FutureWrapper.error(e);
207+
return PromiseWrapper.reject(e);
208208
}
209209

210210
_findOrCreate(key:Key, binding:Binding, deps:List) {

modules/di/src/reflector.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
library facade.di.reflector;
22

33
import 'dart:mirrors';
4-
import 'annotations.dart' show Inject, InjectFuture, InjectLazy;
4+
import 'annotations.dart' show Inject, InjectPromise, InjectLazy;
55
import 'key.dart' show Key;
66
import 'binding.dart' show Dependency;
77
import 'exceptions.dart' show NoAnnotationError;
@@ -34,14 +34,14 @@ class Reflector {
3434
final metadata = p.metadata.map((m) => m.reflectee);
3535

3636
var inject = metadata.firstWhere((m) => m is Inject, orElse: () => null);
37-
var injectFuture = metadata.firstWhere((m) => m is InjectFuture, orElse: () => null);
37+
var injectPromise = metadata.firstWhere((m) => m is InjectPromise, orElse: () => null);
3838
var injectLazy = metadata.firstWhere((m) => m is InjectLazy, orElse: () => null);
3939

4040
if (inject != null) {
4141
return new Dependency(Key.get(inject.token), false, false);
4242

43-
} else if (injectFuture != null) {
44-
return new Dependency(Key.get(injectFuture.token), true, false);
43+
} else if (injectPromise != null) {
44+
return new Dependency(Key.get(injectPromise.token), true, false);
4545

4646
} else if (injectLazy != null) {
4747
return new Dependency(Key.get(injectLazy.token), false, true);

modules/di/src/reflector.es6

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Type, isPresent} from 'facade/lang';
22
import {List} from 'facade/collection';
3-
import {Inject, InjectFuture, InjectLazy} from './annotations';
3+
import {Inject, InjectPromise, InjectLazy} from './annotations';
44
import {Key} from './key';
55
import {Dependency} from './binding';
66
import {NoAnnotationError} from './exceptions';
@@ -31,7 +31,7 @@ class Reflector {
3131
} else if (paramAnnotation instanceof Inject) {
3232
return this._createDependency(paramAnnotation.token, false, false);
3333

34-
} else if (paramAnnotation instanceof InjectFuture) {
34+
} else if (paramAnnotation instanceof InjectPromise) {
3535
return this._createDependency(paramAnnotation.token, true, false);
3636

3737
} else if (paramAnnotation instanceof InjectLazy) {
@@ -46,8 +46,8 @@ class Reflector {
4646
}
4747
}
4848

49-
_createDependency(token, asFuture, lazy):Dependency {
50-
return new Dependency(Key.get(token), asFuture, lazy);
49+
_createDependency(token, asPromise, lazy):Dependency {
50+
return new Dependency(Key.get(token), asPromise, lazy);
5151
}
5252
}
5353

modules/di/test/di/async_spec.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {ddescribe, describe, it, iit, xit, expect, beforeEach} from 'test_lib/test_lib';
2-
import {Injector, Inject, InjectFuture, bind, Key} from 'di/di';
3-
import {Future, FutureWrapper} from 'facade/async';
2+
import {Injector, Inject, InjectPromise, bind, Key} from 'di/di';
3+
import {Promise, PromiseWrapper} from 'facade/async';
44

55
class UserList {
66
}
77

88
function fetchUsers() {
9-
return FutureWrapper.value(new UserList());
9+
return PromiseWrapper.resolve(new UserList());
1010
}
1111

1212
class SynchronousUserList {
@@ -19,7 +19,7 @@ class UserController {
1919
}
2020

2121
class AsyncUserController {
22-
constructor(@InjectFuture(UserList) userList) {
22+
constructor(@InjectPromise(UserList) userList) {
2323
this.userList = userList;
2424
}
2525
}
@@ -28,28 +28,28 @@ export function main() {
2828
describe("async injection", function () {
2929

3030
describe("asyncGet", function () {
31-
it('should return a future', function () {
31+
it('should return a promise', function () {
3232
var injector = new Injector([
3333
bind(UserList).toAsyncFactory(fetchUsers)
3434
]);
3535
var p = injector.asyncGet(UserList);
36-
expect(p).toBeFuture();
36+
expect(p).toBePromise();
3737
});
3838

39-
it('should return a future when the binding is sync', function () {
39+
it('should return a promise when the binding is sync', function () {
4040
var injector = new Injector([
4141
SynchronousUserList
4242
]);
4343
var p = injector.asyncGet(SynchronousUserList);
44-
expect(p).toBeFuture();
44+
expect(p).toBePromise();
4545
});
4646

47-
it("should return a future when the binding is sync (from cache)", function () {
47+
it("should return a promise when the binding is sync (from cache)", function () {
4848
var injector = new Injector([
4949
UserList
5050
]);
5151
expect(injector.get(UserList)).toBeAnInstanceOf(UserList);
52-
expect(injector.asyncGet(UserList)).toBeFuture();
52+
expect(injector.asyncGet(UserList)).toBePromise();
5353
});
5454

5555
it('should return the injector', function (done) {
@@ -61,7 +61,7 @@ export function main() {
6161
});
6262
});
6363

64-
it('should return a future when instantiating a sync binding ' +
64+
it('should return a promise when instantiating a sync binding ' +
6565
'with an async dependency', function (done) {
6666
var injector = new Injector([
6767
bind(UserList).toAsyncFactory(fetchUsers),
@@ -83,7 +83,7 @@ export function main() {
8383
var ul1 = injector.asyncGet(UserList);
8484
var ul2 = injector.asyncGet(UserList);
8585

86-
FutureWrapper.wait([ul1, ul2]).then(function (uls) {
86+
PromiseWrapper.all([ul1, ul2]).then(function (uls) {
8787
expect(uls[0]).toBe(uls[1]);
8888
done();
8989
});
@@ -94,13 +94,13 @@ export function main() {
9494
UserList
9595
]);
9696

97-
var future = injector.asyncGet(UserList);
97+
var promise = injector.asyncGet(UserList);
9898
var ul = injector.get(UserList);
9999

100-
expect(future).toBeFuture();
100+
expect(promise).toBePromise();
101101
expect(ul).toBeAnInstanceOf(UserList);
102102

103-
future.then(function (ful) {
103+
promise.then(function (ful) {
104104
expect(ful).toBe(ul);
105105
done();
106106
});
@@ -114,8 +114,8 @@ export function main() {
114114
})
115115
]);
116116

117-
var future = injector.asyncGet(UserController);
118-
FutureWrapper.catchError(future, function (e) {
117+
var promise = injector.asyncGet(UserController);
118+
PromiseWrapper.then(promise, null, function (e) {
119119
expect(e.message).toContain("Error during instantiation of UserList! (UserController -> UserList)");
120120
done();
121121
});
@@ -129,7 +129,7 @@ export function main() {
129129
]);
130130

131131
expect(() => injector.get(UserList))
132-
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a future!');
132+
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise!');
133133
});
134134

135135
it('should throw when instantiating a sync binding with an dependency', function () {
@@ -139,29 +139,29 @@ export function main() {
139139
]);
140140

141141
expect(() => injector.get(UserController))
142-
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a future! (UserController -> UserList)');
142+
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise! (UserController -> UserList)');
143143
});
144144

145-
it('should resolve synchronously when an async dependency requested as a future', function () {
145+
it('should resolve synchronously when an async dependency requested as a promise', function () {
146146
var injector = new Injector([
147147
bind(UserList).toAsyncFactory(fetchUsers),
148148
AsyncUserController
149149
]);
150150
var controller = injector.get(AsyncUserController);
151151

152152
expect(controller).toBeAnInstanceOf(AsyncUserController);
153-
expect(controller.userList).toBeFuture();
153+
expect(controller.userList).toBePromise();
154154
});
155155

156-
it('should wrap sync dependencies into futures if required', function () {
156+
it('should wrap sync dependencies into promises if required', function () {
157157
var injector = new Injector([
158158
bind(UserList).toFactory(() => new UserList()),
159159
AsyncUserController
160160
]);
161161
var controller = injector.get(AsyncUserController);
162162

163163
expect(controller).toBeAnInstanceOf(AsyncUserController);
164-
expect(controller.userList).toBeFuture();
164+
expect(controller.userList).toBePromise();
165165
});
166166
});
167167
});

0 commit comments

Comments
 (0)