OLD | NEW |
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 Loading... |
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, [void onTimeout()]) { |
| 542 if (_isComplete) return new _Future.immediate(this); |
| 543 _Future result = new _Future(); |
| 544 Timer timer; |
| 545 if (onTimeout == null) { |
| 546 timer = new Timer(timeLimit, () { |
| 547 result._completeError(new TimeoutException(timeLimit)); |
| 548 }); |
| 549 } else { |
| 550 Zone zone = Zone.current; |
| 551 onTimeout = zone.registerCallback(onTimeout); |
| 552 timer = new Timer(timeLimit, () { |
| 553 try { |
| 554 result._complete(zone.run(onTimeout)); |
| 555 } catch (e, s) { |
| 556 result._completeError(e, s); |
| 557 } |
| 558 }); |
| 559 } |
| 560 this.then((T v) { |
| 561 if (timer.isActive) { |
| 562 timer.cancel(); |
| 563 result._complete(v); |
| 564 } |
| 565 }, onError: (e, s) { |
| 566 if (timer.isActive) { |
| 567 timer.cancel(); |
| 568 result._completeError(e, s); |
| 569 } |
| 570 }); |
| 571 return result; |
| 572 } |
540 } | 573 } |
OLD | NEW |