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.core; | 5 part of dart.async; |
6 /** | |
7 * An interface for an object that can receive a sequence of values. | |
8 */ | |
9 abstract class Sink<T> { | |
10 /** Write a value to the sink. */ | |
11 add(T value); | |
12 /** Tell the sink that no further values will be written. */ | |
13 void close(); | |
14 } | |
15 | |
16 // ---------------------------------------------------------------------- | |
17 // Collections/Sink interoperability | |
18 // ---------------------------------------------------------------------- | |
19 | 6 |
20 typedef void _CollectionSinkCallback<T>(Collection<T> collection); | 7 typedef void _CollectionSinkCallback<T>(Collection<T> collection); |
| 8 typedef void _CollectionSinkErrorCallback(AsyncError error); |
21 | 9 |
22 /** Sink that stores incoming data in a collection. */ | 10 /** StreamSink that stores incoming data in a collection. */ |
23 class CollectionSink<T> implements Sink<T> { | 11 class CollectionSink<T> implements StreamSink<T> { |
24 final Collection<T> collection; | 12 final Collection<T> collection; |
25 final _CollectionSinkCallback<T> callback; | 13 final _CollectionSinkCallback<T> _callback; |
| 14 final _CollectionSinkErrorCallback _errorCallback; |
26 bool _isClosed = false; | 15 bool _isClosed = false; |
27 | 16 |
28 /** | 17 /** |
29 * Create a sink that stores incoming values in a collection. | 18 * Create a sink that stores incoming values in a collection. |
30 * | 19 * |
31 * The [collection] is the collection to add the values to. | 20 * The [collection] is the collection to add the values to. |
32 * | 21 * |
33 * If [callback] is provided, then it's called with the collection as arugment | 22 * If [callback] is provided, then it's called with the collection as arugment |
34 * when the sink's [close] method is called. | 23 * when the sink's [close] method is called. |
35 */ | 24 */ |
36 CollectionSink(this.collection, [void callback(Collection<T> collection)]) | 25 CollectionSink(this.collection, |
37 : this.callback = callback; | 26 { void onClose(Collection<T> collection), |
| 27 void onError(AsyncError error) }) |
| 28 : this._callback = onClose, |
| 29 this._errorCallback = onError; |
38 | 30 |
39 add(T value) { | 31 add(T value) { |
40 if (_isClosed) throw new StateError("Adding to closed sink"); | 32 if (_isClosed) throw new StateError("Adding to closed sink"); |
41 collection.add(value); | 33 collection.add(value); |
42 } | 34 } |
43 | 35 |
| 36 void signalError(AsyncError error) { |
| 37 if (_isClosed) throw new StateError("Singalling error on closed sink"); |
| 38 if (_errorCallback != null) _errorCallback(error); |
| 39 } |
| 40 |
44 void close() { | 41 void close() { |
45 if (_isClosed) throw new StateError("Closing closed sink"); | 42 if (_isClosed) throw new StateError("Closing closed sink"); |
46 _isClosed = true; | 43 _isClosed = true; |
47 if (callback != null) callback(collection); | 44 if (_callback != null) _callback(collection); |
48 } | 45 } |
49 } | 46 } |
OLD | NEW |