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 import 'dart:convert'; | 6 import 'dart:convert'; |
7 | 7 |
8 import 'package:async/async.dart'; | 8 import 'package:async/async.dart'; |
9 | 9 |
10 import '../stream_channel.dart'; | 10 import '../stream_channel.dart'; |
11 | 11 |
12 /// A [StreamChannelTransformer] transforms the events being passed to and | 12 /// A [StreamChannelTransformer] transforms the events being passed to and |
13 /// emitted by a [StreamChannel]. | 13 /// emitted by a [StreamChannel]. |
14 /// | 14 /// |
15 /// This works on the same principle as [StreamTransformer] and | 15 /// This works on the same principle as [StreamTransformer] and |
16 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes | 16 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes |
17 /// in the original [StreamChannel] and returns the transformed version. | 17 /// in the original [StreamChannel] and returns the transformed version. |
18 /// | 18 /// |
19 /// Transformers must be able to have `bind` called multiple times. | 19 /// Transformers must be able to have `bind` called multiple times. |
20 class StreamChannelTransformer<S, T> { | 20 class StreamChannelTransformer<S, T> { |
21 /// The transformer to use on the channel's stream. | 21 /// The transformer to use on the channel's stream. |
22 final StreamTransformer _streamTransformer; | 22 final StreamTransformer<T, S> _streamTransformer; |
23 | 23 |
24 /// The transformer to use on the channel's sink. | 24 /// The transformer to use on the channel's sink. |
25 final StreamSinkTransformer _sinkTransformer; | 25 final StreamSinkTransformer<S, T> _sinkTransformer; |
26 | 26 |
27 /// Creates a [StreamChannelTransformer] from existing stream and sink | 27 /// Creates a [StreamChannelTransformer] from existing stream and sink |
28 /// transformers. | 28 /// transformers. |
29 const StreamChannelTransformer( | 29 const StreamChannelTransformer( |
30 this._streamTransformer, this._sinkTransformer); | 30 this._streamTransformer, this._sinkTransformer); |
31 | 31 |
32 /// Creates a [StreamChannelTransformer] from a codec's encoder and decoder. | 32 /// Creates a [StreamChannelTransformer] from a codec's encoder and decoder. |
33 /// | 33 /// |
34 /// All input to the inner channel's sink is encoded using [Codec.encoder], | 34 /// All input to the inner channel's sink is encoded using [Codec.encoder], |
35 /// and all output from its stream is decoded using [Codec.decoder]. | 35 /// and all output from its stream is decoded using [Codec.decoder]. |
36 StreamChannelTransformer.fromCodec(Codec<S, T> codec) | 36 StreamChannelTransformer.fromCodec(Codec<S, T> codec) |
37 : this( | 37 : this( |
38 codec.decoder, | 38 typedStreamTransformer(codec.decoder), |
39 new StreamSinkTransformer.fromStreamTransformer(codec.encoder)); | 39 StreamSinkTransformer.typed( |
| 40 new StreamSinkTransformer.fromStreamTransformer(codec.encoder))); |
40 | 41 |
41 /// Transforms the events sent to and emitted by [channel]. | 42 /// Transforms the events sent to and emitted by [channel]. |
42 /// | 43 /// |
43 /// Creates a new channel. When events are passed to the returned channel's | 44 /// Creates a new channel. When events are passed to the returned channel's |
44 /// sink, the transformer will transform them and pass the transformed | 45 /// sink, the transformer will transform them and pass the transformed |
45 /// versions to `channel.sink`. When events are emitted from the | 46 /// versions to `channel.sink`. When events are emitted from the |
46 /// `channel.straem`, the transformer will transform them and pass the | 47 /// `channel.straem`, the transformer will transform them and pass the |
47 /// transformed versions to the returned channel's stream. | 48 /// transformed versions to the returned channel's stream. |
48 StreamChannel<S> bind(StreamChannel<T> channel) => | 49 StreamChannel<S> bind(StreamChannel<T> channel) => |
49 new StreamChannel<S>( | 50 new StreamChannel<S>( |
50 channel.stream.transform(_streamTransformer), | 51 channel.stream.transform(_streamTransformer), |
51 _sinkTransformer.bind(channel.sink)); | 52 _sinkTransformer.bind(channel.sink)); |
52 } | 53 } |
OLD | NEW |