| 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 typedef dynamic _FutureOnError(error); | 9 typedef dynamic _FutureOnError(error); |
| 10 /** Test used by [Future.catchError] to handle skip some errors. */ | 10 /** Test used by [Future.catchError] to handle skip some errors. */ |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 void _setError(Object error) { | 217 void _setError(Object error) { |
| 218 assert(!_isComplete); // But may have a completion pending. | 218 assert(!_isComplete); // But may have a completion pending. |
| 219 _state = _ERROR; | 219 _state = _ERROR; |
| 220 _resultOrListeners = error; | 220 _resultOrListeners = error; |
| 221 } | 221 } |
| 222 | 222 |
| 223 void _addListener(_Future listener) { | 223 void _addListener(_Future listener) { |
| 224 assert(listener._nextListener == null); | 224 assert(listener._nextListener == null); |
| 225 if (_isComplete) { | 225 if (_isComplete) { |
| 226 // Handle late listeners asynchronously. | 226 // Handle late listeners asynchronously. |
| 227 runAsync(() { | 227 _zone.runAsync(() { |
| 228 _propagateToListeners(this, listener); | 228 _propagateToListeners(this, listener); |
| 229 }); | 229 }, _zone); |
| 230 } else { | 230 } else { |
| 231 listener._nextListener = _resultOrListeners; | 231 listener._nextListener = _resultOrListeners; |
| 232 _resultOrListeners = listener; | 232 _resultOrListeners = listener; |
| 233 } | 233 } |
| 234 } | 234 } |
| 235 | 235 |
| 236 _Future _removeListeners() { | 236 _Future _removeListeners() { |
| 237 // Reverse listeners before returning them, so the resulting list is in | 237 // Reverse listeners before returning them, so the resulting list is in |
| 238 // subscription order. | 238 // subscription order. |
| 239 assert(!_isComplete); | 239 assert(!_isComplete); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 (value is! _Future || !(value as _Future)._isComplete)) { | 334 (value is! _Future || !(value as _Future)._isComplete)) { |
| 335 // Case 2 from above. We need to register. | 335 // Case 2 from above. We need to register. |
| 336 // Note that we are still completing asynchronously: either we register | 336 // Note that we are still completing asynchronously: either we register |
| 337 // through .then (in which case the completing is asynchronous), or we | 337 // through .then (in which case the completing is asynchronous), or we |
| 338 // have a _Future which isn't complete yet. | 338 // have a _Future which isn't complete yet. |
| 339 _complete(value); | 339 _complete(value); |
| 340 return; | 340 return; |
| 341 } | 341 } |
| 342 | 342 |
| 343 _markPendingCompletion(); | 343 _markPendingCompletion(); |
| 344 runAsync(() { | 344 _zone.runAsync(() { _complete(value); }, _zone); |
| 345 _complete(value); | |
| 346 }); | |
| 347 } | 345 } |
| 348 | 346 |
| 349 void _asyncCompleteError(error, [StackTrace stackTrace]) { | 347 void _asyncCompleteError(error, [StackTrace stackTrace]) { |
| 350 assert(!_isComplete); | 348 assert(!_isComplete); |
| 351 assert(_onValue == null); | 349 assert(_onValue == null); |
| 352 assert(_onError == null); | 350 assert(_onError == null); |
| 353 assert(_whenCompleteAction == null); | 351 assert(_whenCompleteAction == null); |
| 354 assert(_errorTest == null); | 352 assert(_errorTest == null); |
| 355 | 353 |
| 356 _markPendingCompletion(); | 354 _markPendingCompletion(); |
| 357 runAsync(() { | 355 _zone.runAsync(() { _completeError(error, stackTrace); }, _zone); |
| 358 _completeError(error, stackTrace); | |
| 359 }); | |
| 360 } | 356 } |
| 361 | 357 |
| 362 /** | 358 /** |
| 363 * Propagates the value/error of [source] to its [listeners]. | 359 * Propagates the value/error of [source] to its [listeners]. |
| 364 * | 360 * |
| 365 * Unlinks all listeners and propagates the source to each listener | 361 * Unlinks all listeners and propagates the source to each listener |
| 366 * separately. | 362 * separately. |
| 367 */ | 363 */ |
| 368 static void _propagateMultipleListeners(_Future source, _Future listeners) { | 364 static void _propagateMultipleListeners(_Future source, _Future listeners) { |
| 369 assert(listeners != null); | 365 assert(listeners != null); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 listener._setValue(listenerValueOrError); | 511 listener._setValue(listenerValueOrError); |
| 516 } else { | 512 } else { |
| 517 listeners = listener._removeListeners(); | 513 listeners = listener._removeListeners(); |
| 518 listener._setError(listenerValueOrError); | 514 listener._setError(listenerValueOrError); |
| 519 } | 515 } |
| 520 // Prepare for next round. | 516 // Prepare for next round. |
| 521 source = listener; | 517 source = listener; |
| 522 } | 518 } |
| 523 } | 519 } |
| 524 } | 520 } |
| OLD | NEW |