OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:async/async.dart'; | 7 import 'package:async/async.dart'; |
8 | 8 |
9 /// An asynchronous operation that can be cancelled. | 9 /// An asynchronous operation that can be cancelled. |
10 /// | 10 /// |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 return controller.stream; | 55 return controller.stream; |
56 } | 56 } |
57 | 57 |
58 /// Creates a [Future] that completes when this operation completes *or* when | 58 /// Creates a [Future] that completes when this operation completes *or* when |
59 /// it's cancelled. | 59 /// it's cancelled. |
60 /// | 60 /// |
61 /// If this operation completes, this completes to the same result as [value]. | 61 /// If this operation completes, this completes to the same result as [value]. |
62 /// If this operation is cancelled, the returned future waits for the future | 62 /// If this operation is cancelled, the returned future waits for the future |
63 /// returned by [cancel], then completes to [cancellationValue]. | 63 /// returned by [cancel], then completes to [cancellationValue]. |
64 Future valueOrCancellation([T cancellationValue]) { | 64 Future valueOrCancellation([T cancellationValue]) { |
65 var completer = new Completer.sync(); | 65 var completer = new Completer<T>.sync(); |
66 | 66 value.then((result) => completer.complete(result), |
67 value.then(completer.complete, onError: completer.completeError); | 67 onError: completer.completeError); |
68 | 68 |
69 _completer._cancelMemo.future.then((_) { | 69 _completer._cancelMemo.future.then((_) { |
70 completer.complete(cancellationValue); | 70 completer.complete(cancellationValue); |
71 }, onError: completer.completeError); | 71 }, onError: completer.completeError); |
72 | 72 |
73 return completer.future; | 73 return completer.future; |
74 } | 74 } |
75 | 75 |
76 /// Cancels this operation. | 76 /// Cancels this operation. |
77 /// | 77 /// |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 /// Cancel the completer. | 158 /// Cancel the completer. |
159 Future _cancel() { | 159 Future _cancel() { |
160 if (_inner.isCompleted) return new Future.value(); | 160 if (_inner.isCompleted) return new Future.value(); |
161 | 161 |
162 return _cancelMemo.runOnce(() { | 162 return _cancelMemo.runOnce(() { |
163 _isCanceled = true; | 163 _isCanceled = true; |
164 if (_onCancel != null) return _onCancel(); | 164 if (_onCancel != null) return _onCancel(); |
165 }); | 165 }); |
166 } | 166 } |
167 } | 167 } |
OLD | NEW |