Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(986)

Unified Diff: lib/src/utils.dart

Issue 1062943005: Make BrowserManager resilient to an early exit. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/util/multi_channel.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/utils.dart
diff --git a/lib/src/utils.dart b/lib/src/utils.dart
index 37f2e6639248a30d20dd15e6d7293f54bd11b286..7d697981ffbe97dc5f9f36e2ec8923f7513cd1ae 100644
--- a/lib/src/utils.dart
+++ b/lib/src/utils.dart
@@ -184,6 +184,25 @@ Stream mergeStreams(Iterable<Stream> streamIter) {
return controller.stream;
}
+/// Returns the first value [stream] emits, or `null` if [stream] closes before
+/// emitting a value.
+Future maybeFirst(Stream stream) {
+ var completer = new Completer();
+
+ var subscription;
+ subscription = stream.listen((data) {
+ completer.complete(data);
+ subscription.cancel();
+ }, onError: (error, stackTrace) {
+ completer.completeError(error, stackTrace);
+ subscription.cancel();
+ }, onDone: () {
+ completer.complete();
+ });
+
+ return completer.future;
+}
+
/// Returns a random base64 string containing [bytes] bytes of data.
///
/// [seed] is passed to [math.Random]; [urlSafe] and [addLineSeparator] are
« no previous file with comments | « lib/src/util/multi_channel.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698