OLD | NEW |
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 typedef void _CollectionSinkCallback<T>(Collection<T> collection); | 7 typedef void _CollectionSinkCallback<T>(Collection<T> collection); |
8 typedef void _CollectionSinkErrorCallback(AsyncError error); | 8 typedef void _CollectionSinkErrorCallback(AsyncError error); |
9 | 9 |
10 /** StreamSink that stores incoming data in a collection. */ | 10 /** EventSink that stores incoming data in a collection. */ |
11 class CollectionSink<T> implements StreamSink<T> { | 11 class CollectionSink<T> extends StreamSink<T> { |
| 12 // TODO(8997): Implement EventSink instead. |
12 final Collection<T> collection; | 13 final Collection<T> collection; |
13 final _CollectionSinkCallback<T> _callback; | 14 final _CollectionSinkCallback<T> _callback; |
14 final _CollectionSinkErrorCallback _errorCallback; | 15 final _CollectionSinkErrorCallback _errorCallback; |
15 bool _isClosed = false; | 16 bool _isClosed = false; |
16 | 17 |
17 /** | 18 /** |
18 * Create a sink that stores incoming values in a collection. | 19 * Create a sink that stores incoming values in a collection. |
19 * | 20 * |
20 * The [collection] is the collection to add the values to. | 21 * The [collection] is the collection to add the values to. |
21 * | 22 * |
22 * If [callback] is provided, then it's called with the collection as arugment | 23 * If [callback] is provided, then it's called with the collection as arugment |
23 * when the sink's [close] method is called. | 24 * when the sink's [close] method is called. |
24 */ | 25 */ |
25 CollectionSink(this.collection, | 26 CollectionSink(this.collection, |
26 { void onClose(Collection<T> collection), | 27 { void onClose(Collection<T> collection), |
27 void onError(AsyncError error) }) | 28 void onError(AsyncError error) }) |
28 : this._callback = onClose, | 29 : this._callback = onClose, |
29 this._errorCallback = onError; | 30 this._errorCallback = onError; |
30 | 31 |
31 add(T value) { | 32 add(T value) { |
32 if (_isClosed) throw new StateError("Adding to closed sink"); | 33 if (_isClosed) throw new StateError("Adding to closed sink"); |
33 collection.add(value); | 34 collection.add(value); |
34 } | 35 } |
35 | 36 |
36 void signalError(AsyncError error) { | 37 void addError(AsyncError error) { |
37 if (_isClosed) throw new StateError("Singalling error on closed sink"); | 38 if (_isClosed) throw new StateError("Adding error to closed sink"); |
38 if (_errorCallback != null) _errorCallback(error); | 39 if (_errorCallback != null) _errorCallback(error); |
39 } | 40 } |
40 | 41 |
41 void close() { | 42 void close() { |
42 if (_isClosed) throw new StateError("Closing closed sink"); | 43 if (_isClosed) throw new StateError("Closing closed sink"); |
43 _isClosed = true; | 44 _isClosed = true; |
44 if (_callback != null) _callback(collection); | 45 if (_callback != null) _callback(collection); |
45 } | 46 } |
46 } | 47 } |
OLD | NEW |