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

Unified Diff: lib/src/stream_completer.dart

Issue 1615253002: Add ClosedStreamSink, and *Completer.setError, and StreamSinkCompleter.fromFuture. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 4 years, 11 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/null_stream_sink.dart ('k') | lib/src/stream_sink_completer.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/stream_completer.dart
diff --git a/lib/src/stream_completer.dart b/lib/src/stream_completer.dart
index c343e6e7bd2330ccda59e9c793f96b0de3fbed50..a6260dcb9d606d1e18c2d5a4005522d3274bc66f 100644
--- a/lib/src/stream_completer.dart
+++ b/lib/src/stream_completer.dart
@@ -39,9 +39,7 @@ class StreamCompleter<T> {
static Stream fromFuture(Future<Stream> streamFuture) {
var completer = new StreamCompleter();
streamFuture.then(completer.setSourceStream,
- onError: (e, s) {
- completer.setSourceStream(streamFuture.asStream());
- });
+ onError: completer.setError);
return completer.stream;
}
@@ -76,8 +74,8 @@ class StreamCompleter<T> {
/// it is immediately listened to, and its events are forwarded to the
/// existing subscription.
///
- /// Either [setSourceStream] or [setEmpty] may be called at most once.
- /// Trying to call either of them again will fail.
+ /// Any one of [setSourceStream], [setEmpty], and [setError] may be called at
+ /// most once. Trying to call any of them again will fail.
void setSourceStream(Stream<T> sourceStream) {
if (_stream._isSourceStreamSet) {
throw new StateError("Source stream already set");
@@ -87,14 +85,24 @@ class StreamCompleter<T> {
/// Equivalent to setting an empty stream using [setSourceStream].
///
- /// Either [setSourceStream] or [setEmpty] may be called at most once.
- /// Trying to call either of them again will fail.
+ /// Any one of [setSourceStream], [setEmpty], and [setError] may be called at
+ /// most once. Trying to call any of them again will fail.
void setEmpty() {
if (_stream._isSourceStreamSet) {
throw new StateError("Source stream already set");
}
_stream._setEmpty();
}
+
+ /// Completes this to a stream that emits [error] and then closes.
+ ///
+ /// This is useful when the process of creating the data for the stream fails.
+ ///
+ /// Any one of [setSourceStream], [setEmpty], and [setError] may be called at
+ /// most once. Trying to call any of them again will fail.
+ void setError(error, [StackTrace stackTrace]) {
+ setSourceStream(new Stream.fromFuture(new Future.error(error, stackTrace)));
+ }
}
/// Stream completed by [StreamCompleter].
« no previous file with comments | « lib/src/null_stream_sink.dart ('k') | lib/src/stream_sink_completer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698