Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: sdk/lib/async/future_impl.dart

Issue 14690009: Make Completers asynchronous. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.async; 5 part of dart.async;
6 6
7 deprecatedFutureValue(_FutureImpl future) => 7 deprecatedFutureValue(_FutureImpl future) =>
8 future._isComplete ? future._resultOrListeners : null; 8 future._isComplete ? future._resultOrListeners : null;
9 9
10 class _CompleterImpl<T> implements Completer<T> { 10 abstract class _Completer<T> implements Completer<T> {
11 final Future<T> future; 11 final _FutureImpl<T> future;
Lasse Reichstein Nielsen 2013/05/06 06:49:11 _FutureImpl -> Future. No reason to change the typ
floitsch 2013/05/06 15:14:45 Done.
12 bool _isComplete = false; 12 bool _isComplete = false;
13 13
14 _CompleterImpl() : future = new _FutureImpl<T>(); 14 _Completer() : future = new _FutureImpl<T>();
15
16 void _setFutureValue(T value);
17 void _setFutureError(error);
15 18
16 void complete([T value]) { 19 void complete([T value]) {
17 if (_isComplete) throw new StateError("Future already completed"); 20 if (_isComplete) throw new StateError("Future already completed");
18 _isComplete = true; 21 _isComplete = true;
19 _FutureImpl future = this.future; 22 _FutureImpl future = this.future;
Lasse Reichstein Nielsen 2013/05/06 06:49:11 Move this cast to _setFutureValue.
floitsch 2013/05/06 15:14:45 Done.
20 future._setValue(value); 23 _setFutureValue(value);
21 } 24 }
22 25
23 void completeError(Object error, [Object stackTrace = null]) { 26 void completeError(Object error, [Object stackTrace = null]) {
24 if (_isComplete) throw new StateError("Future already completed"); 27 if (_isComplete) throw new StateError("Future already completed");
25 _isComplete = true; 28 _isComplete = true;
26 if (stackTrace != null) { 29 if (stackTrace != null) {
27 // Force the stack trace onto the error, even if it already had one. 30 // Force the stack trace onto the error, even if it already had one.
28 _attachStackTrace(error, stackTrace); 31 _attachStackTrace(error, stackTrace);
29 } 32 }
30 _FutureImpl future = this.future; 33 _FutureImpl future = this.future;
Lasse Reichstein Nielsen 2013/05/06 06:49:11 And remove this one.
floitsch 2013/05/06 15:14:45 Done.
31 future._setError(error); 34 _setFutureError(error);
32 } 35 }
33 36
34 bool get isCompleted => _isComplete; 37 bool get isCompleted => _isComplete;
35 } 38 }
36 39
40 class _CompleterImpl<T> extends _Completer<T> {
Lasse Reichstein Nielsen 2013/05/06 06:49:11 I prefer _AsyncCompleter (and _SyncCompleter below
floitsch 2013/05/06 15:14:45 Done.
41 void _setFutureValue(T value) {
42 runAsync(() { future._setValue(value); });
43 }
44
45 void _setFutureError(error) {
46 runAsync(() { future._setError(error); });
47 }
48 }
49
50 class _SyncCompleterImpl<T> extends _Completer<T> {
51 void _setFutureValue(T value) {
52 future._setValue(value);
53 }
54
55 void _setFutureError(error) {
56 future._setError(error);
57 }
58 }
59
37 /** 60 /**
38 * A listener on a future. 61 * A listener on a future.
39 * 62 *
40 * When the future completes, the [_sendValue] or [_sendError] method 63 * When the future completes, the [_sendValue] or [_sendError] method
41 * is invoked with the result. 64 * is invoked with the result.
42 * 65 *
43 * Listeners are kept in a linked list. 66 * Listeners are kept in a linked list.
44 */ 67 */
45 abstract class _FutureListener<T> { 68 abstract class _FutureListener<T> {
46 _FutureListener _nextListener; 69 _FutureListener _nextListener;
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 Future catchError(function(error), {bool test(var error)}) { 531 Future catchError(function(error), {bool test(var error)}) {
509 return _future.catchError(function, test: test); 532 return _future.catchError(function, test: test);
510 } 533 }
511 534
512 Future<T> whenComplete(action()) { 535 Future<T> whenComplete(action()) {
513 return _future.whenComplete(action); 536 return _future.whenComplete(action);
514 } 537 }
515 538
516 Stream<T> asStream() => new Stream.fromFuture(_future); 539 Stream<T> asStream() => new Stream.fromFuture(_future);
517 } 540 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698