| 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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 * Wraps handlers (from [StreamTransformer.fromHandlers]) into an `EventSink`. | 202 * Wraps handlers (from [StreamTransformer.fromHandlers]) into an `EventSink`. |
| 203 * | 203 * |
| 204 * This way we can reuse the code from [_StreamSinkTransformer]. | 204 * This way we can reuse the code from [_StreamSinkTransformer]. |
| 205 */ | 205 */ |
| 206 class _HandlerEventSink<S, T> implements EventSink<S> { | 206 class _HandlerEventSink<S, T> implements EventSink<S> { |
| 207 final _TransformDataHandler<S, T> _handleData; | 207 final _TransformDataHandler<S, T> _handleData; |
| 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 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 if (_sink == null) { | |
| 217 throw new ArgumentError("The provided sink must not be null."); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 bool get _isClosed => _sink == null; | |
| 222 | 216 |
| 223 void add(S data) { | 217 void add(S data) { |
| 224 if (_isClosed) { | |
| 225 // TODO(29554): throw a StateError, and don't just report the problem. | |
| 226 Zone.ROOT | |
| 227 ..print("Sink is closed and adding to it is an error.") | |
| 228 ..print(" See http://dartbug.com/29554.") | |
| 229 ..print(StackTrace.current); | |
| 230 } | |
| 231 if (_handleData != null) { | 218 if (_handleData != null) { |
| 232 _handleData(data, _sink); | 219 _handleData(data, _sink); |
| 233 } else { | 220 } else { |
| 234 _sink.add(data as T); | 221 _sink.add(data as T); |
| 235 } | 222 } |
| 236 } | 223 } |
| 237 | 224 |
| 238 void addError(Object error, [StackTrace stackTrace]) { | 225 void addError(Object error, [StackTrace stackTrace]) { |
| 239 if (_isClosed) { | |
| 240 // TODO(29554): throw a StateError, and don't just report the problem. | |
| 241 Zone.ROOT | |
| 242 ..print("Sink is closed and adding to it is an error.") | |
| 243 ..print(" See http://dartbug.com/29554.") | |
| 244 ..print(StackTrace.current); | |
| 245 } | |
| 246 if (_handleError != null) { | 226 if (_handleError != null) { |
| 247 _handleError(error, stackTrace, _sink); | 227 _handleError(error, stackTrace, _sink); |
| 248 } else { | 228 } else { |
| 249 _sink.addError(error, stackTrace); | 229 _sink.addError(error, stackTrace); |
| 250 } | 230 } |
| 251 } | 231 } |
| 252 | 232 |
| 253 void close() { | 233 void close() { |
| 254 if (_isClosed) return; | |
| 255 var sink = _sink; | |
| 256 _sink = null; | |
| 257 if (_handleDone != null) { | 234 if (_handleDone != null) { |
| 258 _handleDone(sink); | 235 _handleDone(_sink); |
| 259 } else { | 236 } else { |
| 260 sink.close(); | 237 _sink.close(); |
| 261 } | 238 } |
| 262 } | 239 } |
| 263 } | 240 } |
| 264 | 241 |
| 265 /** | 242 /** |
| 266 * A StreamTransformer that transformers events with the given handlers. | 243 * A StreamTransformer that transformers events with the given handlers. |
| 267 * | 244 * |
| 268 * Note that this transformer can only be used once. | 245 * Note that this transformer can only be used once. |
| 269 */ | 246 */ |
| 270 class _StreamHandlerTransformer<S, T> extends _StreamSinkTransformer<S, T> { | 247 class _StreamHandlerTransformer<S, T> extends _StreamSinkTransformer<S, T> { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 StreamSubscription<T> listen(void onData(T event), | 300 StreamSubscription<T> listen(void onData(T event), |
| 324 {Function onError, void onDone(), bool cancelOnError}) { | 301 {Function onError, void onDone(), bool cancelOnError}) { |
| 325 cancelOnError = identical(true, cancelOnError); | 302 cancelOnError = identical(true, cancelOnError); |
| 326 StreamSubscription<T> result = _onListen(_stream, cancelOnError); | 303 StreamSubscription<T> result = _onListen(_stream, cancelOnError); |
| 327 result.onData(onData); | 304 result.onData(onData); |
| 328 result.onError(onError); | 305 result.onError(onError); |
| 329 result.onDone(onDone); | 306 result.onDone(onDone); |
| 330 return result; | 307 return result; |
| 331 } | 308 } |
| 332 } | 309 } |
| OLD | NEW |