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)".
This commit is contained in:
Bob Nystrom 2016-08-05 15:18:07 -07:00 committed by GitHub
parent 21ee4b92f1
commit b504fd428c

View File

@ -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);
}
}
}