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 final EventSink<T> _sink; | 212 final EventSink<T> _sink; |
Lasse Reichstein Nielsen
2017/04/26 08:26:19
Make this field non-final and set it to null when
floitsch
2017/05/01 16:46:25
Done.
| |
213 bool _isClosed = false; | |
213 | 214 |
214 _HandlerEventSink( | 215 _HandlerEventSink( |
215 this._handleData, this._handleError, this._handleDone, this._sink); | 216 this._handleData, this._handleError, this._handleDone, this._sink); |
216 | 217 |
217 void add(S data) { | 218 void add(S data) { |
219 if (_isClosed) throw new StateError("Sink is closed"); | |
218 if (_handleData != null) { | 220 if (_handleData != null) { |
219 _handleData(data, _sink); | 221 _handleData(data, _sink); |
220 } else { | 222 } else { |
221 _sink.add(data as T); | 223 _sink.add(data as T); |
222 } | 224 } |
223 } | 225 } |
224 | 226 |
225 void addError(Object error, [StackTrace stackTrace]) { | 227 void addError(Object error, [StackTrace stackTrace]) { |
228 if (_isClosed) throw new StateError("Sink is closed"); | |
226 if (_handleError != null) { | 229 if (_handleError != null) { |
227 _handleError(error, stackTrace, _sink); | 230 _handleError(error, stackTrace, _sink); |
228 } else { | 231 } else { |
229 _sink.addError(error, stackTrace); | 232 _sink.addError(error, stackTrace); |
230 } | 233 } |
231 } | 234 } |
232 | 235 |
233 void close() { | 236 void close() { |
Lasse Reichstein Nielsen
2017/04/26 08:26:19
We should probably bail out here if the sink is al
floitsch
2017/05/01 16:46:25
Realized the same thing.
done.
| |
237 _isClosed = true; | |
234 if (_handleDone != null) { | 238 if (_handleDone != null) { |
235 _handleDone(_sink); | 239 _handleDone(_sink); |
236 } else { | 240 } else { |
237 _sink.close(); | 241 _sink.close(); |
238 } | 242 } |
239 } | 243 } |
240 } | 244 } |
241 | 245 |
242 /** | 246 /** |
243 * A StreamTransformer that transformers events with the given handlers. | 247 * A StreamTransformer that transformers events with the given handlers. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 StreamSubscription<T> listen(void onData(T event), | 304 StreamSubscription<T> listen(void onData(T event), |
301 {Function onError, void onDone(), bool cancelOnError}) { | 305 {Function onError, void onDone(), bool cancelOnError}) { |
302 cancelOnError = identical(true, cancelOnError); | 306 cancelOnError = identical(true, cancelOnError); |
303 StreamSubscription<T> result = _transformer(_stream, cancelOnError); | 307 StreamSubscription<T> result = _transformer(_stream, cancelOnError); |
304 result.onData(onData); | 308 result.onData(onData); |
305 result.onError(onError); | 309 result.onError(onError); |
306 result.onDone(onDone); | 310 result.onDone(onDone); |
307 return result; | 311 return result; |
308 } | 312 } |
309 } | 313 } |
OLD | NEW |