Skip to content

Commit

Permalink
Use FutureOr. (dart-archive/async#27)
Browse files Browse the repository at this point in the history
Closes dart-lang/async#26
  • Loading branch information
nex3 authored Feb 15, 2017
1 parent 3f264da commit ce17710
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
10 changes: 4 additions & 6 deletions pkgs/async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkgs/async/lib/src/async_memoizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AsyncMemoizer<T> {
/// 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;
}
Expand Down
18 changes: 10 additions & 8 deletions pkgs/async/lib/src/cancelable_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'dart:async';

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
Expand All @@ -22,13 +24,13 @@ class CancelableOperation<T> {
/// 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;
Expand Down Expand Up @@ -86,17 +88,17 @@ class CancelableCompleter<T> {
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);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/async/lib/src/delegate/future.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DelegatingFuture<T> implements Future<T> {
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);
Expand Down
13 changes: 6 additions & 7 deletions pkgs/async/lib/src/lazy_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,13 +16,11 @@ import "delegate/stream.dart";
/// 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');
}
Expand All @@ -41,9 +40,9 @@ class LazyStream<T> extends Stream<T> {
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);
Expand Down
5 changes: 5 additions & 0 deletions pkgs/async/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
4 changes: 2 additions & 2 deletions pkgs/async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,4 +10,4 @@ dev_dependencies:
stack_trace: "^1.0.0"
test: "^0.12.0"
environment:
sdk: ">=1.21.0 <2.0.0"
sdk: ">=1.22.0 <2.0.0"

0 comments on commit ce17710

Please sign in to comment.