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

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

Issue 807283002: Add examples to illustrate the functionality of CancelableCompleter. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 library cancelable_future; 5 library cancelable_future;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 /** 9 /**
10 * Type of callback called when the future returned by a CancelableCompleter 10 * Type of callback called when the future returned by a CancelableCompleter
11 * is canceled. 11 * is canceled.
12 */ 12 */
13 typedef void CancelHandler(); 13 typedef void CancelHandler();
14 14
15 /** 15 /**
16 * A way to produce [CancelableFuture] objects and to complete them later with 16 * A way to produce [CancelableFuture] objects and to complete them later with
17 * a value or error. 17 * a value or error.
18 * 18 *
19 * This class behaves like the standard library [Completer] class, except that 19 * This class behaves like the standard library [Completer] class, except that
20 * its [future] getter returns a [CancelableFuture]. If the future is 20 * its [future] getter returns a [CancelableFuture].
21 * canceled before being completed, the [CancelHandler] which was passed to 21 *
22 * the constructor is invoked. 22 * If the future is canceled before being completed, the [CancelHandler] which
23 * was passed to the constructor is invoked, and any further attempt to
24 * complete the future has no effect. For example, in the following code:
25 *
26 * main() {
27 * var cc = new CancelableCompleter(() {
28 * print('cancelled'); // (2)
29 * });
30 * cc.future.then((value) {
31 * print('completed with value $value');
32 * }, onError: (error) {
33 * print('Completed with error $error'); // (3)
Brian Wilkerson 2014/12/16 22:28:45 nit: 'C' --> 'c'
Paul Berry 2014/12/16 22:33:13 Done.
34 * });
35 * cc.future.cancel(); // (1)
36 * }
37 *
38 * The call at (1) causes (2) to be invoked immediately. (3) will be invoked
39 * later (on a microtask), with an error that is an instance of
40 * [FutureCanceledError].
41 *
42 * Note that since the closure passed to then() is executed on a microtask,
43 * there is a short window of time between the call to [complete] and the
44 * client being informed that the future has completed. During this window,
45 * any attempt to cancel the future will have no effect. For example, in the
46 * following code:
47 *
48 * main() {
49 * var cc = new CancelableCompleter(() {
50 * print('cancelled'); // (3)
51 * });
52 * cc.future.then((value) {
53 * print('completed with value $value'); // (4)
54 * }, onError: (error) {
55 * print('Completed with error $error');
56 * });
57 * cc.complete(100); // (1)
58 * cc.future.cancel(); // (2)
59 * }
60 *
61 * The call at (1) will place the completer in the "completed" state, so the
62 * call at (2) will have no effect (in particular, (3) won't ever execute).
63 * Later, (4) will be invoked on a microtask.
23 */ 64 */
24 class CancelableCompleter<T> implements Completer<T> { 65 class CancelableCompleter<T> implements Completer<T> {
25 /** 66 /**
26 * The completer which holds the state of the computation. If the 67 * The completer which holds the state of the computation. If the
27 * computation is canceled, this completer will remain in the non-completed 68 * computation is canceled, this completer will remain in the non-completed
28 * state. 69 * state.
29 */ 70 */
30 final Completer<T> _innerCompleter = new Completer<T>.sync(); 71 final Completer<T> _innerCompleter = new Completer<T>.sync();
31 72
32 /** 73 /**
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 Future then(onValue(value), {Function onError}) => 293 Future then(onValue(value), {Function onError}) =>
253 _future.then(onValue, onError: onError); 294 _future.then(onValue, onError: onError);
254 295
255 @override 296 @override
256 Future timeout(Duration timeLimit, {onTimeout()}) => 297 Future timeout(Duration timeLimit, {onTimeout()}) =>
257 _future.timeout(timeLimit, onTimeout: onTimeout); 298 _future.timeout(timeLimit, onTimeout: onTimeout);
258 299
259 @override 300 @override
260 Future whenComplete(action()) => _future.whenComplete(action); 301 Future whenComplete(action()) => _future.whenComplete(action);
261 } 302 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698