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

Unified Diff: lib/src/stream_sink_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/stream_completer.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/stream_sink_completer.dart
diff --git a/lib/src/stream_sink_completer.dart b/lib/src/stream_sink_completer.dart
index 10caa06b064d1af80aaddf658b5ded7114231900..0b6dc46561d1466cef7cbd99249d53aab10c57d3 100644
--- a/lib/src/stream_sink_completer.dart
+++ b/lib/src/stream_sink_completer.dart
@@ -6,6 +6,8 @@ library async.stream_sink_completer;
import 'dart:async';
+import 'null_stream_sink.dart';
+
/// A [sink] where the destination is provided later.
///
/// The [sink] is a normal sink that you can add events to to immediately, but
@@ -29,6 +31,20 @@ class StreamSinkCompleter<T> {
/// Returns [sink] typed as a [_CompleterSink].
_CompleterSink<T> get _sink => sink;
+ /// Convert a `Future<StreamSink>` to a `StreamSink`.
+ ///
+ /// This creates a sink using a sink completer, and sets the destination sink
+ /// to the result of the future when the future completes.
+ ///
+ /// If the future completes with an error, the returned sink will instead
+ /// be closed. Its [Sink.done] future will contain the error.
+ static StreamSink fromFuture(Future<StreamSink> sinkFuture) {
+ var completer = new StreamSinkCompleter();
+ sinkFuture.then(completer.setDestinationSink,
+ onError: completer.setError);
+ return completer.sink;
+ }
+
/// Sets a sink as the destination for events from the [StreamSinkCompleter]'s
/// [sink].
///
@@ -41,12 +57,25 @@ class StreamSinkCompleter<T> {
/// buffered until the destination is available.
///
/// A destination sink may be set at most once.
+ ///
+ /// Either of [setDestinationSink] or [setError] may be called at most once.
+ /// Trying to call either of them again will fail.
void setDestinationSink(StreamSink<T> destinationSink) {
if (_sink._destinationSink != null) {
throw new StateError("Destination sink already set");
}
_sink._setDestinationSink(destinationSink);
}
+
+ /// Completes this to a closed sink whose [Sink.done] future emits [error].
+ ///
+ /// This is useful when the process of loading the sink fails.
+ ///
+ /// Either of [setDestinationSink] or [setError] may be called at most once.
+ /// Trying to call either of them again will fail.
+ void setError(error, [StackTrace stackTrace]) {
+ setDestinationSink(new NullStreamSink.error(error, stackTrace));
+ }
}
/// [StreamSink] completed by [StreamSinkCompleter].
« no previous file with comments | « lib/src/stream_completer.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698