Chromium Code Reviews| 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; |
| + } |
| +} |