Index: utils/pub/utils.dart |
diff --git a/utils/pub/utils.dart b/utils/pub/utils.dart |
index 729f55a0e0235380dad5be8198df6cbcc95ceb09..601290789e5dbbe1bdbf5cc3c257c307f5f54918 100644 |
--- a/utils/pub/utils.dart |
+++ b/utils/pub/utils.dart |
@@ -173,15 +173,25 @@ void chainToCompleter(Future future, Completer completer) { |
/// Returns a [Future] that will complete to the first element of [stream]. |
/// Unlike [Stream.first], this is safe to use with single-subscription streams. |
Future streamFirst(Stream stream) { |
+ // TODO(nweiz): remove this when issue 8512 is fixed. |
+ var cancelled = false; |
var completer = new Completer(); |
var subscription; |
subscription = stream.listen((value) { |
- subscription.cancel(); |
- completer.complete(value); |
- }, |
- onError: (e) => completer.completeError(e.error, e.stackTrace), |
- onDone: () => completer.completeError(new StateError("No elements")), |
- unsubscribeOnError: true); |
+ if (!cancelled) { |
+ cancelled = true; |
+ subscription.cancel(); |
+ completer.complete(value); |
+ } |
+ }, onError: (e) { |
+ if (!cancelled) { |
+ completer.completeError(e.error, e.stackTrace); |
+ } |
+ }, onDone: () { |
+ if (!cancelled) { |
+ completer.completeError(new StateError("No elements")); |
+ } |
+ }, unsubscribeOnError: true); |
return completer.future; |
} |