Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: lib/src/stream_channel_transformer.dart

Issue 1632903004: Add StreamChannelTransformer. (Closed) Base URL: git@github.com:dart-lang/stream_channel.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:async';
6 import 'dart:convert';
7
8 import 'package:async/async.dart';
9
10 import '../stream_channel.dart';
11
12 /// A [StreamChannelTransformer] transforms the events being passed to and
13 /// emitted by a [StreamChannel].
14 ///
15 /// This works on the same principle as [StreamTransformer] and
16 /// [StreamSinkTransformer]. Each transformer defines a [bind] method that takes
17 /// in the original [StreamChannel] and returns the transformed version.
18 ///
19 /// Transformers must be able to have `bind` called used multiple times.
Bob Nystrom 2016/01/27 19:30:41 Remove "used".
nweiz 2016/01/27 23:23:49 Done.
20 class StreamChannelTransformer<S, T> {
21 /// The transformer to use on the channel's stream.
22 final StreamTransformer _streamTransformer;
23
24 /// The transformer to use on the channel's sink.
25 final StreamSinkTransformer _sinkTransformer;
26
27 /// Creates a [StreamChannelTransformer] from existing stream and sink
28 /// transformers.
29 const StreamChannelTransformer(
30 this._streamTransformer, this._sinkTransformer);
31
32 /// Creates a [StreamChannelTransformer] from a codec's encoder and decoder.
33 ///
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].
36 StreamChannelTransformer.fromCodec(Codec<S, T> codec)
37 : this(
38 codec.decoder,
39 new StreamSinkTransformer.fromStreamTransformer(codec.encoder));
40
41 /// Transforms the events sent to and emitted by [channel].
42 ///
43 /// 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 /// versions to `channel.sink`. When events are emitted from the
46 /// `channel.straem`, the transformer will transform them and pass the
47 /// transformed versions to the returned channel's stream.
48 StreamChannel<S> bind(StreamChannel<T> channel) =>
49 new StreamChannel<S>(
50 channel.stream.transform(_streamTransformer),
51 _sinkTransformer.bind(channel.sink));
52 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698