Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: sdk/lib/async/future.dart

Issue 2853203002: Change Future.doWhile to use a loop for non-future results instead of recursion. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests/lib/async/futures_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 * 477 *
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.
floitsch 2017/05/02 10:16:56 Mention stacktrace package here.
488 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { 488 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) {
489 if (keepGoing) { 489 while (keepGoing) {
490 FutureOr<bool> result; 490 FutureOr<bool> result;
491 try { 491 try {
492 result = f(); 492 result = f();
493 } catch (error, stackTrace) { 493 } catch (error, stackTrace) {
494 // Cannot use _completeWithErrorCallback because it completes 494 // Cannot use _completeWithErrorCallback because it completes
495 // the future synchronously. 495 // the future synchronously.
496 _asyncCompleteWithErrorCallback(doneSignal, error, stackTrace); 496 _asyncCompleteWithErrorCallback(doneSignal, error, stackTrace);
497 return; 497 return;
498 } 498 }
499 if (result is Future<bool>) { 499 if (result is Future<bool>) {
500 result.then(nextIteration, onError: doneSignal._completeError); 500 result.then(nextIteration, onError: doneSignal._completeError);
501 return; 501 return;
502 } 502 }
503 nextIteration(result); 503 keepGoing = result;
504 } else {
505 doneSignal._complete(null);
506 } 504 }
505 doneSignal._complete(null);
507 }, runGuarded: true); 506 }, runGuarded: true);
508 nextIteration(true); 507 nextIteration(true);
509 return doneSignal; 508 return doneSignal;
510 } 509 }
511 510
512 /** 511 /**
513 * Register callbacks to be called when this future completes. 512 * Register callbacks to be called when this future completes.
514 * 513 *
515 * When this future completes with a value, 514 * When this future completes with a value,
516 * the [onValue] callback will be called with that value. 515 * the [onValue] callback will be called with that value.
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); 885 AsyncError replacement = Zone.current.errorCallback(error, stackTrace);
887 if (replacement != null) { 886 if (replacement != null) {
888 error = _nonNullError(replacement.error); 887 error = _nonNullError(replacement.error);
889 stackTrace = replacement.stackTrace; 888 stackTrace = replacement.stackTrace;
890 } 889 }
891 result._asyncCompleteError(error, stackTrace); 890 result._asyncCompleteError(error, stackTrace);
892 } 891 }
893 892
894 /** Helper function that converts `null` to a [NullThrownError]. */ 893 /** Helper function that converts `null` to a [NullThrownError]. */
895 Object _nonNullError(Object error) => error ?? new NullThrownError(); 894 Object _nonNullError(Object error) => error ?? new NullThrownError();
OLDNEW
« no previous file with comments | « no previous file | tests/lib/async/futures_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698