OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 * Wraps an [_EventSink] so it exposes only the [EventSink] interface. | 8 * Wraps an [_EventSink] so it exposes only the [EventSink] interface. |
9 */ | 9 */ |
10 class _EventSinkWrapper<T> implements EventSink<T> { | 10 class _EventSinkWrapper<T> implements EventSink<T> { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 final _TransformErrorHandler<T> _handleError; | 208 final _TransformErrorHandler<T> _handleError; |
209 final _TransformDoneHandler<T> _handleDone; | 209 final _TransformDoneHandler<T> _handleDone; |
210 | 210 |
211 /// The output sink where the handlers should send their data into. | 211 /// The output sink where the handlers should send their data into. |
212 final EventSink<T> _sink; | 212 final EventSink<T> _sink; |
213 | 213 |
214 _HandlerEventSink( | 214 _HandlerEventSink( |
215 this._handleData, this._handleError, this._handleDone, this._sink); | 215 this._handleData, this._handleError, this._handleDone, this._sink); |
216 | 216 |
217 void add(S data) { | 217 void add(S data) { |
218 _handleData(data, _sink); | 218 if (_handleData != null) { |
| 219 _handleData(data, _sink); |
| 220 } else { |
| 221 _sink.add(data as T); |
| 222 } |
219 } | 223 } |
220 | 224 |
221 void addError(Object error, [StackTrace stackTrace]) { | 225 void addError(Object error, [StackTrace stackTrace]) { |
222 _handleError(error, stackTrace, _sink); | 226 if (_handleError != null) { |
| 227 _handleError(error, stackTrace, _sink); |
| 228 } else { |
| 229 _sink.addError(error, stackTrace); |
| 230 } |
223 } | 231 } |
224 | 232 |
225 void close() { | 233 void close() { |
226 _handleDone(_sink); | 234 if (_handleDone != null) { |
| 235 _handleDone(_sink); |
| 236 } else { |
| 237 _sink.close(); |
| 238 } |
227 } | 239 } |
228 } | 240 } |
229 | 241 |
230 /** | 242 /** |
231 * A StreamTransformer that transformers events with the given handlers. | 243 * A StreamTransformer that transformers events with the given handlers. |
232 * | 244 * |
233 * Note that this transformer can only be used once. | 245 * Note that this transformer can only be used once. |
234 */ | 246 */ |
235 class _StreamHandlerTransformer<S, T> extends _StreamSinkTransformer<S, T> { | 247 class _StreamHandlerTransformer<S, T> extends _StreamSinkTransformer<S, T> { |
236 _StreamHandlerTransformer( | 248 _StreamHandlerTransformer( |
237 {void handleData(S data, EventSink<T> sink), | 249 {void handleData(S data, EventSink<T> sink), |
238 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), | 250 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), |
239 void handleDone(EventSink<T> sink)}) | 251 void handleDone(EventSink<T> sink)}) |
240 : super((EventSink<T> outputSink) { | 252 : super((EventSink<T> outputSink) { |
241 if (handleData == null) handleData = _defaultHandleData; | |
242 if (handleError == null) handleError = _defaultHandleError; | |
243 if (handleDone == null) handleDone = _defaultHandleDone; | |
244 return new _HandlerEventSink<S, T>( | 253 return new _HandlerEventSink<S, T>( |
245 handleData, handleError, handleDone, outputSink); | 254 handleData, handleError, handleDone, outputSink); |
246 }); | 255 }); |
247 | 256 |
248 Stream<T> bind(Stream<S> stream) { | 257 Stream<T> bind(Stream<S> stream) { |
249 return super.bind(stream); | 258 return super.bind(stream); |
250 } | 259 } |
251 | |
252 /** Default data handler forwards all data. */ | |
253 static void _defaultHandleData(var data, EventSink sink) { | |
254 sink.add(data); | |
255 } | |
256 | |
257 /** Default error handler forwards all errors. */ | |
258 static void _defaultHandleError( | |
259 error, StackTrace stackTrace, EventSink sink) { | |
260 sink.addError(error, stackTrace); | |
261 } | |
262 | |
263 /** Default done handler forwards done. */ | |
264 static void _defaultHandleDone(EventSink sink) { | |
265 sink.close(); | |
266 } | |
267 } | 260 } |
268 | 261 |
269 /// A closure mapping a stream and cancelOnError to a StreamSubscription. | 262 /// A closure mapping a stream and cancelOnError to a StreamSubscription. |
270 typedef StreamSubscription<T> _SubscriptionTransformer<S, T>( | 263 typedef StreamSubscription<T> _SubscriptionTransformer<S, T>( |
271 Stream<S> stream, bool cancelOnError); | 264 Stream<S> stream, bool cancelOnError); |
272 | 265 |
273 /** | 266 /** |
274 * A [StreamTransformer] that minimizes the number of additional classes. | 267 * A [StreamTransformer] that minimizes the number of additional classes. |
275 * | 268 * |
276 * Instead of implementing three classes: a [StreamTransformer], a [Stream] | 269 * Instead of implementing three classes: a [StreamTransformer], a [Stream] |
(...skipping 30 matching lines...) Expand all Loading... |
307 StreamSubscription<T> listen(void onData(T event), | 300 StreamSubscription<T> listen(void onData(T event), |
308 {Function onError, void onDone(), bool cancelOnError}) { | 301 {Function onError, void onDone(), bool cancelOnError}) { |
309 cancelOnError = identical(true, cancelOnError); | 302 cancelOnError = identical(true, cancelOnError); |
310 StreamSubscription<T> result = _transformer(_stream, cancelOnError); | 303 StreamSubscription<T> result = _transformer(_stream, cancelOnError); |
311 result.onData(onData); | 304 result.onData(onData); |
312 result.onError(onError); | 305 result.onError(onError); |
313 result.onDone(onDone); | 306 result.onDone(onDone); |
314 return result; | 307 return result; |
315 } | 308 } |
316 } | 309 } |
OLD | NEW |