Created
June 17, 2022 16:17
-
-
Save mateusfccp/628ac2622f6f3403b94fa6275094c0dc to your computer and use it in GitHub Desktop.
NextStreamValueFuture
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 NextStreamValueFuture<T> with Future<T> { | |
NextStreamValueFuture(this.stream) { | |
late final StreamSubscription<T> subscription; | |
subscription = stream.listen( | |
(value) { | |
completer.complete(value); | |
subscription.cancel(); | |
}, | |
onError: completer.completeError, | |
); | |
} | |
final completer = Completer<T>(); | |
final Stream<T> stream; | |
@override | |
Stream<T> asStream() => completer.future.asStream(); | |
@override | |
Future<T> catchError(Function onError, {bool Function(Object error)? test}) => completer.future.catchError(onError, test: test); | |
@override | |
Future<R> then<R>(FutureOr<R> Function(T value) onValue, {Function? onError}) => completer.future.then(onValue, onError: onError); | |
@override | |
Future<T> timeout(Duration timeLimit, {FutureOr<T> Function()? onTimeout}) => completer.future.timeout(timeLimit, onTimeout: onTimeout); | |
@override | |
Future<T> whenComplete(FutureOr<void> Function() action) => completer.future.whenComplete(action); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment