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