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

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

Issue 91213002: Add Future.timeout. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years 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
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 /** 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 /** Test used by [Future.catchError] to handle skip some errors. */ 9 /** Test used by [Future.catchError] to handle skip some errors. */
10 typedef bool _FutureErrorTest(var error); 10 typedef bool _FutureErrorTest(var error);
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 listener._setValue(listenerValueOrError); 530 listener._setValue(listenerValueOrError);
531 } else { 531 } else {
532 listeners = listener._removeListeners(); 532 listeners = listener._removeListeners();
533 _AsyncError asyncError = listenerValueOrError; 533 _AsyncError asyncError = listenerValueOrError;
534 listener._setError(asyncError.error, asyncError.stackTrace); 534 listener._setError(asyncError.error, asyncError.stackTrace);
535 } 535 }
536 // Prepare for next round. 536 // Prepare for next round.
537 source = listener; 537 source = listener;
538 } 538 }
539 } 539 }
540
541 Future timeout(Duration timeLimit, onTimeout()) {
542 if (_isComplete) return new _Future.immediate(this);
543 Zone zone = Zone.current;
544 onTimeout = zone.registerCallback(onTimeout);
545 _Future result = new _Future();
546 bool completeNormally = true;
547 Timer timer = new Timer(timeLimit, () {
548 completeNormally = false;
549 zone.run(() {
floitsch 2013/11/27 12:31:16 I think it would be nicer if you passed "onTimeout
Lasse Reichstein Nielsen 2013/11/27 12:54:20 Good point. Done.
550 try {
551 result._complete(onTimeout());
552 } catch (e, s) {
553 result._completeError(e, s);
554 }
555 });
556 });
557 this.then((T v) {
558 if (completeNormally) {
559 timer.cancel();
560 result._complete(v);
561 }
562 }, onError: (e, s) {
563 if (completeNormally) {
564 timer.cancel();
565 result._completeError(e, s);
566 }
567 });
568 return result;
569 }
540 } 570 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698