OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 export 'src/multi_channel.dart'; | 7 export 'src/multi_channel.dart'; |
8 | 8 |
9 /// An abstract class representing a two-way communication channel. | 9 /// An abstract class representing a two-way communication channel. |
10 /// | 10 /// |
11 /// Subclasses are strongly encouraged to mix in or extend [StreamChannelMixin] | 11 /// Users should consider the [stream] emitting a "done" event to be the |
12 /// to get default implementations of the various instance methods. Adding new | 12 /// canonical indicator that the channel has closed. If they wish to close the |
13 /// methods to this interface will not be considered a breaking change if | 13 /// channel, they should close the [sink]—canceling their stream subscription is |
14 /// implementations are also added to [StreamChannelMixin]. | 14 /// not sufficient. Protocol errors may be emitted through the stream or through |
| 15 /// [Sink.done], depending on their underlying cause. Note that the sink may |
| 16 /// silently drop events if the channel closes before the stream has a listener. |
| 17 /// |
| 18 /// Implementations are strongly encouraged to mix in or extend |
| 19 /// [StreamChannelMixin] to get default implementations of the various instance |
| 20 /// methods. Adding new methods to this interface will not be considered a |
| 21 /// breaking change if implementations are also added to [StreamChannelMixin]. |
| 22 /// |
| 23 /// Implementations must provide the following guarantees: |
| 24 /// |
| 25 /// * The stream is single-subscription, and must follow all the guarantees of |
| 26 /// single-subscription streams. |
| 27 /// |
| 28 /// * Closing the sink causes the stream to close before it emits any more |
| 29 /// events. |
| 30 /// |
| 31 /// * After the stream closes, the sink is automatically closed. If this |
| 32 /// happens, sink methods should silently drop their arguments at least until |
| 33 /// [Sink.done] and the stream's `onDone` callback fire. |
| 34 /// |
| 35 /// * If the stream closes before it has a listener, the sink may either close |
| 36 /// or silently drop events. |
| 37 /// |
| 38 /// * Canceling the stream's subscription has no effect on the sink. The channel |
| 39 /// must still be able to respond to the other endpoint closing the channel |
| 40 /// even after the subscription has been canceled. |
| 41 /// |
| 42 /// * The sink *either* forwards errors to the other endpoint *or* closes as |
| 43 /// soon as an error is added and forwards that error to the [Sink.done] |
| 44 /// future. |
| 45 /// |
| 46 /// These guarantees allow users to interact uniformly with all implementations, |
| 47 /// and ensure that either endpoint closing the stream produces consistent |
| 48 /// behavior. |
15 abstract class StreamChannel<T> { | 49 abstract class StreamChannel<T> { |
16 /// The stream that emits values from the other endpoint. | 50 /// The single-subscription stream that emits values from the other endpoint. |
17 Stream<T> get stream; | 51 Stream<T> get stream; |
18 | 52 |
19 /// The sink for sending values to the other endpoint. | 53 /// The sink for sending values to the other endpoint. |
20 StreamSink<T> get sink; | 54 StreamSink<T> get sink; |
21 | 55 |
22 /// Creates a new [StreamChannel] that communicates over [stream] and [sink]. | 56 /// Creates a new [StreamChannel] that communicates over [stream] and [sink]. |
| 57 /// |
| 58 /// Note that this stream/sink pair must provide the guarantees listed in the |
| 59 /// [StreamChannel] documentation. |
23 factory StreamChannel(Stream<T> stream, StreamSink<T> sink) => | 60 factory StreamChannel(Stream<T> stream, StreamSink<T> sink) => |
24 new _StreamChannel<T>(stream, sink); | 61 new _StreamChannel<T>(stream, sink); |
25 | 62 |
26 /// Connects [this] to [other], so that any values emitted by either are sent | 63 /// Connects [this] to [other], so that any values emitted by either are sent |
27 /// directly to the other. | 64 /// directly to the other. |
28 void pipe(StreamChannel<T> other); | 65 void pipe(StreamChannel<T> other); |
29 } | 66 } |
30 | 67 |
31 /// An implementation of [StreamChannel] that simply takes a stream and a sink | 68 /// An implementation of [StreamChannel] that simply takes a stream and a sink |
32 /// as parameters. | 69 /// as parameters. |
33 /// | 70 /// |
34 /// This is distinct from [StreamChannel] so that it can use | 71 /// This is distinct from [StreamChannel] so that it can use |
35 /// [StreamChannelMixin]. | 72 /// [StreamChannelMixin]. |
36 class _StreamChannel<T> extends StreamChannelMixin<T> { | 73 class _StreamChannel<T> extends StreamChannelMixin<T> { |
37 final Stream<T> stream; | 74 final Stream<T> stream; |
38 final StreamSink<T> sink; | 75 final StreamSink<T> sink; |
39 | 76 |
40 _StreamChannel(this.stream, this.sink); | 77 _StreamChannel(this.stream, this.sink); |
41 } | 78 } |
42 | 79 |
43 /// A mixin that implements the instance methods of [StreamChannel] in terms of | 80 /// A mixin that implements the instance methods of [StreamChannel] in terms of |
44 /// [stream] and [sink]. | 81 /// [stream] and [sink]. |
45 abstract class StreamChannelMixin<T> implements StreamChannel<T> { | 82 abstract class StreamChannelMixin<T> implements StreamChannel<T> { |
46 void pipe(StreamChannel<T> other) { | 83 void pipe(StreamChannel<T> other) { |
47 stream.pipe(other.sink); | 84 stream.pipe(other.sink); |
48 other.stream.pipe(sink); | 85 other.stream.pipe(sink); |
49 } | 86 } |
50 } | 87 } |
OLD | NEW |