| 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 /** The onValue and onError handlers return either a value or a future */ | 7 /** The onValue and onError handlers return either a value or a future */ |
| 8 typedef dynamic _FutureOnValue<T>(T value); | 8 typedef dynamic _FutureOnValue<T>(T value); |
| 9 /** Test used by [Future.catchError] to handle skip some errors. */ | 9 /** Test used by [Future.catchError] to handle skip some errors. */ |
| 10 typedef bool _FutureErrorTest(var error); | 10 typedef bool _FutureErrorTest(var error); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 * will complete with the same result. | 168 * will complete with the same result. |
| 169 * All listeners are forwarded to the other future. | 169 * All listeners are forwarded to the other future. |
| 170 * | 170 * |
| 171 * The cases are disjoint - incomplete and unchained ([_INCOMPLETE]), | 171 * The cases are disjoint - incomplete and unchained ([_INCOMPLETE]), |
| 172 * incomplete and chained ([_CHAINED]), or completed with value or error | 172 * incomplete and chained ([_CHAINED]), or completed with value or error |
| 173 * ([_VALUE] or [_ERROR]) - so the field only needs to hold | 173 * ([_VALUE] or [_ERROR]) - so the field only needs to hold |
| 174 * one value at a time. | 174 * one value at a time. |
| 175 */ | 175 */ |
| 176 var _resultOrListeners; | 176 var _resultOrListeners; |
| 177 | 177 |
| 178 // This constructor is used by async/await. |
| 178 _Future(); | 179 _Future(); |
| 179 | 180 |
| 180 /// Valid types for value: `T` or `Future<T>`. | 181 /// Valid types for value: `T` or `Future<T>`. |
| 181 _Future.immediate(value) { | 182 _Future.immediate(value) { |
| 182 _asyncComplete(value); | 183 _asyncComplete(value); |
| 183 } | 184 } |
| 184 | 185 |
| 185 _Future.immediateError(var error, [StackTrace stackTrace]) { | 186 _Future.immediateError(var error, [StackTrace stackTrace]) { |
| 186 _asyncCompleteError(error, stackTrace); | 187 _asyncCompleteError(error, stackTrace); |
| 187 } | 188 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 206 Zone currentZone = Zone.current; | 207 Zone currentZone = Zone.current; |
| 207 if (!identical(currentZone, _ROOT_ZONE)) { | 208 if (!identical(currentZone, _ROOT_ZONE)) { |
| 208 f = currentZone.registerUnaryCallback(f); | 209 f = currentZone.registerUnaryCallback(f); |
| 209 if (onError != null) { | 210 if (onError != null) { |
| 210 onError = _registerErrorHandler(onError, currentZone); | 211 onError = _registerErrorHandler(onError, currentZone); |
| 211 } | 212 } |
| 212 } | 213 } |
| 213 return _thenNoZoneRegistration(f, onError); | 214 return _thenNoZoneRegistration(f, onError); |
| 214 } | 215 } |
| 215 | 216 |
| 216 // This function is used by async/await. | 217 // This method is used by async/await. |
| 217 Future _thenNoZoneRegistration(f(T value), Function onError) { | 218 Future _thenNoZoneRegistration(f(T value), Function onError) { |
| 218 _Future result = new _Future(); | 219 _Future result = new _Future(); |
| 219 _addListener(new _FutureListener.then(result, f, onError)); | 220 _addListener(new _FutureListener.then(result, f, onError)); |
| 220 return result; | 221 return result; |
| 221 } | 222 } |
| 222 | 223 |
| 223 Future catchError(Function onError, { bool test(error) }) { | 224 Future catchError(Function onError, { bool test(error) }) { |
| 224 _Future result = new _Future(); | 225 _Future result = new _Future(); |
| 225 if (!identical(result._zone, _ROOT_ZONE)) { | 226 if (!identical(result._zone, _ROOT_ZONE)) { |
| 226 onError = _registerErrorHandler(onError, result._zone); | 227 onError = _registerErrorHandler(onError, result._zone); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 249 T get _value { | 250 T get _value { |
| 250 assert(_isComplete && _hasValue); | 251 assert(_isComplete && _hasValue); |
| 251 return _resultOrListeners; | 252 return _resultOrListeners; |
| 252 } | 253 } |
| 253 | 254 |
| 254 AsyncError get _error { | 255 AsyncError get _error { |
| 255 assert(_isComplete && _hasError); | 256 assert(_isComplete && _hasError); |
| 256 return _resultOrListeners; | 257 return _resultOrListeners; |
| 257 } | 258 } |
| 258 | 259 |
| 260 // This method is used by async/await. |
| 259 void _setValue(T value) { | 261 void _setValue(T value) { |
| 260 assert(!_isComplete); // But may have a completion pending. | 262 assert(!_isComplete); // But may have a completion pending. |
| 261 _state = _VALUE; | 263 _state = _VALUE; |
| 262 _resultOrListeners = value; | 264 _resultOrListeners = value; |
| 263 } | 265 } |
| 264 | 266 |
| 265 void _setErrorObject(AsyncError error) { | 267 void _setErrorObject(AsyncError error) { |
| 266 assert(!_isComplete); // But may have a completion pending. | 268 assert(!_isComplete); // But may have a completion pending. |
| 267 _state = _ERROR; | 269 _state = _ERROR; |
| 268 _resultOrListeners = error; | 270 _resultOrListeners = error; |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 } | 660 } |
| 659 }, onError: (e, s) { | 661 }, onError: (e, s) { |
| 660 if (timer.isActive) { | 662 if (timer.isActive) { |
| 661 timer.cancel(); | 663 timer.cancel(); |
| 662 result._completeError(e, s); | 664 result._completeError(e, s); |
| 663 } | 665 } |
| 664 }); | 666 }); |
| 665 return result; | 667 return result; |
| 666 } | 668 } |
| 667 } | 669 } |
| OLD | NEW |