OLD | NEW |
1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
2 // Dart core library. | 2 // Dart core library. |
3 | 3 |
4 /** | 4 /** |
5 * Thrown if client tries to obtain value or exception | 5 * Thrown if client tries to obtain value or exception |
6 * before a future has completed. | 6 * before a future has completed. |
7 */ | 7 */ |
8 class FutureNotCompleteException implements Exception { | 8 class FutureNotCompleteException implements Exception { |
9 FutureNotCompleteException() {} | 9 FutureNotCompleteException() {} |
10 String toString() => "Exception: future has not been completed"; | 10 String toString() => "Exception: future has not been completed"; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 void then(void onComplete(T value)) { | 87 void then(void onComplete(T value)) { |
88 if (hasValue) { | 88 if (hasValue) { |
89 onComplete(value); | 89 onComplete(value); |
90 } else if (!isComplete) { | 90 } else if (!isComplete) { |
91 _listeners.add(onComplete); | 91 _listeners.add(onComplete); |
92 } else if (!_exceptionHandled) { | 92 } else if (!_exceptionHandled) { |
93 throw _exception; | 93 throw _exception; |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 void handleException(void onException(Object exception)) { | 97 void handleException(bool onException(Object exception)) { |
98 _exceptionHandlers.add(onException); | 98 if (_exceptionHandled) return; |
| 99 if (_isComplete) { |
| 100 if (_exception != null) { |
| 101 _exceptionHandled = onException(_exception); |
| 102 } |
| 103 } else { |
| 104 _exceptionHandlers.add(onException); |
| 105 } |
99 } | 106 } |
100 | 107 |
101 void _complete() { | 108 void _complete() { |
102 _isComplete = true; | 109 _isComplete = true; |
103 if (_exception !== null) { | 110 if (_exception !== null) { |
104 for (Function handler in _exceptionHandlers) { | 111 for (Function handler in _exceptionHandlers) { |
105 if (handler(_exception)) { | 112 if (handler(_exception)) { |
106 _exceptionHandled = true; | 113 _exceptionHandled = true; |
107 break; | 114 break; |
108 } | 115 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 } | 200 } |
194 | 201 |
195 void complete(T value) { | 202 void complete(T value) { |
196 _futureImpl._setValue(value); | 203 _futureImpl._setValue(value); |
197 } | 204 } |
198 | 205 |
199 void completeException(var exception) { | 206 void completeException(var exception) { |
200 _futureImpl._setException(exception); | 207 _futureImpl._setException(exception); |
201 } | 208 } |
202 } | 209 } |
OLD | NEW |