OLD | NEW |
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 Future<T> future; |
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 _setFutureValue(value); |
20 future._setValue(value); | |
21 } | 23 } |
22 | 24 |
23 void completeError(Object error, [Object stackTrace = null]) { | 25 void completeError(Object error, [Object stackTrace = null]) { |
24 if (_isComplete) throw new StateError("Future already completed"); | 26 if (_isComplete) throw new StateError("Future already completed"); |
25 _isComplete = true; | 27 _isComplete = true; |
26 if (stackTrace != null) { | 28 if (stackTrace != null) { |
27 // Force the stack trace onto the error, even if it already had one. | 29 // Force the stack trace onto the error, even if it already had one. |
28 _attachStackTrace(error, stackTrace); | 30 _attachStackTrace(error, stackTrace); |
29 } | 31 } |
30 _FutureImpl future = this.future; | 32 _setFutureError(error); |
31 future._setError(error); | |
32 } | 33 } |
33 | 34 |
34 bool get isCompleted => _isComplete; | 35 bool get isCompleted => _isComplete; |
35 } | 36 } |
36 | 37 |
| 38 class _AsyncCompleter<T> extends _Completer<T> { |
| 39 void _setFutureValue(T value) { |
| 40 _FutureImpl future = this.future; |
| 41 runAsync(() { future._setValue(value); }); |
| 42 } |
| 43 |
| 44 void _setFutureError(error) { |
| 45 _FutureImpl future = this.future; |
| 46 runAsync(() { future._setError(error); }); |
| 47 } |
| 48 } |
| 49 |
| 50 class _SyncCompleter<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 Loading... |
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 } |
OLD | NEW |