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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | pubspec.yaml » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/stream_channel.dart
diff --git a/lib/stream_channel.dart b/lib/stream_channel.dart
index fd7b0932733e56808a3c2b87b68c723bcc8ab9c6..8cd27199614512d396a7aeb33cf0389a6a212f56 100644
--- a/lib/stream_channel.dart
+++ b/lib/stream_channel.dart
@@ -3,6 +3,9 @@
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
+import 'dart:convert';
+
+import 'package:async/async.dart';
export 'src/delegating_stream_channel.dart';
export 'src/multi_channel.dart';
@@ -64,6 +67,13 @@ abstract class StreamChannel<T> {
/// Connects [this] to [other], so that any values emitted by either are sent
/// directly to the other.
void pipe(StreamChannel<T> other);
+
+ /// Transforms [this] using [codec].
+ ///
+ /// This returns a stream channel that encodes all input using [Codec.encoder]
+ /// before passing it to this channel's [sink], and decodes all output from
+ /// this channel's [stream] using [Codec.decoder].
+ StreamChannel transform(Codec<dynamic, T> codec);
}
/// An implementation of [StreamChannel] that simply takes a stream and a sink
@@ -85,4 +95,12 @@ abstract class StreamChannelMixin<T> implements StreamChannel<T> {
stream.pipe(other.sink);
other.stream.pipe(sink);
}
+
+ StreamChannel transform(Codec<dynamic, T> codec) {
+ var sinkTransformer =
+ new StreamSinkTransformer.fromStreamTransformer(codec.encoder);
+ return new _StreamChannel(
+ stream.transform(codec.decoder),
+ sinkTransformer.bind(sink));
+ }
}
« no previous file with comments | « no previous file | pubspec.yaml » ('j') | pubspec.yaml » ('J')

Powered by Google App Engine
This is Rietveld 408576698