OLD | NEW |
(Empty) | |
| 1 import 'dart:async'; |
| 2 |
| 3 /// A wrapper for [Future] that can be cancelled. |
| 4 /// |
| 5 /// When this is cancelled, that means it won't complete either successfully or |
| 6 /// with an error, regardless of whether the wrapped Future completes. |
| 7 /// Cancelling this won't stop whatever code is feeding the wrapped future from |
| 8 /// running. |
| 9 class CancelableFuture<T> implements Future<T> { |
| 10 bool _canceled = false; |
| 11 final _completer = new Completer<T>(); |
| 12 |
| 13 CancelableFuture(Future<T> inner) { |
| 14 inner.then((result) { |
| 15 if (_canceled) return; |
| 16 _completer.complete(result); |
| 17 }).catchError((error) { |
| 18 if (_canceled) return; |
| 19 _completer.completeError(error); |
| 20 }); |
| 21 } |
| 22 |
| 23 Stream<T> asStream() => _completer.future.asStream(); |
| 24 Future catchError(onError(asyncError), {bool test(error)}) => |
| 25 _completer.future.catchError(onError, test: test); |
| 26 Future then(onValue(T value), {onError(error)}) => |
| 27 _completer.future.then(onValue, onError: onError); |
| 28 Future<T> whenComplete(action()) => _completer.future.whenComplete(action); |
| 29 |
| 30 /// Cancels this future. |
| 31 void cancel() { |
| 32 _canceled = true; |
| 33 } |
| 34 } |
OLD | NEW |