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

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

Issue 2762953003: More minor type improvements in dart:async. (Closed)
Patch Set: More fixes. Created 3 years, 9 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 | « sdk/lib/async/broadcast_stream_controller.dart ('k') | sdk/lib/async/future_impl.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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 * If [computation] is omitted, 254 * If [computation] is omitted,
255 * it will be treated as if [computation] was set to `() => null`, 255 * it will be treated as if [computation] was set to `() => null`,
256 * and the future will eventually complete with the `null` value. 256 * and the future will eventually complete with the `null` value.
257 * 257 *
258 * If calling [computation] throws, the created future will complete with the 258 * If calling [computation] throws, the created future will complete with the
259 * error. 259 * error.
260 * 260 *
261 * See also [Completer] for a way to create and complete a future at a 261 * See also [Completer] for a way to create and complete a future at a
262 * later time that isn't necessarily after a known fixed duration. 262 * later time that isn't necessarily after a known fixed duration.
263 */ 263 */
264 factory Future.delayed(Duration duration, [computation()]) { 264 factory Future.delayed(Duration duration, [FutureOr<T> computation()]) {
265 _Future<T> result = new _Future<T>(); 265 _Future<T> result = new _Future<T>();
266 new Timer(duration, () { 266 new Timer(duration, () {
267 try { 267 try {
268 result._complete(computation?.call()); 268 result._complete(computation?.call());
269 } catch (e, s) { 269 } catch (e, s) {
270 _completeWithErrorCallback(result, e, s); 270 _completeWithErrorCallback(result, e, s);
271 } 271 }
272 }); 272 });
273 return result; 273 return result;
274 } 274 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 * Runs [f] for each element in [input] in order, moving to the next element 415 * Runs [f] for each element in [input] in order, moving to the next element
416 * only when the [Future] returned by [f] completes. Returns a [Future] that 416 * only when the [Future] returned by [f] completes. Returns a [Future] that
417 * completes when all elements have been processed. 417 * completes when all elements have been processed.
418 * 418 *
419 * The return values of all [Future]s are discarded. Any errors will cause the 419 * The return values of all [Future]s are discarded. Any errors will cause the
420 * iteration to stop and will be piped through the returned [Future]. 420 * iteration to stop and will be piped through the returned [Future].
421 * 421 *
422 * If [f] returns a non-[Future], iteration continues immediately. Otherwise 422 * If [f] returns a non-[Future], iteration continues immediately. Otherwise
423 * it waits for the returned [Future] to complete. 423 * it waits for the returned [Future] to complete.
424 */ 424 */
425 static Future forEach<T>(Iterable<T> input, f(T element)) { 425 static Future forEach<T>(Iterable<T> input, dynamic f(T element)) {
426 var iterator = input.iterator; 426 var iterator = input.iterator;
427 return doWhile(() { 427 return doWhile(() {
428 if (!iterator.moveNext()) return false; 428 if (!iterator.moveNext()) return false;
429 return new Future.sync(() => f(iterator.current)).then((_) => true); 429 return new Future.sync(() => f(iterator.current)).then((_) => true);
430 }); 430 });
431 } 431 }
432 432
433 /** 433 /**
434 * Performs an async operation repeatedly until it returns `false`. 434 * Performs an async operation repeatedly until it returns `false`.
435 * 435 *
436 * The function [f] is called repeatedly while it returns either the [bool] 436 * The function [f] is called repeatedly while it returns either the [bool]
437 * value `true` or a [Future] which completes with the value `true`. 437 * value `true` or a [Future] which completes with the value `true`.
438 * 438 *
439 * If a call to [f] returns `false` or a [Future] that completes to `false`, 439 * If a call to [f] returns `false` or a [Future] that completes to `false`,
440 * iteration ends and the future returned by [doWhile] is completed. 440 * iteration ends and the future returned by [doWhile] is completed.
441 * 441 *
442 * If a future returned by [f] completes with an error, iteration ends and 442 * If a future returned by [f] completes with an error, iteration ends and
443 * the future returned by [doWhile] completes with the same error. 443 * the future returned by [doWhile] completes with the same error.
444 * 444 *
445 * The [f] function must return either a `bool` value or a [Future] completing 445 * The [f] function must return either a `bool` value or a [Future] completing
446 * with a `bool` value. 446 * with a `bool` value.
447 */ 447 */
448 static Future doWhile(f()) { 448 static Future doWhile(FutureOr<bool> f()) {
449 _Future doneSignal = new _Future(); 449 _Future doneSignal = new _Future();
450 var nextIteration; 450 var nextIteration;
451 // Bind this callback explicitly so that each iteration isn't bound in the 451 // Bind this callback explicitly so that each iteration isn't bound in the
452 // context of all the previous iterations' callbacks. 452 // context of all the previous iterations' callbacks.
453 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { 453 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) {
454 if (keepGoing) { 454 if (keepGoing) {
455 new Future.sync(f) 455 new Future.sync(f)
456 .then(nextIteration, onError: doneSignal._completeError); 456 .then(nextIteration, onError: doneSignal._completeError);
457 } else { 457 } else {
458 doneSignal._complete(null); 458 doneSignal._complete(null);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 * var f2 = action(); 592 * var f2 = action();
593 * if (f2 is Future) return f2.then((_) => v); 593 * if (f2 is Future) return f2.then((_) => v);
594 * return v 594 * return v
595 * }, onError: (e) { 595 * }, onError: (e) {
596 * var f2 = action(); 596 * var f2 = action();
597 * if (f2 is Future) return f2.then((_) { throw e; }); 597 * if (f2 is Future) return f2.then((_) { throw e; });
598 * throw e; 598 * throw e;
599 * }); 599 * });
600 * } 600 * }
601 */ 601 */
602 Future<T> whenComplete(action()); 602 Future<T> whenComplete(dynamic action());
603 603
604 /** 604 /**
605 * Creates a [Stream] containing the result of this future. 605 * Creates a [Stream] containing the result of this future.
606 * 606 *
607 * The stream will produce single data or error event containing the 607 * The stream will produce single data or error event containing the
608 * completion result of this future, and then it will close with a 608 * completion result of this future, and then it will close with a
609 * done event. 609 * done event.
610 * 610 *
611 * If the future never completes, the stream will not produce any events. 611 * If the future never completes, the stream will not produce any events.
612 */ 612 */
613 Stream<T> asStream(); 613 Stream<T> asStream();
614 614
615 /** 615 /**
616 * Time-out the future computation after [timeLimit] has passed. 616 * Time-out the future computation after [timeLimit] has passed.
617 * 617 *
618 * Returns a new future that completes with the same value as this future, 618 * Returns a new future that completes with the same value as this future,
619 * if this future completes in time. 619 * if this future completes in time.
620 * 620 *
621 * If this future does not complete before `timeLimit` has passed, 621 * If this future does not complete before `timeLimit` has passed,
622 * the [onTimeout] action is executed instead, and its result (whether it 622 * the [onTimeout] action is executed instead, and its result (whether it
623 * returns or throws) is used as the result of the returned future. 623 * returns or throws) is used as the result of the returned future.
624 * The [onTimeout] function must return a [T] or a `Future<T>`. 624 * The [onTimeout] function must return a [T] or a `Future<T>`.
625 * 625 *
626 * If `onTimeout` is omitted, a timeout will cause the returned future to 626 * If `onTimeout` is omitted, a timeout will cause the returned future to
627 * complete with a [TimeoutException]. 627 * complete with a [TimeoutException].
628 */ 628 */
629 Future<T> timeout(Duration timeLimit, {onTimeout()}); 629 Future<T> timeout(Duration timeLimit, {FutureOr<T> onTimeout()});
630 } 630 }
631 631
632 /** 632 /**
633 * Thrown when a scheduled timeout happens while waiting for an async result. 633 * Thrown when a scheduled timeout happens while waiting for an async result.
634 */ 634 */
635 class TimeoutException implements Exception { 635 class TimeoutException implements Exception {
636 /** Description of the cause of the timeout. */ 636 /** Description of the cause of the timeout. */
637 final String message; 637 final String message;
638 /** The duration that was exceeded. */ 638 /** The duration that was exceeded. */
639 final Duration duration; 639 final Duration duration;
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); 819 AsyncError replacement = Zone.current.errorCallback(error, stackTrace);
820 if (replacement != null) { 820 if (replacement != null) {
821 error = _nonNullError(replacement.error); 821 error = _nonNullError(replacement.error);
822 stackTrace = replacement.stackTrace; 822 stackTrace = replacement.stackTrace;
823 } 823 }
824 result._completeError(error, stackTrace); 824 result._completeError(error, stackTrace);
825 } 825 }
826 826
827 /** Helper function that converts `null` to a [NullThrownError]. */ 827 /** Helper function that converts `null` to a [NullThrownError]. */
828 Object _nonNullError(Object error) => error ?? new NullThrownError(); 828 Object _nonNullError(Object error) => error ?? new NullThrownError();
OLDNEW
« no previous file with comments | « sdk/lib/async/broadcast_stream_controller.dart ('k') | sdk/lib/async/future_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698