| 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 import 'transformer/typed.dart'; |
| 12 | 12 |
| 13 /// A [StreamChannelTransformer] transforms the events being passed to and | 13 /// A [StreamChannelTransformer] transforms the events being passed to and |
| 14 /// emitted by a [StreamChannel]. | 14 /// emitted by a [StreamChannel]. |
| 15 /// | 15 /// |
| 16 /// This works on the same principle as [StreamTransformer] and | 16 /// This works on the same principle as [StreamTransformer] and |
| 17 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes | 17 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes |
| 18 /// in the original [StreamChannel] and returns the transformed version. | 18 /// in the original [StreamChannel] and returns the transformed version. |
| 19 /// | 19 /// |
| 20 /// Transformers must be able to have `bind` called multiple times. | 20 /// Transformers must be able to have [bind] called multiple times. If a |
| 21 /// subclass implements [bind] explicitly, it should be sure that the returned |
| 22 /// stream follows the second stream channel guarantee: closing the sink causes |
| 23 /// the stream to close before it emits any more events. This guarantee is |
| 24 /// invalidated when an asynchronous gap is added between the original stream's |
| 25 /// event dispatch and the returned stream's, for example by transforming it |
| 26 /// with a [StreamTransformer]. The guarantee can be easily preserved using [new |
| 27 /// StreamChannel.withCloseGuarantee]. |
| 21 class StreamChannelTransformer<S, T> { | 28 class StreamChannelTransformer<S, T> { |
| 22 /// The transformer to use on the channel's stream. | 29 /// The transformer to use on the channel's stream. |
| 23 final StreamTransformer<T, S> _streamTransformer; | 30 final StreamTransformer<T, S> _streamTransformer; |
| 24 | 31 |
| 25 /// The transformer to use on the channel's sink. | 32 /// The transformer to use on the channel's sink. |
| 26 final StreamSinkTransformer<S, T> _sinkTransformer; | 33 final StreamSinkTransformer<S, T> _sinkTransformer; |
| 27 | 34 |
| 28 /// Creates a wrapper that coerces the type of [transformer]. | 35 /// Creates a wrapper that coerces the type of [transformer]. |
| 29 /// | 36 /// |
| 30 /// This soundly converts a [StreamChannelTransformer] to a | 37 /// This soundly converts a [StreamChannelTransformer] to a |
| (...skipping 25 matching lines...) Expand all Loading... |
| 56 new StreamSinkTransformer.fromStreamTransformer(codec.encoder))); | 63 new StreamSinkTransformer.fromStreamTransformer(codec.encoder))); |
| 57 | 64 |
| 58 /// Transforms the events sent to and emitted by [channel]. | 65 /// Transforms the events sent to and emitted by [channel]. |
| 59 /// | 66 /// |
| 60 /// Creates a new channel. When events are passed to the returned channel's | 67 /// Creates a new channel. When events are passed to the returned channel's |
| 61 /// sink, the transformer will transform them and pass the transformed | 68 /// sink, the transformer will transform them and pass the transformed |
| 62 /// versions to `channel.sink`. When events are emitted from the | 69 /// versions to `channel.sink`. When events are emitted from the |
| 63 /// `channel.straem`, the transformer will transform them and pass the | 70 /// `channel.straem`, the transformer will transform them and pass the |
| 64 /// transformed versions to the returned channel's stream. | 71 /// transformed versions to the returned channel's stream. |
| 65 StreamChannel<S> bind(StreamChannel<T> channel) => | 72 StreamChannel<S> bind(StreamChannel<T> channel) => |
| 66 new StreamChannel<S>( | 73 new StreamChannel<S>.withCloseGuarantee( |
| 67 channel.stream.transform(_streamTransformer), | 74 channel.stream.transform(_streamTransformer), |
| 68 _sinkTransformer.bind(channel.sink)); | 75 _sinkTransformer.bind(channel.sink)); |
| 69 } | 76 } |
| OLD | NEW |