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

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

Issue 356773005: Add Future.doWhile to dart:async. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 5 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 | Annotate | Revision Log
« 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 /** 7 /**
8 * An object representing a delayed computation. 8 * An object representing a delayed computation.
9 * 9 *
10 * A [Future] is used to represent a potential value, or error, 10 * A [Future] is used to represent a potential value, or error,
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 284
285 /** 285 /**
286 * Perform an async operation for each element of the iterable, in turn. 286 * Perform an async operation for each element of the iterable, in turn.
287 * 287 *
288 * Runs [f] for each element in [input] in order, moving to the next element 288 * Runs [f] for each element in [input] in order, moving to the next element
289 * only when the [Future] returned by [f] completes. Returns a [Future] that 289 * only when the [Future] returned by [f] completes. Returns a [Future] that
290 * completes when all elements have been processed. 290 * completes when all elements have been processed.
291 * 291 *
292 * The return values of all [Future]s are discarded. Any errors will cause the 292 * The return values of all [Future]s are discarded. Any errors will cause the
293 * iteration to stop and will be piped through the returned [Future]. 293 * iteration to stop and will be piped through the returned [Future].
294 *
295 * If [f] returns a non-[Future], iteration continues immediately. Otherwise
296 * it waits for the returned [Future] to complete.
294 */ 297 */
295 static Future forEach(Iterable input, Future f(element)) { 298 static Future forEach(Iterable input, f(element)) {
299 Iterator iterator = input.iterator;
300 return doWhile(() {
301 if (!iterator.moveNext()) return false;
302 return new Future.sync(() => f(iterator.current)).then((_) => true);
303 });
304 }
305
306 /**
307 * Perform an async operation repeatedly until it returns `false`.
308 *
309 * Runs [f] repeatedly, starting the next iteration only when the [Future]
310 * returned by [f] completes to `true`. Returns a [Future] that completes once
311 * [f] returns `false`.
312 *
313 * The return values of all [Future]s are discarded. Any errors will cause the
314 * iteration to stop and will be piped through the returned [Future].
315 *
316 * The function [f] may return either a [bool] or a [Future] that completes to
317 * a [bool]. If it returns a non-[Future], iteration continues immediately.
318 * Otherwise it waits for the returned [Future] to complete.
319 */
320 static Future doWhile(f()) {
296 _Future doneSignal = new _Future(); 321 _Future doneSignal = new _Future();
297 Iterator iterator = input.iterator; 322 var nextIteration;
298 void nextElement(_) { 323 // Bind this callback explicitly so that each iteration isn't bound in the
299 if (iterator.moveNext()) { 324 // context of all the previous iterations' callbacks.
300 new Future.sync(() => f(iterator.current)) 325 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) {
301 .then(nextElement, onError: doneSignal._completeError); 326 if (keepGoing) {
327 new Future.sync(f).then(nextIteration,
328 onError: doneSignal._completeError);
302 } else { 329 } else {
303 doneSignal._complete(null); 330 doneSignal._complete(null);
304 } 331 }
305 } 332 }, runGuarded: true);
306 nextElement(null); 333 nextIteration(true);
307 return doneSignal; 334 return doneSignal;
308 } 335 }
309 336
310 /** 337 /**
311 * Register callbacks to be called when this future completes. 338 * Register callbacks to be called when this future completes.
312 * 339 *
313 * When this future completes with a value, 340 * When this future completes with a value,
314 * the [onValue] callback will be called with that value. 341 * the [onValue] callback will be called with that value.
315 * If this future is already completed, the callback will not be called 342 * If this future is already completed, the callback will not be called
316 * immediately, but will be scheduled in a later microtask. 343 * immediately, but will be scheduled in a later microtask.
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 * theFuture.catchError(thisCompleter.completeError); 642 * theFuture.catchError(thisCompleter.completeError);
616 * 643 *
617 */ 644 */
618 void completeError(Object error, [StackTrace stackTrace]); 645 void completeError(Object error, [StackTrace stackTrace]);
619 646
620 /** 647 /**
621 * Whether the future has been completed. 648 * Whether the future has been completed.
622 */ 649 */
623 bool get isCompleted; 650 bool get isCompleted;
624 } 651 }
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