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 import 'dart:async'; |
| 6 |
| 7 export 'src/multi_channel.dart'; |
| 8 |
| 9 /// An abstract class representing a two-way communication channel. |
| 10 /// |
| 11 /// Subclasses are strongly encouraged to mix in or extend [StreamChannelMixin] |
| 12 /// to get default implementations of the various instance methods. Adding new |
| 13 /// methods to this interface will not be considered a breaking change if |
| 14 /// implementations are also added to [StreamChannelMixin]. |
| 15 abstract class StreamChannel<T> { |
| 16 /// The stream that emits values from the other endpoint. |
| 17 Stream<T> get stream; |
| 18 |
| 19 /// The sink for sending values to the other endpoint. |
| 20 StreamSink<T> get sink; |
| 21 |
| 22 /// Creates a new [StreamChannel] that communicates over [stream] and [sink]. |
| 23 factory StreamChannel(Stream<T> stream, StreamSink<T> sink) => |
| 24 new _StreamChannel<T>(stream, sink); |
| 25 |
| 26 /// Connects [this] to [other], so that any values emitted by either are sent |
| 27 /// directly to the other. |
| 28 void pipe(StreamChannel<T> other); |
| 29 } |
| 30 |
| 31 /// An implementation of [StreamChannel] that simply takes a stream and a sink |
| 32 /// as parameters. |
| 33 /// |
| 34 /// This is distinct from [StreamChannel] so that it can use |
| 35 /// [StreamChannelMixin]. |
| 36 class _StreamChannel<T> extends StreamChannelMixin<T> { |
| 37 final Stream<T> stream; |
| 38 final StreamSink<T> sink; |
| 39 |
| 40 _StreamChannel(this.stream, this.sink); |
| 41 } |
| 42 |
| 43 /// A mixin that implements the instance methods of [StreamChannel] in terms of |
| 44 /// [stream] and [sink]. |
| 45 abstract class StreamChannelMixin<T> implements StreamChannel<T> { |
| 46 void pipe(StreamChannel<T> other) { |
| 47 stream.pipe(other.sink); |
| 48 other.stream.pipe(sink); |
| 49 } |
| 50 } |
OLD | NEW |