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