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 import 'transformer/typed.dart'; |
11 | 12 |
12 /// A [StreamChannelTransformer] transforms the events being passed to and | 13 /// A [StreamChannelTransformer] transforms the events being passed to and |
13 /// emitted by a [StreamChannel]. | 14 /// emitted by a [StreamChannel]. |
14 /// | 15 /// |
15 /// This works on the same principle as [StreamTransformer] and | 16 /// This works on the same principle as [StreamTransformer] and |
16 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes | 17 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes |
17 /// in the original [StreamChannel] and returns the transformed version. | 18 /// in the original [StreamChannel] and returns the transformed version. |
18 /// | 19 /// |
19 /// Transformers must be able to have `bind` called multiple times. | 20 /// Transformers must be able to have `bind` called multiple times. |
20 class StreamChannelTransformer<S, T> { | 21 class StreamChannelTransformer<S, T> { |
21 /// The transformer to use on the channel's stream. | 22 /// The transformer to use on the channel's stream. |
22 final StreamTransformer<T, S> _streamTransformer; | 23 final StreamTransformer<T, S> _streamTransformer; |
23 | 24 |
24 /// The transformer to use on the channel's sink. | 25 /// The transformer to use on the channel's sink. |
25 final StreamSinkTransformer<S, T> _sinkTransformer; | 26 final StreamSinkTransformer<S, T> _sinkTransformer; |
26 | 27 |
| 28 /// Creates a wrapper that coerces the type of [transformer]. |
| 29 /// |
| 30 /// This soundly converts a [StreamChannelTransformer] to a |
| 31 /// `StreamChannelTransformer<S, T>`, regardless of its original generic type, |
| 32 /// by asserting that the events emitted by the transformed channel's stream |
| 33 /// are instances of `T` whenever they're provided. If they're not, the stream |
| 34 /// throws a [CastError]. This also means that calls to [StreamSink.add] on |
| 35 /// the transformed channel's sink may throw a [CastError] if the argument |
| 36 /// type doesn't match the reified type of the sink. |
| 37 static StreamChannelTransformer/*<S, T>*/ typed/*<S, T>*/( |
| 38 StreamChannelTransformer transformer) => |
| 39 transformer is StreamChannelTransformer/*<S, T>*/ |
| 40 ? transformer |
| 41 : new TypeSafeStreamChannelTransformer(transformer); |
| 42 |
27 /// Creates a [StreamChannelTransformer] from existing stream and sink | 43 /// Creates a [StreamChannelTransformer] from existing stream and sink |
28 /// transformers. | 44 /// transformers. |
29 const StreamChannelTransformer( | 45 const StreamChannelTransformer( |
30 this._streamTransformer, this._sinkTransformer); | 46 this._streamTransformer, this._sinkTransformer); |
31 | 47 |
32 /// Creates a [StreamChannelTransformer] from a codec's encoder and decoder. | 48 /// Creates a [StreamChannelTransformer] from a codec's encoder and decoder. |
33 /// | 49 /// |
34 /// All input to the inner channel's sink is encoded using [Codec.encoder], | 50 /// 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]. | 51 /// and all output from its stream is decoded using [Codec.decoder]. |
36 StreamChannelTransformer.fromCodec(Codec<S, T> codec) | 52 StreamChannelTransformer.fromCodec(Codec<S, T> codec) |
37 : this( | 53 : this( |
38 typedStreamTransformer(codec.decoder), | 54 typedStreamTransformer(codec.decoder), |
39 StreamSinkTransformer.typed( | 55 StreamSinkTransformer.typed( |
40 new StreamSinkTransformer.fromStreamTransformer(codec.encoder))); | 56 new StreamSinkTransformer.fromStreamTransformer(codec.encoder))); |
41 | 57 |
42 /// Transforms the events sent to and emitted by [channel]. | 58 /// Transforms the events sent to and emitted by [channel]. |
43 /// | 59 /// |
44 /// Creates a new channel. When events are passed to the returned channel's | 60 /// Creates a new channel. When events are passed to the returned channel's |
45 /// sink, the transformer will transform them and pass the transformed | 61 /// sink, the transformer will transform them and pass the transformed |
46 /// versions to `channel.sink`. When events are emitted from the | 62 /// versions to `channel.sink`. When events are emitted from the |
47 /// `channel.straem`, the transformer will transform them and pass the | 63 /// `channel.straem`, the transformer will transform them and pass the |
48 /// transformed versions to the returned channel's stream. | 64 /// transformed versions to the returned channel's stream. |
49 StreamChannel<S> bind(StreamChannel<T> channel) => | 65 StreamChannel<S> bind(StreamChannel<T> channel) => |
50 new StreamChannel<S>( | 66 new StreamChannel<S>( |
51 channel.stream.transform(_streamTransformer), | 67 channel.stream.transform(_streamTransformer), |
52 _sinkTransformer.bind(channel.sink)); | 68 _sinkTransformer.bind(channel.sink)); |
53 } | 69 } |
OLD | NEW |