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 /// A type representing values that are either `Future<T>` or `T`. | 7 /// A type representing values that are either `Future<T>` or `T`. |
8 /// | 8 /// |
9 /// This class declaration is a public stand-in for an internal | 9 /// This class declaration is a public stand-in for an internal |
10 /// future-or-value generic type. References to this class are resolved to the | 10 /// future-or-value generic type. References to this class are resolved to the |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 * Calls to [f] may happen at any time, including immediately after calling | 478 * Calls to [f] may happen at any time, including immediately after calling |
479 * `doWhile`. The only restriction is a new call to [f] won't happen before | 479 * `doWhile`. The only restriction is a new call to [f] won't happen before |
480 * the previous call has returned, and if it returned a `Future<bool>`, not | 480 * the previous call has returned, and if it returned a `Future<bool>`, not |
481 * until that future has completed. | 481 * until that future has completed. |
482 */ | 482 */ |
483 static Future doWhile(FutureOr<bool> f()) { | 483 static Future doWhile(FutureOr<bool> f()) { |
484 _Future doneSignal = new _Future(); | 484 _Future doneSignal = new _Future(); |
485 var nextIteration; | 485 var nextIteration; |
486 // Bind this callback explicitly so that each iteration isn't bound in the | 486 // Bind this callback explicitly so that each iteration isn't bound in the |
487 // context of all the previous iterations' callbacks. | 487 // context of all the previous iterations' callbacks. |
| 488 // This avoids, e.g., deeply nested stack traces from the stack trace |
| 489 // package. |
488 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { | 490 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { |
489 if (keepGoing) { | 491 while (keepGoing) { |
490 FutureOr<bool> result; | 492 FutureOr<bool> result; |
491 try { | 493 try { |
492 result = f(); | 494 result = f(); |
493 } catch (error, stackTrace) { | 495 } catch (error, stackTrace) { |
494 // Cannot use _completeWithErrorCallback because it completes | 496 // Cannot use _completeWithErrorCallback because it completes |
495 // the future synchronously. | 497 // the future synchronously. |
496 _asyncCompleteWithErrorCallback(doneSignal, error, stackTrace); | 498 _asyncCompleteWithErrorCallback(doneSignal, error, stackTrace); |
497 return; | 499 return; |
498 } | 500 } |
499 if (result is Future<bool>) { | 501 if (result is Future<bool>) { |
500 result.then(nextIteration, onError: doneSignal._completeError); | 502 result.then(nextIteration, onError: doneSignal._completeError); |
501 return; | 503 return; |
502 } | 504 } |
503 nextIteration(result); | 505 keepGoing = result; |
504 } else { | |
505 doneSignal._complete(null); | |
506 } | 506 } |
| 507 doneSignal._complete(null); |
507 }, runGuarded: true); | 508 }, runGuarded: true); |
508 nextIteration(true); | 509 nextIteration(true); |
509 return doneSignal; | 510 return doneSignal; |
510 } | 511 } |
511 | 512 |
512 /** | 513 /** |
513 * Register callbacks to be called when this future completes. | 514 * Register callbacks to be called when this future completes. |
514 * | 515 * |
515 * When this future completes with a value, | 516 * When this future completes with a value, |
516 * the [onValue] callback will be called with that value. | 517 * the [onValue] callback will be called with that value. |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); | 887 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); |
887 if (replacement != null) { | 888 if (replacement != null) { |
888 error = _nonNullError(replacement.error); | 889 error = _nonNullError(replacement.error); |
889 stackTrace = replacement.stackTrace; | 890 stackTrace = replacement.stackTrace; |
890 } | 891 } |
891 result._asyncCompleteError(error, stackTrace); | 892 result._asyncCompleteError(error, stackTrace); |
892 } | 893 } |
893 | 894 |
894 /** Helper function that converts `null` to a [NullThrownError]. */ | 895 /** Helper function that converts `null` to a [NullThrownError]. */ |
895 Object _nonNullError(Object error) => error ?? new NullThrownError(); | 896 Object _nonNullError(Object error) => error ?? new NullThrownError(); |
OLD | NEW |