Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library async.closed_stream_sink; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 /// A [StreamSink] that's created in a closed state. | |
| 10 /// | |
| 11 /// The sink silently drops events until [close] is called, at which point it | |
| 12 /// 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.
| |
| 13 /// | |
| 14 /// This can be used when a sink is needed but no events are actually intended | |
| 15 /// 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
| |
| 16 /// 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
| |
| 17 class ClosedStreamSink<T> implements StreamSink<T> { | |
| 18 final Future done; | |
| 19 | |
| 20 /// Whether the sink has been closed. | |
| 21 /// | |
| 22 /// 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
| |
| 23 /// this ignores events until [close] is called. After that, it throws | |
| 24 /// [StateError]s. | |
| 25 var _closed = false; | |
| 26 | |
| 27 /// Creates a closed sink. | |
| 28 /// | |
| 29 /// If [done] is passed, it's used as the [Sink.done] future. Otherwise, a | |
| 30 /// completed future is used. | |
| 31 ClosedStreamSink({Future done}) : done = done ?? new Future.value(); | |
| 32 | |
| 33 /// Creates a closed sink whose [done] future emits [error]. | |
| 34 ClosedStreamSink.error(error, [StackTrace stackTrace]) | |
| 35 : 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.
| |
| 36 | |
| 37 void add(T data) { | |
| 38 if (!_closed) return; | |
| 39 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.
| |
| 40 } | |
| 41 | |
| 42 void addError(error, [StackTrace stackTrace]) { | |
| 43 if (!_closed) return; | |
| 44 throw new StateError("Cannot add to a closed sink."); | |
| 45 } | |
| 46 | |
| 47 Future addStream(Stream<T> stream) { | |
| 48 if (_closed) { | |
| 49 throw new StateError("Cannot add to a closed sink."); | |
| 50 } | |
| 51 | |
|
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.
| |
| 52 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.
| |
| 53 } | |
| 54 | |
| 55 Future close() { | |
| 56 _closed = true; | |
| 57 return done; | |
| 58 } | |
| 59 } | |
| OLD | NEW |