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 abstract class _Completer<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 _Completer() : future = new _FutureImpl<T>() { | 14 _Completer() : future = new _FutureImpl<T>() { |
15 _FutureImpl futureImpl = future; | 15 _FutureImpl futureImpl = future; |
16 futureImpl._zone.expectCallback(); | 16 futureImpl._zone.expectCallback(); |
17 } | 17 } |
18 | 18 |
19 void _setFutureValue(T value); | 19 void _setFutureValue(T value); |
20 void _setFutureError(error); | 20 void _setFutureError(error); |
21 | 21 |
22 void complete([T value]) { | 22 void complete([T value]) { |
23 if (_isComplete) throw new StateError("Future already completed"); | 23 if (_isComplete) throw new StateError("Future already completed"); |
24 _isComplete = true; | 24 _isComplete = true; |
25 _FutureImpl futureImpl = future; | 25 _FutureImpl futureImpl = future; |
26 futureImpl._zone.cancelCallbackExpectation(); | |
27 _setFutureValue(value); | 26 _setFutureValue(value); |
28 } | 27 } |
29 | 28 |
30 void completeError(Object error, [Object stackTrace = null]) { | 29 void completeError(Object error, [Object stackTrace = null]) { |
31 if (_isComplete) throw new StateError("Future already completed"); | 30 if (_isComplete) throw new StateError("Future already completed"); |
32 _isComplete = true; | 31 _isComplete = true; |
33 if (stackTrace != null) { | 32 if (stackTrace != null) { |
34 // Force the stack trace onto the error, even if it already had one. | 33 // Force the stack trace onto the error, even if it already had one. |
35 _attachStackTrace(error, stackTrace); | 34 _attachStackTrace(error, stackTrace); |
36 } | 35 } |
37 _FutureImpl futureImpl = future; | 36 _FutureImpl futureImpl = future; |
38 if (futureImpl._inSameErrorZone(_Zone.current)) { | 37 _setFutureError(error); |
39 futureImpl._zone.cancelCallbackExpectation(); | |
40 _setFutureError(error); | |
41 } else { | |
42 _Zone.current.handleUncaughtError(error); | |
43 } | |
44 } | 38 } |
45 | 39 |
46 bool get isCompleted => _isComplete; | 40 bool get isCompleted => _isComplete; |
47 } | 41 } |
48 | 42 |
49 class _AsyncCompleter<T> extends _Completer<T> { | 43 class _AsyncCompleter<T> extends _Completer<T> { |
50 void _setFutureValue(T value) { | 44 void _setFutureValue(T value) { |
51 _FutureImpl future = this.future; | 45 _FutureImpl future = this.future; |
52 future._asyncSetValue(value); | 46 future._asyncSetValue(value); |
| 47 // The async-error will schedule another callback, so we can cancel |
| 48 // the expectation without shutting down the zone. |
| 49 future._zone.cancelCallbackExpectation(); |
53 } | 50 } |
54 | 51 |
55 void _setFutureError(error) { | 52 void _setFutureError(error) { |
56 _FutureImpl future = this.future; | 53 _FutureImpl future = this.future; |
57 future._asyncSetError(error); | 54 future._asyncSetError(error); |
| 55 // The async-error will schedule another callback, so we can cancel |
| 56 // the expectation without shutting down the zone. |
| 57 future._zone.cancelCallbackExpectation(); |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 class _SyncCompleter<T> extends _Completer<T> { | 61 class _SyncCompleter<T> extends _Completer<T> { |
62 void _setFutureValue(T value) { | 62 void _setFutureValue(T value) { |
63 _FutureImpl future = this.future; | 63 _FutureImpl future = this.future; |
64 future._setValue(value); | 64 future._setValue(value); |
| 65 future._zone.cancelCallbackExpectation(); |
65 } | 66 } |
66 | 67 |
67 void _setFutureError(error) { | 68 void _setFutureError(error) { |
68 _FutureImpl future = this.future; | 69 _FutureImpl future = this.future; |
69 future._setError(error); | 70 future._setError(error); |
| 71 future._zone.cancelCallbackExpectation(); |
70 } | 72 } |
71 } | 73 } |
72 | 74 |
73 /** | 75 /** |
74 * A listener on a future. | 76 * A listener on a future. |
75 * | 77 * |
76 * When the future completes, the [_sendValue] or [_sendError] method | 78 * When the future completes, the [_sendValue] or [_sendError] method |
77 * is invoked with the result. | 79 * is invoked with the result. |
78 * | 80 * |
79 * Listeners are kept in a linked list. | 81 * Listeners are kept in a linked list. |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 _setError(error); | 678 _setError(error); |
677 }, onError: _setError); | 679 }, onError: _setError); |
678 return; | 680 return; |
679 } | 681 } |
680 } catch (e, s) { | 682 } catch (e, s) { |
681 error = _asyncError(e, s); | 683 error = _asyncError(e, s); |
682 } | 684 } |
683 _setError(error); | 685 _setError(error); |
684 } | 686 } |
685 } | 687 } |
OLD | NEW |