Chromium Code Reviews| Index: lib/src/cancelable_operation.dart |
| diff --git a/lib/src/cancelable_operation.dart b/lib/src/cancelable_operation.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8095ca22e3ad5a064ed34b2215b465400244a595 |
| --- /dev/null |
| +++ b/lib/src/cancelable_operation.dart |
| @@ -0,0 +1,148 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library async.cancelable_operation; |
| + |
| +import 'dart:async'; |
| + |
| +import 'package:async/async.dart'; |
| + |
| +/// An asynchronuos operation that can be cancelled. |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
So basically, you have the result of an async oper
nweiz
2015/08/28 00:34:56
All this is true, although CancelableCompleter is
|
| +/// |
| +/// The value of this operation is exposed as [value]. When this operation is |
| +/// cancelled, [value] won't complete either successfully or with an error. If |
| +/// [value] has already completed, cancelling the operation does nothing. |
| +class CancelableOperation<T> { |
| + /// The completer that produced this operation. |
| + /// |
| + /// This is canceled when [cancel] is called. |
| + final CancelableCompleter<T> _completer; |
| + |
| + Future<T> get value => _completer._inner.future; |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:53
I'd call this "result" since it may be an error. W
Lasse Reichstein Nielsen
2015/08/27 10:20:54
Move getter below constructor.
nweiz
2015/08/28 00:34:56
"result" seems confusing when the package has a "R
nweiz
2015/08/28 00:34:56
Done, although this is contrary to the style most
Lasse Reichstein Nielsen
2015/09/03 13:06:17
The order in the file is independent of how the AP
|
| + |
| + /// Whether this operation has been canceled. |
| + bool _canceled = false; |
| + |
| + CancelableOperation._(this._completer); |
|
Lasse Reichstein Nielsen
2015/08/27 10:57:41
I think one thing that makes it hard for me to wra
Lasse Reichstein Nielsen
2015/08/27 10:59:47
And it is, the other one was the cancelable comple
nweiz
2015/08/28 00:34:55
That's true, but I expect exposing operations crea
|
| + |
| + /// Creates a [CancelableOperation] wrapping [inner]. |
| + /// |
| + /// When this operation is canceled, [onCancel] will be called and any value |
| + /// or error produced by [inner] will be discarded. The callback may return a |
| + /// Future to indicate that asynchronous work has to be done to cancel the |
| + /// future; this Future will be returned by [cancel]. |
| + factory CancelableOperation.fromFuture(Future<T> inner, {onCancel()}) { |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
Why is onCancel optional? If it's null, I don't se
nweiz
2015/08/28 00:34:56
From the user's perspective, canceling an operatio
Lasse Reichstein Nielsen
2015/09/03 13:06:17
Ack, makes sense. Cancel only cancels the "operati
|
| + var completer = new CancelableCompleter<T>(onCancel: onCancel); |
| + completer.complete(inner); |
| + return completer.operation; |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
You are passing a callback here, which is not goin
nweiz
2015/08/28 00:34:56
I don't think it's very likely that multiple dispa
Lasse Reichstein Nielsen
2015/09/03 13:06:17
True. It has to be documented when the callback wi
nweiz
2015/09/08 21:14:45
Done.
|
| + } |
| + |
| + /// Creates a [Stream] containing the result of this operation. |
| + /// |
| + /// This is like `value.asStream()`, but if a subscription to the stream is |
| + /// canceled, this is as well. |
| + Stream<T> asStream() { |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
I prefer to make this a getter.
nweiz
2015/08/28 00:34:56
I think it's more important for it to be consisten
Lasse Reichstein Nielsen
2015/09/03 13:06:17
Ok, that's fair.
|
| + var controller = new StreamController<T>( |
| + sync: true, onCancel: _completer._cancel); |
| + |
| + value.then((value) { |
| + controller.add(value); |
| + controller.close(); |
| + }, onError: (error, stackTrace) { |
| + controller.addError(error, stackTrace); |
| + controller.close(); |
| + }); |
| + return controller.stream; |
| + } |
| + |
| + /// Cancels this operation. |
| + /// |
| + /// This returns the [Future] returned by the [CancelableCompleter]'s |
| + /// `onCancel` callback. Unlike [Stream.cancel], it never returns `null`. |
| + Future cancel() { |
| + _canceled = true; |
|
Lasse Reichstein Nielsen
2015/09/03 13:06:17
Maybe drop the _canceled field in this class and j
nweiz
2015/09/08 21:14:45
We don't actually use this field anyway, it turns
|
| + return _completer._cancel(); |
| + } |
| +} |
| + |
| +/// A completer for a [CancelableOperation]. |
| +class CancelableCompleter<T> { |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
I don't think you need this class.
Do you *want* i
nweiz
2015/08/28 00:34:56
Yeah; see above comments.
|
| + /// The completer for the wrapped future. |
| + final Completer<T> _inner; |
| + |
| + /// The callback to call if the future is canceled. |
| + final ZoneCallback _onCancel; |
| + |
| + CancelableOperation<T> get operation => _operation; |
| + CancelableOperation<T> _operation; |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:53
I find it very hard to see which fields the class
nweiz
2015/08/28 00:34:56
Done, although see above.
|
| + |
| + /// Whether the completer has completed. |
| + bool get isCompleted => _isCompleted; |
| + bool _isCompleted = false; |
| + |
| + /// Whether the completer was canceled before being completed. |
| + bool get isCanceled => _isCanceled; |
| + bool _isCanceled = false; |
| + |
| + /// Whether the completer has fired. |
| + /// |
| + /// This is distinct from [isCompleted] when a [Future] is passed to |
| + /// [complete]; this won't be `true` until that [Future] fires. |
| + bool _fired = false; |
|
Lasse Reichstein Nielsen
2015/09/03 13:06:17
I think _fired is equivalent to _inner.isCompleted
nweiz
2015/09/08 21:14:45
Done.
|
| + |
| + /// Creates a new completer for a [CancelableOperation]. |
| + /// |
| + /// When the future operation canceled, as long as the completer hasn't yet |
| + /// completed, [onCancel] is called. The callback may return a [Future]; if |
| + /// so, that [Future] is returned by [CancelableOperation.cancel]. |
| + CancelableCompleter({onCancel()}) |
| + : _onCancel = onCancel, |
| + _inner = new Completer<T>() { |
| + _operation = new CancelableOperation<T>._(this); |
| + } |
| + |
| + /// Completes [operation] to [value]. |
| + /// |
| + /// If [value] is a [Future], this will complete to the result of that |
| + /// [Future] once it completes. |
| + void complete([value]) { |
| + if (_isCompleted) throw new StateError("Operation already completed"); |
| + _isCompleted = true; |
| + |
| + if (_isCanceled) return; |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
If value is a future with an error, the error is n
nweiz
2015/08/28 00:34:56
Done.
|
| + if (value is! Future) { |
| + _fired = true; |
| + _inner.complete(value); |
| + return; |
| + } |
| + |
| + value.then((result) { |
| + if (_isCanceled) return; |
| + _fired = true; |
| + _inner.complete(result); |
| + }, onError: (error, stackTrace) { |
| + if (_isCanceled) return; |
| + _fired = true; |
| + _inner.completeError(error, stackTrace); |
| + }); |
| + } |
| + |
| + /// Completes [operation] to [error]. |
| + void completeError(Object error, [StackTrace stackTrace]) { |
| + if (_isCompleted) throw new StateError("Operation already completed"); |
| + _isCompleted = true; |
| + |
| + if (_isCanceled) return; |
| + _fired = true; |
| + _inner.completeError(error, stackTrace); |
| + } |
| + |
| + /// Cancel the completer. |
| + Future _cancel() => _cancelMemo.runOnce(() { |
| + if (_fired) return null; |
| + _isCanceled = true; |
| + if (_onCancel != null) return _onCancel(); |
| + }); |
| + final _cancelMemo = new AsyncMemoizer(); |
|
Lasse Reichstein Nielsen
2015/08/27 10:20:54
Move field to top of class.
nweiz
2015/08/28 00:34:56
I will if you insist, but I think it's a lot clear
Lasse Reichstein Nielsen
2015/09/03 13:06:17
I still prefer to have all the fields collected in
nweiz
2015/09/08 21:14:45
Done.
|
| +} |