Commit b504fd42 authored by Bob Nystrom's avatar Bob Nystrom Committed by GitHub

Fix covariant overrides in SynchronousFuture. (#5262)

* Fix covariant overrides in SynchronousFuture.

There were two things going on here. In timeout(), the callback's return
type was needlessly tightened to only allow callbacks that return
futures. This makes SynchronousFuture not substitutable with Future,
whose timeout() allows callbacks that return immediate values.

Since SynchronousFuture.timeout() never calls the callback anyway, I
just loosened it to match Future.timeout().

SynchronousFuture.whenComplete() is just wrong. The type error, again,
is that the callback's return type is too tight. Future.whenComplete()
allows synchronous callbacks.

But the actual implementation is wrong as well. whenComplete() should
return a future that completes to the *original value*, not whatever the
callback returns.

So I just fixed the method to work correctly, including handling
callbacks with synchronous results.

* "(error, stackTrace)" -> "(e, stack)".
parent 21ee4b92
......@@ -42,8 +42,17 @@ class SynchronousFuture<T> implements Future<T> {
}
@override
Future<T> timeout(Duration timeLimit, { Future<T> onTimeout() }) => new Completer<T>().future;
Future<T> timeout(Duration timeLimit, { dynamic onTimeout() }) => new Completer<T>().future;
@override
Future<T> whenComplete(Future<T> action()) => action();
Future<T> whenComplete(dynamic action()) {
try {
dynamic result = action();
if (result is Future)
return result.then((_) => _value);
return this;
} catch (e, stack) {
return new Future<T>.error(e, stack);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment