Created
June 20, 2019 04:01
-
-
Save miquelbeltran/088bf2e512b622ef10bfcf3959b844b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:async"; | |
class SynchronousError<T> implements Future<T> { | |
final Object _error; | |
SynchronousError(this._error); | |
@override | |
Stream<T> asStream() { | |
final StreamController controller = StreamController<T>(); | |
controller.addError(_error); | |
controller.close(); | |
return controller.stream; | |
} | |
@override | |
Future<T> catchError(Function onError, {bool test(dynamic error)}) { | |
onError(_error); | |
return Completer<T>().future; | |
} | |
@override | |
Future<E> then<E>(dynamic f(T value), {Function onError}) { | |
return SynchronousError<E>(_error); | |
} | |
@override | |
Future<T> timeout(Duration timeLimit, {dynamic onTimeout()}) { | |
return Future<T>.error(_error).timeout(timeLimit, onTimeout: onTimeout); | |
} | |
@override | |
Future<T> whenComplete(dynamic action()) { | |
action(); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment