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

Side by Side Diff: sdk/lib/async/stream.dart

Issue 12082047: Remove transformEvents and make StreamEventTransformer extend StreamTransformer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/async/stream_pipe.dart » ('j') | sdk/lib/async/stream_pipe.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart.async; 5 part of dart.async;
6 6
7 // ------------------------------------------------------------------- 7 // -------------------------------------------------------------------
8 // Core Stream types 8 // Core Stream types
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 10
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 202
203 /** 203 /**
204 * Chain this stream as the input of the provided [StreamTransformer]. 204 * Chain this stream as the input of the provided [StreamTransformer].
205 * 205 *
206 * Returns the result of [:streamTransformer.bind:] itself. 206 * Returns the result of [:streamTransformer.bind:] itself.
207 */ 207 */
208 Stream transform(StreamTransformer<T, dynamic> streamTransformer) { 208 Stream transform(StreamTransformer<T, dynamic> streamTransformer) {
209 return streamTransformer.bind(this); 209 return streamTransformer.bind(this);
210 } 210 }
211 211
212 /**
213 * Create a new stream from this by modifying events.
214 *
215 * Subscribing on the returned stream is the same as subscribing on
216 * this stream, except that events are passed through the [transformer]
217 * before being emitted. The transformer may generate any number and
218 * types of events for each incoming event. Pauses on the returned
219 * subscription are pauses on this stream.
220 *
221 * An example that duplicates all data events:
222 *
223 * someStream.transformEvents(new StreamEventTransformer.from(
224 * handleData: (var value, StreamSink sink) {
225 * sink.add(value);
226 * sink.add(value);
227 * }));
228 */
229 Stream transformEvents(StreamEventTransformer<T, dynamic> transformer) {
230 return new EventTransformStream<T, dynamic>(this, transformer);
231 }
232
233 /** Reduces a sequence of values by repeatedly applying [combine]. */ 212 /** Reduces a sequence of values by repeatedly applying [combine]. */
234 Future reduce(var initialValue, combine(var previous, T element)) { 213 Future reduce(var initialValue, combine(var previous, T element)) {
235 _FutureImpl result = new _FutureImpl(); 214 _FutureImpl result = new _FutureImpl();
236 var value = initialValue; 215 var value = initialValue;
237 StreamSubscription subscription; 216 StreamSubscription subscription;
238 subscription = this.listen( 217 subscription = this.listen(
239 // TODO(ahe): Restore type when feature is implemented in dart2js 218 // TODO(ahe): Restore type when feature is implemented in dart2js
240 // checked mode. http://dartbug.com/7733 219 // checked mode. http://dartbug.com/7733
241 (/*T*/ element) { 220 (/*T*/ element) {
242 _runUserCode( 221 _runUserCode(
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 * The target of a [Stream.pipe] call. 905 * The target of a [Stream.pipe] call.
927 * 906 *
928 * The [Stream.pipe] call will pass itself to this object, and then return 907 * The [Stream.pipe] call will pass itself to this object, and then return
929 * the resulting [Future]. The pipe should complete the future when it's 908 * the resulting [Future]. The pipe should complete the future when it's
930 * done. 909 * done.
931 */ 910 */
932 abstract class StreamConsumer<S, T> { 911 abstract class StreamConsumer<S, T> {
933 Future<T> consume(Stream<S> stream); 912 Future<T> consume(Stream<S> stream);
934 } 913 }
935 914
915
936 /** 916 /**
937 * The target of a [Stream.transform] call. 917 * The target of a [Stream.transform] call.
938 * 918 *
939 * The [Stream.transform] call will pass itself to this object and then return 919 * The [Stream.transform] call will pass itself to this object and then return
940 * the resulting stream. 920 * the resulting stream.
941 */ 921 */
942 abstract class StreamTransformer<S, T> { 922 abstract class StreamTransformer<S, T> {
943 /** 923 /**
944 * Create a [StreamTransformer] that delegates events to the given functions. 924 * Create a [StreamTransformer] that delegates events to the given functions.
945 * 925 *
946 * If a parameter is omitted, a default handler is used that forwards the 926 * This is actually a [StreamEventTransformer] where the event handling is
947 * event directly to the sink. 927 * performed by the function arguments.
928 * If an argument is omitted, it acts as the corresponding default method from
929 * [StreamEventTransformer].
948 * 930 *
949 * Pauses on the returned stream are forwarded to the input stream as well. 931 * Example use:
932 *
933 * someTypeStream.transform(new StreamTransformer<Type, Type>(
floitsch 2013/01/29 13:37:04 Make it a real example. stringStream.transform(new
Lasse Reichstein Nielsen 2013/01/29 14:04:14 Done.
934 * handleData: (Type value, StreamSink<Type> sink) {
935 * sink.add(value);
936 * sink.add(value);
937 * }));
938 *
950 */ 939 */
951 factory StreamTransformer.from({ 940 factory StreamTransformer({
952 void onData(S data, StreamSink<T> sink), 941 void handleData(S data, StreamSink<T> sink),
953 void onError(AsyncError error, StreamSink<T> sink), 942 void handleError(AsyncError error, StreamSink<T> sink),
954 void onDone(StreamSink<T> sink)}) { 943 void handleDone(StreamSink<T> sink)}) {
955 return new _StreamTransformerImpl<S, T>(onData, onError, onDone); 944 return new _StreamTransformerImpl<S, T>(handleData,
945 handleError,
946 handleDone);
956 } 947 }
957 948
958 Stream<T> bind(Stream<S> stream); 949 Stream<T> bind(Stream<S> stream);
959 } 950 }
960 951
961 952
962 /** 953 /**
963 * A transformer of stream events. 954 * Base class for transformers that modifies stream events.
964 * 955 *
965 * A [StreamEventTransformer] transforms incoming Stream 956 * A [StreamEventTransformer] transforms incoming Stream
966 * events of one kind into outgoing events of another kind. 957 * events of one kind into outgoing events of (possibly) another kind.
958 *
959 * Subscribing on the stream returned by [bind] is the same as subscribing on
960 * the source stream, except that events are passed through the [transformer]
961 * before being emitted. The transformer may generate any number and
962 * types of events for each incoming event. Pauses on the returned
floitsch 2013/01/29 13:37:04 The pause needs more description: - does the Strea
Lasse Reichstein Nielsen 2013/01/29 14:04:14 The latter. The returned stream can't buffer at al
963 * subscription are pauses on this stream.
964 *
965 * An example that duplicates all data events:
966 *
967 * class DoubleTransformer<T> extends StreamEventTransformerBase<T, T> {
968 * void handleData(T data, StreamSink<T> sink) {
969 * sink.add(value);
970 * sink.add(value);
971 * }
972 * }
973 * someTypeStream.transform(new DoubleTransformer<Type>());
967 * 974 *
968 * The default implementations of the "handle" methods forward 975 * The default implementations of the "handle" methods forward
969 * the events unmodified. In that case the generic type [T] needs to be 976 * the events unmodified. If using the default [handleData] the generic type [T]
970 * assignable to [S]. 977 * needs to be assignable to [S].
971 *
972 * You can use a [StreamEventTransformer] to modify a Stream's events using
973 * the [Stream.transformEvents] method.
974 */ 978 */
975 abstract class StreamEventTransformer<S, T> { 979 abstract class StreamEventTransformer<S, T> implements StreamTransformer<S, T> {
976 const StreamEventTransformer(); 980 const StreamEventTransformer();
977 981
978 /** 982 Stream<T> bind(Stream<S> source) {
979 * Create a [StreamEventTransformer] that delegates to the provided methods. 983 return new EventTransformStream<S, T>(source, this);
980 *
981 * The created transformer acts as if the provided functions were the
982 * methods of the same name.
983 */
984 factory StreamEventTransformer.from({
985 void handleData(S data, StreamSink<T> sink),
986 void handleError(AsyncError error, StreamSink<T> sink),
987 void handleDone(StreamSink<T> sink)
988 }) {
989 return new _StreamEventTransformerImpl<S, T>(handleData,
990 handleError,
991 handleDone);
992 } 984 }
993 985
994
995 /** 986 /**
996 * Act on incoming data event. 987 * Act on incoming data event.
997 * 988 *
998 * The method may generate any number of events on the sink, but should 989 * The method may generate any number of events on the sink, but should
999 * not throw. 990 * not throw.
1000 */ 991 */
1001 void handleData(S event, StreamSink<T> sink) { 992 void handleData(S event, StreamSink<T> sink) {
1002 var data = event; 993 var data = event;
1003 sink.add(data); 994 sink.add(data);
1004 } 995 }
(...skipping 12 matching lines...) Expand all
1017 * Act on incoming done event. 1008 * Act on incoming done event.
1018 * 1009 *
1019 * The method may generate any number of events on the sink, but should 1010 * The method may generate any number of events on the sink, but should
1020 * not throw. 1011 * not throw.
1021 */ 1012 */
1022 void handleDone(StreamSink<T> sink){ 1013 void handleDone(StreamSink<T> sink){
1023 sink.close(); 1014 sink.close();
1024 } 1015 }
1025 } 1016 }
1026 1017
1018
1027 /** 1019 /**
1028 * Stream that transforms another stream by intercepting and replacing events. 1020 * Stream that transforms another stream by intercepting and replacing events.
1029 * 1021 *
1030 * This [Stream] is a transformation of a source stream. Listening on this 1022 * This [Stream] is a transformation of a source stream. Listening on this
1031 * stream is the same as listening on the source stream, except that events 1023 * stream is the same as listening on the source stream, except that events
1032 * are intercepted and modified by a [StreamEventTransformer] before becoming 1024 * are intercepted and modified by a [StreamEventTransformer] before becoming
1033 * events on this stream. 1025 * events on this stream.
1034 */ 1026 */
1035 class EventTransformStream<S, T> extends Stream<T> { 1027 class EventTransformStream<S, T> extends Stream<T> {
1036 Stream<S> _source; 1028 final Stream<S> _source;
1037 StreamEventTransformer _transformer; 1029 final StreamEventTransformer _transformer;
1038 EventTransformStream(Stream<S> source, 1030 EventTransformStream(Stream<S> source,
1039 StreamEventTransformer<S, T> transformer) 1031 StreamEventTransformer<S, T> transformer)
1040 : _source = source, _transformer = transformer; 1032 : _source = source, _transformer = transformer;
1041 1033
1042 StreamSubscription<T> listen(void onData(T data), 1034 StreamSubscription<T> listen(void onData(T data),
1043 { void onError(AsyncError error), 1035 { void onError(AsyncError error),
1044 void onDone(), 1036 void onDone(),
1045 bool unsubscribeOnError }) { 1037 bool unsubscribeOnError }) {
1046 return new _EventTransformStreamSubscription(_source, _transformer, 1038 return new _EventTransformStreamSubscription(_source, _transformer,
1047 onData, onError, onDone, 1039 onData, onError, onDone,
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 } 1113 }
1122 1114
1123 class _StreamOutputSinkWrapper<T> implements StreamSink<T> { 1115 class _StreamOutputSinkWrapper<T> implements StreamSink<T> {
1124 _StreamOutputSink _sink; 1116 _StreamOutputSink _sink;
1125 _StreamOutputSinkWrapper(this._sink); 1117 _StreamOutputSinkWrapper(this._sink);
1126 1118
1127 void add(T data) => _sink._sendData(data); 1119 void add(T data) => _sink._sendData(data);
1128 void signalError(AsyncError error) => _sink._sendError(error); 1120 void signalError(AsyncError error) => _sink._sendError(error);
1129 void close() => _sink._sendDone(); 1121 void close() => _sink._sendDone();
1130 } 1122 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/async/stream_pipe.dart » ('j') | sdk/lib/async/stream_pipe.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698