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

Side by Side Diff: lib/stream_channel.dart

Issue 1657943002: Add support for stream- and sink-specific changes. (Closed) Base URL: git@github.com:dart-lang/stream_channel.git@master
Patch Set: 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 | « CHANGELOG.md ('k') | pubspec.yaml » ('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 6
7 import 'package:async/async.dart';
8
7 import 'src/stream_channel_transformer.dart'; 9 import 'src/stream_channel_transformer.dart';
8 10
9 export 'src/delegating_stream_channel.dart'; 11 export 'src/delegating_stream_channel.dart';
10 export 'src/isolate_channel.dart'; 12 export 'src/isolate_channel.dart';
11 export 'src/json_document_transformer.dart'; 13 export 'src/json_document_transformer.dart';
12 export 'src/multi_channel.dart'; 14 export 'src/multi_channel.dart';
13 export 'src/stream_channel_completer.dart'; 15 export 'src/stream_channel_completer.dart';
14 export 'src/stream_channel_transformer.dart'; 16 export 'src/stream_channel_transformer.dart';
15 17
16 /// An abstract class representing a two-way communication channel. 18 /// An abstract class representing a two-way communication channel.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 new _StreamChannel<T>(stream, sink); 70 new _StreamChannel<T>(stream, sink);
69 71
70 /// Connects [this] to [other], so that any values emitted by either are sent 72 /// Connects [this] to [other], so that any values emitted by either are sent
71 /// directly to the other. 73 /// directly to the other.
72 void pipe(StreamChannel<T> other); 74 void pipe(StreamChannel<T> other);
73 75
74 /// Transforms [this] using [transformer]. 76 /// Transforms [this] using [transformer].
75 /// 77 ///
76 /// This is identical to calling `transformer.bind(channel)`. 78 /// This is identical to calling `transformer.bind(channel)`.
77 StreamChannel transform(StreamChannelTransformer<dynamic, T> transformer); 79 StreamChannel transform(StreamChannelTransformer<dynamic, T> transformer);
80
81 /// Transforms only the [stream] component of [this] using [transformer].
82 StreamChannel<T> transformStream(StreamTransformer<T, T> transformer);
83
84 /// Transforms only the [sink] component of [this] using [transformer].
85 StreamChannel<T> transformSink(StreamSinkTransformer<T, T> transformer);
86
87 /// Returns a copy of [this] with [stream] replaced by [change]'s return
88 /// value.
89 StreamChannel<T> changeStream(Stream<T> change(Stream<T> stream));
90
91 /// Returns a copy of [this] with [sink] replaced by [change]'s return
92 /// value.
93 StreamChannel<T> changeSink(StreamSink<T> change(StreamSink<T> sink));
78 } 94 }
79 95
80 /// An implementation of [StreamChannel] that simply takes a stream and a sink 96 /// An implementation of [StreamChannel] that simply takes a stream and a sink
81 /// as parameters. 97 /// as parameters.
82 /// 98 ///
83 /// This is distinct from [StreamChannel] so that it can use 99 /// This is distinct from [StreamChannel] so that it can use
84 /// [StreamChannelMixin]. 100 /// [StreamChannelMixin].
85 class _StreamChannel<T> extends StreamChannelMixin<T> { 101 class _StreamChannel<T> extends StreamChannelMixin<T> {
86 final Stream<T> stream; 102 final Stream<T> stream;
87 final StreamSink<T> sink; 103 final StreamSink<T> sink;
88 104
89 _StreamChannel(this.stream, this.sink); 105 _StreamChannel(this.stream, this.sink);
90 } 106 }
91 107
92 /// A mixin that implements the instance methods of [StreamChannel] in terms of 108 /// A mixin that implements the instance methods of [StreamChannel] in terms of
93 /// [stream] and [sink]. 109 /// [stream] and [sink].
94 abstract class StreamChannelMixin<T> implements StreamChannel<T> { 110 abstract class StreamChannelMixin<T> implements StreamChannel<T> {
95 void pipe(StreamChannel<T> other) { 111 void pipe(StreamChannel<T> other) {
96 stream.pipe(other.sink); 112 stream.pipe(other.sink);
97 other.stream.pipe(sink); 113 other.stream.pipe(sink);
98 } 114 }
99 115
100 StreamChannel transform(StreamChannelTransformer<dynamic, T> transformer) => 116 StreamChannel transform(StreamChannelTransformer<dynamic, T> transformer) =>
101 transformer.bind(this); 117 transformer.bind(this);
118
119 StreamChannel<T> transformStream(StreamTransformer<T, T> transformer) =>
120 changeStream(transformer.bind);
121
122 StreamChannel<T> transformSink(StreamSinkTransformer<T, T> transformer) =>
123 changeSink(transformer.bind);
124
125 StreamChannel<T> changeStream(Stream<T> change(Stream<T> stream)) =>
126 new StreamChannel(change(stream), sink);
127
128 StreamChannel<T> changeSink(StreamSink<T> change(StreamSink<T> sink)) =>
129 new StreamChannel(stream, change(sink));
102 } 130 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698