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

Unified 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: Update indentation. Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/async/future.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/future_impl.dart
diff --git a/sdk/lib/async/future_impl.dart b/sdk/lib/async/future_impl.dart
index 90e9df2b5033586837a245375526a067e24a7805..fb1280c52c1b5c540057cdab773e4dd29870f84b 100644
--- a/sdk/lib/async/future_impl.dart
+++ b/sdk/lib/async/future_impl.dart
@@ -537,4 +537,37 @@ class _Future<T> implements Future<T> {
source = listener;
}
}
+
+ Future timeout(Duration timeLimit, [void onTimeout()]) {
+ if (_isComplete) return new _Future.immediate(this);
+ _Future result = new _Future();
+ Timer timer;
+ if (onTimeout == null) {
+ timer = new Timer(timeLimit, () {
+ result._completeError(new TimeoutException(timeLimit));
+ });
+ } else {
+ Zone zone = Zone.current;
+ onTimeout = zone.registerCallback(onTimeout);
+ timer = new Timer(timeLimit, () {
+ try {
+ result._complete(zone.run(onTimeout));
+ } catch (e, s) {
+ result._completeError(e, s);
+ }
+ });
+ }
+ this.then((T v) {
+ if (timer.isActive) {
+ timer.cancel();
+ result._complete(v);
+ }
+ }, onError: (e, s) {
+ if (timer.isActive) {
+ timer.cancel();
+ result._completeError(e, s);
+ }
+ });
+ return result;
+ }
}
« no previous file with comments | « sdk/lib/async/future.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698