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

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: Addressed review comments. 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') | 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) 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 * stringStream.transform(new StreamTransformer<String, String>(
934 * handleData: (Strung value, StreamSink<String> sink) {
935 * sink.add(value);
936 * sink.add(value); // Duplicate the incoming events.
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
963 * subscription are forwarded to 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 } 1075 }
1084 1076
1085 void cancel() { 1077 void cancel() {
1086 if (_subscription != null) { 1078 if (_subscription != null) {
1087 _subscription.cancel(); 1079 _subscription.cancel();
1088 _subscription = null; 1080 _subscription = null;
1089 } 1081 }
1090 } 1082 }
1091 1083
1092 void _handleData(S data) { 1084 void _handleData(S data) {
1093 _transformer.handleData(data, _sink); 1085 try {
1086 _transformer.handleData(data, _sink);
1087 } catch (e, s) {
1088 _sendError(_asyncError(e, s));
1089 }
1094 } 1090 }
1095 1091
1096 void _handleError(AsyncError error) { 1092 void _handleError(AsyncError error) {
1097 _transformer.handleError(error, _sink); 1093 try {
1094 _transformer.handleError(error, _sink);
1095 } catch (e, s) {
1096 _sendError(_asyncError(e, s, error));
1097 }
1098 } 1098 }
1099 1099
1100 void _handleDone() { 1100 void _handleDone() {
1101 _transformer.handleDone(_sink); 1101 try {
1102 _transformer.handleDone(_sink);
1103 } catch (e, s) {
1104 _sendError(_asyncError(e, s));
1105 }
1102 } 1106 }
1103 1107
1104 // StreamOutputSink interface. 1108 // StreamOutputSink interface.
1105 void _sendData(T data) { 1109 void _sendData(T data) {
1106 _onData(data); 1110 _onData(data);
1107 } 1111 }
1108 1112
1109 void _sendError(AsyncError error) { 1113 void _sendError(AsyncError error) {
1110 _onError(error); 1114 _onError(error);
1111 if (_unsubscribeOnError) { 1115 if (_unsubscribeOnError) {
1112 cancel(); 1116 cancel();
1113 } 1117 }
1114 } 1118 }
1115 1119
1116 void _sendDone() { 1120 void _sendDone() {
1117 // It's ok to cancel even if we have been unsubscribed already. 1121 // It's ok to cancel even if we have been unsubscribed already.
1118 cancel(); 1122 cancel();
1119 _onDone(); 1123 _onDone();
1120 } 1124 }
1121 } 1125 }
1122 1126
1123 class _StreamOutputSinkWrapper<T> implements StreamSink<T> { 1127 class _StreamOutputSinkWrapper<T> implements StreamSink<T> {
1124 _StreamOutputSink _sink; 1128 _StreamOutputSink _sink;
1125 _StreamOutputSinkWrapper(this._sink); 1129 _StreamOutputSinkWrapper(this._sink);
1126 1130
1127 void add(T data) => _sink._sendData(data); 1131 void add(T data) => _sink._sendData(data);
1128 void signalError(AsyncError error) => _sink._sendError(error); 1132 void signalError(AsyncError error) => _sink._sendError(error);
1129 void close() => _sink._sendDone(); 1133 void close() => _sink._sendDone();
1130 } 1134 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/async/stream_pipe.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698