Use FutureOr. (#27)
Closes #26
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be8377c..2f62234 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,9 @@
+## 1.13.1
+
+* Use `FutureOr` for various APIs that had previously used `dynamic`.
+
## 1.13.0
-<<<<<<< ours
* Add `collectBytes` and `collectBytesCancelable` functions which collects
list-of-byte events into a single byte list.
@@ -9,11 +12,6 @@
* `StreamQueue.withTransaction()` now properly returns whether or not the
transaction was committed.
-=======
-* Add a `collectBytes` function which collects list-of-byte events into
- a single byte list.
-* Switched to using generic method syntax.
->>>>>>> theirs
## 1.12.0
diff --git a/lib/src/async_memoizer.dart b/lib/src/async_memoizer.dart
index 0213f68..d1c0f32 100644
--- a/lib/src/async_memoizer.dart
+++ b/lib/src/async_memoizer.dart
@@ -39,7 +39,7 @@
/// Runs the function, [computation], if it hasn't been run before.
///
/// If [runOnce] has already been called, this returns the original result.
- Future<T> runOnce(computation()) {
+ Future<T> runOnce(FutureOr<T> computation()) {
if (!hasRun) _completer.complete(new Future.sync(computation));
return future;
}
diff --git a/lib/src/cancelable_operation.dart b/lib/src/cancelable_operation.dart
index d0a1871..a9663c1 100644
--- a/lib/src/cancelable_operation.dart
+++ b/lib/src/cancelable_operation.dart
@@ -6,6 +6,8 @@
import 'package:async/async.dart';
+import 'utils.dart';
+
/// An asynchronous operation that can be cancelled.
///
/// The value of this operation is exposed as [value]. When this operation is
@@ -22,13 +24,13 @@
/// 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].
+ /// or error produced by [inner] will be discarded. If [onCancel] returns a
+ /// [Future], it will be forwarded to [cancel].
///
/// [onCancel] will be called synchronously when the operation is canceled.
/// It's guaranteed to only be called once.
- factory CancelableOperation.fromFuture(Future<T> inner, {onCancel()}) {
+ factory CancelableOperation.fromFuture(Future<T> inner,
+ {FutureOr onCancel()}) {
var completer = new CancelableCompleter<T>(onCancel: onCancel);
completer.complete(inner);
return completer.operation;
@@ -86,17 +88,17 @@
final Completer<T> _inner;
/// The callback to call if the future is canceled.
- final ZoneCallback _onCancel;
+ final FutureOrCallback _onCancel;
/// 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].
+ /// completed, [onCancel] is called. If [onCancel] returns a [Future], it's
+ /// forwarded to [CancelableOperation.cancel].
///
/// [onCancel] will be called synchronously when the operation is canceled.
/// It's guaranteed to only be called once.
- CancelableCompleter({onCancel()})
+ CancelableCompleter({FutureOr onCancel()})
: _onCancel = onCancel,
_inner = new Completer<T>() {
_operation = new CancelableOperation<T>._(this);
diff --git a/lib/src/delegate/future.dart b/lib/src/delegate/future.dart
index 4704343..f746190 100644
--- a/lib/src/delegate/future.dart
+++ b/lib/src/delegate/future.dart
@@ -27,7 +27,7 @@
Future<T> catchError(Function onError, {bool test(Object error)}) =>
_future.catchError(onError, test: test);
- Future<S> then<S>(dynamic onValue(T value), {Function onError}) =>
+ Future<S> then<S>(FutureOr<S> onValue(T value), {Function onError}) =>
_future.then(onValue, onError: onError);
Future<T> whenComplete(action()) => _future.whenComplete(action);
diff --git a/lib/src/lazy_stream.dart b/lib/src/lazy_stream.dart
index ad00a03..d565b4d 100644
--- a/lib/src/lazy_stream.dart
+++ b/lib/src/lazy_stream.dart
@@ -4,8 +4,9 @@
import "dart:async";
-import "stream_completer.dart";
import "delegate/stream.dart";
+import "stream_completer.dart";
+import "utils.dart";
/// A [Stream] wrapper that forwards to another [Stream] that's initialized
/// lazily.
@@ -15,13 +16,11 @@
/// produce a `Stream`.
class LazyStream<T> extends Stream<T> {
/// The callback that's called to create the inner stream.
- ZoneCallback _callback;
+ FutureOrCallback<Stream<T>> _callback;
/// Creates a single-subscription `Stream` that calls [callback] when it gets
/// a listener and forwards to the returned stream.
- ///
- /// The [callback] may return a `Stream` or a `Future<Stream>`.
- LazyStream(callback()) : _callback = callback {
+ LazyStream(FutureOr<Stream<T>> callback()) : _callback = callback {
// Explicitly check for null because we null out [_callback] internally.
if (_callback == null) throw new ArgumentError.notNull('callback');
}
@@ -41,9 +40,9 @@
var result = callback();
Stream<T> stream;
- if (result is Future) {
+ if (result is Future<Stream<T>>) {
stream = StreamCompleter.fromFuture(result.then((stream) {
- return DelegatingStream.typed<T>(stream as Stream);
+ return DelegatingStream.typed<T>(stream);
}));
} else {
stream = DelegatingStream.typed<T>(result as Stream);
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 0066003..2aab40e 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -2,6 +2,11 @@
// 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.
+import 'dart:async';
+
/// A generic typedef for a function that takes one type and returns another.
typedef F UnaryFunction<E, F>(E argument);
+/// A typedef for a function that takes no arguments and returns a Future or a
+/// value.
+typedef FutureOr<T> FutureOrCallback<T>();
diff --git a/pubspec.yaml b/pubspec.yaml
index ea64ad7..c666434 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: async
-version: 1.13.1-dev
+version: 1.13.1
author: Dart Team <misc@dartlang.org>
description: Utility functions and classes related to the 'dart:async' library.
homepage: https://www.github.com/dart-lang/async
@@ -10,4 +10,4 @@
stack_trace: "^1.0.0"
test: "^0.12.0"
environment:
- sdk: ">=1.21.0 <2.0.0"
+ sdk: ">=1.22.0 <2.0.0"