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

Unified Diff: lib/src/closed_stream_sink.dart

Issue 1615253002: Add ClosedStreamSink, and *Completer.setError, and StreamSinkCompleter.fromFuture. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: 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
Index: lib/src/closed_stream_sink.dart
diff --git a/lib/src/closed_stream_sink.dart b/lib/src/closed_stream_sink.dart
new file mode 100644
index 0000000000000000000000000000000000000000..151f419a03828d09d54af40b8349ce45aa81b95c
--- /dev/null
+++ b/lib/src/closed_stream_sink.dart
@@ -0,0 +1,59 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library async.closed_stream_sink;
+
+import 'dart:async';
+
+/// A [StreamSink] that's created in a closed state.
+///
+/// The sink silently drops events until [close] is called, at which point it
+/// throws [StateError]s when events are added.
Lasse Reichstein Nielsen 2016/01/22 09:13:46 It confuses me that it's "in a closed state" befor
nweiz 2016/01/25 22:39:24 Done.
+///
+/// This can be used when a sink is needed but no events are actually intended
+/// to be added. The [new ClosedStreamSink.error] constructor can be used to
Lasse Reichstein Nielsen 2016/01/22 09:13:46 If it's not intended to receive events, why not th
nweiz 2016/01/25 22:39:24 It's meant to be used to mimic the remote stream b
+/// represent errors in creating a sink.
Lasse Reichstein Nielsen 2016/01/22 09:13:46 How does errors in a done future represent errors
nweiz 2016/01/25 22:39:24 From the StreamSink documentation: "If the StreamS
+class ClosedStreamSink<T> implements StreamSink<T> {
+ final Future done;
+
+ /// Whether the sink has been closed.
+ ///
+ /// To be consistent with other stream sinks that have been closed remotely,
Lasse Reichstein Nielsen 2016/01/22 09:13:46 I would use the name "canceled" for what the recei
nweiz 2016/01/25 22:39:24 I think changing the name and updating the class d
+ /// this ignores events until [close] is called. After that, it throws
+ /// [StateError]s.
+ var _closed = false;
+
+ /// Creates a closed sink.
+ ///
+ /// If [done] is passed, it's used as the [Sink.done] future. Otherwise, a
+ /// completed future is used.
+ ClosedStreamSink({Future done}) : done = done ?? new Future.value();
+
+ /// Creates a closed sink whose [done] future emits [error].
+ ClosedStreamSink.error(error, [StackTrace stackTrace])
+ : done = new Future.error(error, stackTrace);
Lasse Reichstein Nielsen 2016/01/22 09:13:46 This will cause the error to be uncaught in the ne
nweiz 2016/01/25 22:39:24 Done.
+
+ void add(T data) {
+ if (!_closed) return;
+ throw new StateError("Cannot add to a closed sink.");
Lasse Reichstein Nielsen 2016/01/22 09:13:46 How about just: if (_closed) throw ...; The curr
nweiz 2016/01/25 22:39:24 Done.
+ }
+
+ void addError(error, [StackTrace stackTrace]) {
+ if (!_closed) return;
+ throw new StateError("Cannot add to a closed sink.");
+ }
+
+ Future addStream(Stream<T> stream) {
+ if (_closed) {
+ throw new StateError("Cannot add to a closed sink.");
+ }
+
Lasse Reichstein Nielsen 2016/01/22 09:13:46 You need a bit here to remember that you are addin
nweiz 2016/01/25 22:39:24 Done.
+ return stream.listen(null).cancel() ?? new Future.value();
Lasse Reichstein Nielsen 2016/01/22 09:13:46 In dart:async we have a Future<dynamic> completed
nweiz 2016/01/25 22:39:24 I don't think it's worth the complexity.
+ }
+
+ Future close() {
+ _closed = true;
+ return done;
+ }
+}
« no previous file with comments | « lib/async.dart ('k') | lib/src/stream_completer.dart » ('j') | lib/src/stream_completer.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698