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

Side by Side Diff: lib/stream_channel.dart

Issue 1639643002: Add StreamChannel.transform(). (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
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | pubspec.yaml » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
7
8 import 'package:async/async.dart';
6 9
7 export 'src/delegating_stream_channel.dart'; 10 export 'src/delegating_stream_channel.dart';
8 export 'src/multi_channel.dart'; 11 export 'src/multi_channel.dart';
9 12
10 /// An abstract class representing a two-way communication channel. 13 /// An abstract class representing a two-way communication channel.
11 /// 14 ///
12 /// Users should consider the [stream] emitting a "done" event to be the 15 /// Users should consider the [stream] emitting a "done" event to be the
13 /// canonical indicator that the channel has closed. If they wish to close the 16 /// canonical indicator that the channel has closed. If they wish to close the
14 /// channel, they should close the [sink]—canceling the stream subscription is 17 /// channel, they should close the [sink]—canceling the stream subscription is
15 /// not sufficient. Protocol errors may be emitted through the stream or through 18 /// not sufficient. Protocol errors may be emitted through the stream or through
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 /// Creates a new [StreamChannel] that communicates over [stream] and [sink]. 60 /// Creates a new [StreamChannel] that communicates over [stream] and [sink].
58 /// 61 ///
59 /// Note that this stream/sink pair must provide the guarantees listed in the 62 /// Note that this stream/sink pair must provide the guarantees listed in the
60 /// [StreamChannel] documentation. 63 /// [StreamChannel] documentation.
61 factory StreamChannel(Stream<T> stream, StreamSink<T> sink) => 64 factory StreamChannel(Stream<T> stream, StreamSink<T> sink) =>
62 new _StreamChannel<T>(stream, sink); 65 new _StreamChannel<T>(stream, sink);
63 66
64 /// Connects [this] to [other], so that any values emitted by either are sent 67 /// Connects [this] to [other], so that any values emitted by either are sent
65 /// directly to the other. 68 /// directly to the other.
66 void pipe(StreamChannel<T> other); 69 void pipe(StreamChannel<T> other);
70
71 /// Transforms [this] using [codec].
72 ///
73 /// This returns a stream channel that encodes all input using [Codec.encoder]
74 /// before passing it to this channel's [sink], and decodes all output from
75 /// this channel's [stream] using [Codec.decoder].
76 StreamChannel transform(Codec<dynamic, T> codec);
67 } 77 }
68 78
69 /// An implementation of [StreamChannel] that simply takes a stream and a sink 79 /// An implementation of [StreamChannel] that simply takes a stream and a sink
70 /// as parameters. 80 /// as parameters.
71 /// 81 ///
72 /// This is distinct from [StreamChannel] so that it can use 82 /// This is distinct from [StreamChannel] so that it can use
73 /// [StreamChannelMixin]. 83 /// [StreamChannelMixin].
74 class _StreamChannel<T> extends StreamChannelMixin<T> { 84 class _StreamChannel<T> extends StreamChannelMixin<T> {
75 final Stream<T> stream; 85 final Stream<T> stream;
76 final StreamSink<T> sink; 86 final StreamSink<T> sink;
77 87
78 _StreamChannel(this.stream, this.sink); 88 _StreamChannel(this.stream, this.sink);
79 } 89 }
80 90
81 /// A mixin that implements the instance methods of [StreamChannel] in terms of 91 /// A mixin that implements the instance methods of [StreamChannel] in terms of
82 /// [stream] and [sink]. 92 /// [stream] and [sink].
83 abstract class StreamChannelMixin<T> implements StreamChannel<T> { 93 abstract class StreamChannelMixin<T> implements StreamChannel<T> {
84 void pipe(StreamChannel<T> other) { 94 void pipe(StreamChannel<T> other) {
85 stream.pipe(other.sink); 95 stream.pipe(other.sink);
86 other.stream.pipe(sink); 96 other.stream.pipe(sink);
87 } 97 }
98
99 StreamChannel transform(Codec<dynamic, T> codec) {
100 var sinkTransformer =
101 new StreamSinkTransformer.fromStreamTransformer(codec.encoder);
102 return new _StreamChannel(
103 stream.transform(codec.decoder),
104 sinkTransformer.bind(sink));
105 }
88 } 106 }
OLDNEW
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | pubspec.yaml » ('J')

Powered by Google App Engine
This is Rietveld 408576698