| 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. | |
| 179 _Future(); | 178 _Future(); |
| 180 | 179 |
| 181 /// Valid types for value: `T` or `Future<T>`. | 180 /// Valid types for value: `T` or `Future<T>`. |
| 182 _Future.immediate(value) { | 181 _Future.immediate(value) { |
| 183 _asyncComplete(value); | 182 _asyncComplete(value); |
| 184 } | 183 } |
| 185 | 184 |
| 186 _Future.immediateError(var error, [StackTrace stackTrace]) { | 185 _Future.immediateError(var error, [StackTrace stackTrace]) { |
| 187 _asyncCompleteError(error, stackTrace); | 186 _asyncCompleteError(error, stackTrace); |
| 188 } | 187 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 207 Zone currentZone = Zone.current; | 206 Zone currentZone = Zone.current; |
| 208 if (!identical(currentZone, _ROOT_ZONE)) { | 207 if (!identical(currentZone, _ROOT_ZONE)) { |
| 209 f = currentZone.registerUnaryCallback(f); | 208 f = currentZone.registerUnaryCallback(f); |
| 210 if (onError != null) { | 209 if (onError != null) { |
| 211 onError = _registerErrorHandler(onError, currentZone); | 210 onError = _registerErrorHandler(onError, currentZone); |
| 212 } | 211 } |
| 213 } | 212 } |
| 214 return _thenNoZoneRegistration(f, onError); | 213 return _thenNoZoneRegistration(f, onError); |
| 215 } | 214 } |
| 216 | 215 |
| 217 // This method is used by async/await. | 216 // This function is used by async/await. |
| 218 Future _thenNoZoneRegistration(f(T value), Function onError) { | 217 Future _thenNoZoneRegistration(f(T value), Function onError) { |
| 219 _Future result = new _Future(); | 218 _Future result = new _Future(); |
| 220 _addListener(new _FutureListener.then(result, f, onError)); | 219 _addListener(new _FutureListener.then(result, f, onError)); |
| 221 return result; | 220 return result; |
| 222 } | 221 } |
| 223 | 222 |
| 224 Future catchError(Function onError, { bool test(error) }) { | 223 Future catchError(Function onError, { bool test(error) }) { |
| 225 _Future result = new _Future(); | 224 _Future result = new _Future(); |
| 226 if (!identical(result._zone, _ROOT_ZONE)) { | 225 if (!identical(result._zone, _ROOT_ZONE)) { |
| 227 onError = _registerErrorHandler(onError, result._zone); | 226 onError = _registerErrorHandler(onError, result._zone); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 250 T get _value { | 249 T get _value { |
| 251 assert(_isComplete && _hasValue); | 250 assert(_isComplete && _hasValue); |
| 252 return _resultOrListeners; | 251 return _resultOrListeners; |
| 253 } | 252 } |
| 254 | 253 |
| 255 AsyncError get _error { | 254 AsyncError get _error { |
| 256 assert(_isComplete && _hasError); | 255 assert(_isComplete && _hasError); |
| 257 return _resultOrListeners; | 256 return _resultOrListeners; |
| 258 } | 257 } |
| 259 | 258 |
| 260 // This method is used by async/await. | |
| 261 void _setValue(T value) { | 259 void _setValue(T value) { |
| 262 assert(!_isComplete); // But may have a completion pending. | 260 assert(!_isComplete); // But may have a completion pending. |
| 263 _state = _VALUE; | 261 _state = _VALUE; |
| 264 _resultOrListeners = value; | 262 _resultOrListeners = value; |
| 265 } | 263 } |
| 266 | 264 |
| 267 void _setErrorObject(AsyncError error) { | 265 void _setErrorObject(AsyncError error) { |
| 268 assert(!_isComplete); // But may have a completion pending. | 266 assert(!_isComplete); // But may have a completion pending. |
| 269 _state = _ERROR; | 267 _state = _ERROR; |
| 270 _resultOrListeners = error; | 268 _resultOrListeners = error; |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 } | 658 } |
| 661 }, onError: (e, s) { | 659 }, onError: (e, s) { |
| 662 if (timer.isActive) { | 660 if (timer.isActive) { |
| 663 timer.cancel(); | 661 timer.cancel(); |
| 664 result._completeError(e, s); | 662 result._completeError(e, s); |
| 665 } | 663 } |
| 666 }); | 664 }); |
| 667 return result; | 665 return result; |
| 668 } | 666 } |
| 669 } | 667 } |
| OLD | NEW |