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

Side by Side Diff: pkg/barback/lib/src/cancelable_future.dart

Issue 21226004: Refactor the barback tests somewhat. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: AssetSet additions Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
(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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698