| 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 completer.complete(value); |
| 67 value.then(completer.complete, onError: completer.completeError); | |
| 68 | 67 |
| 69 _completer._cancelMemo.future.then((_) { | 68 _completer._cancelMemo.future.then((_) { |
| 70 completer.complete(cancellationValue); | 69 completer.complete(cancellationValue); |
| 71 }, onError: completer.completeError); | 70 }, onError: completer.completeError); |
| 72 | 71 |
| 73 return completer.future; | 72 return completer.future; |
| 74 } | 73 } |
| 75 | 74 |
| 76 /// Cancels this operation. | 75 /// Cancels this operation. |
| 77 /// | 76 /// |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 /// Cancel the completer. | 157 /// Cancel the completer. |
| 159 Future _cancel() { | 158 Future _cancel() { |
| 160 if (_inner.isCompleted) return new Future.value(); | 159 if (_inner.isCompleted) return new Future.value(); |
| 161 | 160 |
| 162 return _cancelMemo.runOnce(() { | 161 return _cancelMemo.runOnce(() { |
| 163 _isCanceled = true; | 162 _isCanceled = true; |
| 164 if (_onCancel != null) return _onCancel(); | 163 if (_onCancel != null) return _onCancel(); |
| 165 }); | 164 }); |
| 166 } | 165 } |
| 167 } | 166 } |
| OLD | NEW |