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

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

Powered by Google App Engine
This is Rietveld 408576698