| 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 if (value) { | 196 if (value) { |
| 197 assert(!_isComplete); | 197 assert(!_isComplete); |
| 198 _state = _CHAINED; | 198 _state = _CHAINED; |
| 199 } else { | 199 } else { |
| 200 assert(_isChained); | 200 assert(_isChained); |
| 201 _state = _INCOMPLETE; | 201 _state = _INCOMPLETE; |
| 202 } | 202 } |
| 203 } | 203 } |
| 204 | 204 |
| 205 Future then(f(T value), { Function onError }) { | 205 Future then(f(T value), { Function onError }) { |
| 206 _Future result = new _Future(); | 206 Zone currentZone = Zone.current; |
| 207 if (!identical(result._zone, _ROOT_ZONE)) { | 207 if (!identical(currentZone, _ROOT_ZONE)) { |
| 208 f = result._zone.registerUnaryCallback(f); | 208 f = currentZone.registerUnaryCallback(f); |
| 209 if (onError != null) { | 209 if (onError != null) { |
| 210 onError = _registerErrorHandler(onError, result._zone); | 210 onError = _registerErrorHandler(onError, currentZone); |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 return _thenNoZoneRegistration(f, onError); |
| 214 } |
| 215 |
| 216 // This function is used by async/await. |
| 217 Future _thenNoZoneRegistration(f(T value), Function onError) { |
| 218 _Future result = new _Future(); |
| 213 _addListener(new _FutureListener.then(result, f, onError)); | 219 _addListener(new _FutureListener.then(result, f, onError)); |
| 214 return result; | 220 return result; |
| 215 } | 221 } |
| 216 | 222 |
| 217 Future catchError(Function onError, { bool test(error) }) { | 223 Future catchError(Function onError, { bool test(error) }) { |
| 218 _Future result = new _Future(); | 224 _Future result = new _Future(); |
| 219 if (!identical(result._zone, _ROOT_ZONE)) { | 225 if (!identical(result._zone, _ROOT_ZONE)) { |
| 220 onError = _registerErrorHandler(onError, result._zone); | 226 onError = _registerErrorHandler(onError, result._zone); |
| 221 if (test != null) test = result._zone.registerUnaryCallback(test); | 227 if (test != null) test = result._zone.registerUnaryCallback(test); |
| 222 } | 228 } |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 } | 658 } |
| 653 }, onError: (e, s) { | 659 }, onError: (e, s) { |
| 654 if (timer.isActive) { | 660 if (timer.isActive) { |
| 655 timer.cancel(); | 661 timer.cancel(); |
| 656 result._completeError(e, s); | 662 result._completeError(e, s); |
| 657 } | 663 } |
| 658 }); | 664 }); |
| 659 return result; | 665 return result; |
| 660 } | 666 } |
| 661 } | 667 } |
| OLD | NEW |