OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library async.cancelable_operation; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:async/async.dart'; |
| 10 |
| 11 /// An asynchronuos operation that can be cancelled. |
| 12 /// |
| 13 /// The value of this operation is exposed as [value]. When this operation is |
| 14 /// cancelled, [value] won't complete either successfully or with an error. If |
| 15 /// [value] has already completed, cancelling the operation does nothing. |
| 16 class CancelableOperation<T> { |
| 17 /// The completer that produced this operation. |
| 18 /// |
| 19 /// This is canceled when [cancel] is called. |
| 20 final CancelableCompleter<T> _completer; |
| 21 |
| 22 CancelableOperation._(this._completer); |
| 23 |
| 24 /// Creates a [CancelableOperation] wrapping [inner]. |
| 25 /// |
| 26 /// When this operation is canceled, [onCancel] will be called and any value |
| 27 /// or error produced by [inner] will be discarded. The callback may return a |
| 28 /// Future to indicate that asynchronous work has to be done to cancel the |
| 29 /// future; this Future will be returned by [cancel]. |
| 30 /// |
| 31 /// [onCancel] will be called synchronously when the operation is canceled. |
| 32 /// It's guaranteed to only be called once. |
| 33 factory CancelableOperation.fromFuture(Future<T> inner, {onCancel()}) { |
| 34 var completer = new CancelableCompleter<T>(onCancel: onCancel); |
| 35 completer.complete(inner); |
| 36 return completer.operation; |
| 37 } |
| 38 |
| 39 /// The value returned by the operation. |
| 40 Future<T> get value => _completer._inner.future; |
| 41 |
| 42 /// Creates a [Stream] containing the result of this operation. |
| 43 /// |
| 44 /// This is like `value.asStream()`, but if a subscription to the stream is |
| 45 /// canceled, this is as well. |
| 46 Stream<T> asStream() { |
| 47 var controller = new StreamController<T>( |
| 48 sync: true, onCancel: _completer._cancel); |
| 49 |
| 50 value.then((value) { |
| 51 controller.add(value); |
| 52 controller.close(); |
| 53 }, onError: (error, stackTrace) { |
| 54 controller.addError(error, stackTrace); |
| 55 controller.close(); |
| 56 }); |
| 57 return controller.stream; |
| 58 } |
| 59 |
| 60 /// Cancels this operation. |
| 61 /// |
| 62 /// This returns the [Future] returned by the [CancelableCompleter]'s |
| 63 /// `onCancel` callback. Unlike [Stream.cancel], it never returns `null`. |
| 64 Future cancel() => _completer._cancel(); |
| 65 } |
| 66 |
| 67 /// A completer for a [CancelableOperation]. |
| 68 class CancelableCompleter<T> { |
| 69 /// The completer for the wrapped future. |
| 70 final Completer<T> _inner; |
| 71 |
| 72 /// The callback to call if the future is canceled. |
| 73 final ZoneCallback _onCancel; |
| 74 |
| 75 /// Creates a new completer for a [CancelableOperation]. |
| 76 /// |
| 77 /// When the future operation canceled, as long as the completer hasn't yet |
| 78 /// completed, [onCancel] is called. The callback may return a [Future]; if |
| 79 /// so, that [Future] is returned by [CancelableOperation.cancel]. |
| 80 /// |
| 81 /// [onCancel] will be called synchronously when the operation is canceled. |
| 82 /// It's guaranteed to only be called once. |
| 83 CancelableCompleter({onCancel()}) |
| 84 : _onCancel = onCancel, |
| 85 _inner = new Completer<T>() { |
| 86 _operation = new CancelableOperation<T>._(this); |
| 87 } |
| 88 |
| 89 /// The operation controlled by this completer. |
| 90 CancelableOperation<T> get operation => _operation; |
| 91 CancelableOperation<T> _operation; |
| 92 |
| 93 /// Whether the completer has completed. |
| 94 bool get isCompleted => _isCompleted; |
| 95 bool _isCompleted = false; |
| 96 |
| 97 /// Whether the completer was canceled before being completed. |
| 98 bool get isCanceled => _isCanceled; |
| 99 bool _isCanceled = false; |
| 100 |
| 101 /// The memoizer for [_cancel]. |
| 102 final _cancelMemo = new AsyncMemoizer(); |
| 103 |
| 104 /// Completes [operation] to [value]. |
| 105 /// |
| 106 /// If [value] is a [Future], this will complete to the result of that |
| 107 /// [Future] once it completes. |
| 108 void complete([value]) { |
| 109 if (_isCompleted) throw new StateError("Operation already completed"); |
| 110 _isCompleted = true; |
| 111 |
| 112 if (value is! Future) { |
| 113 if (_isCanceled) return; |
| 114 _inner.complete(value); |
| 115 return; |
| 116 } |
| 117 |
| 118 if (_isCanceled) { |
| 119 // Make sure errors from [value] aren't top-leveled. |
| 120 value.catchError((_) {}); |
| 121 return; |
| 122 } |
| 123 |
| 124 value.then((result) { |
| 125 if (_isCanceled) return; |
| 126 _inner.complete(result); |
| 127 }, onError: (error, stackTrace) { |
| 128 if (_isCanceled) return; |
| 129 _inner.completeError(error, stackTrace); |
| 130 }); |
| 131 } |
| 132 |
| 133 /// Completes [operation] to [error]. |
| 134 void completeError(Object error, [StackTrace stackTrace]) { |
| 135 if (_isCompleted) throw new StateError("Operation already completed"); |
| 136 _isCompleted = true; |
| 137 |
| 138 if (_isCanceled) return; |
| 139 _inner.completeError(error, stackTrace); |
| 140 } |
| 141 |
| 142 /// Cancel the completer. |
| 143 Future _cancel() => _cancelMemo.runOnce(() { |
| 144 if (_inner.isCompleted) return null; |
| 145 _isCanceled = true; |
| 146 if (_onCancel != null) return _onCancel(); |
| 147 }); |
| 148 } |
OLD | NEW |