OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.convert; |
| 6 |
| 7 typedef void _ChunkedConversionCallback<T>(T accumulated); |
| 8 |
| 9 /** |
| 10 * A [ChunkedConversionSink] is used to transmit data more efficiently between |
| 11 * two converters during chunked conversions. |
| 12 */ |
| 13 abstract class ChunkedConversionSink<T> { |
| 14 ChunkedConversionSink(); |
| 15 factory ChunkedConversionSink.withCallback( |
| 16 void callback(List<T> accumulated)) = _SimpleCallbackSink; |
| 17 |
| 18 /** |
| 19 * Adds chunked data to this sink. |
| 20 * |
| 21 * This method is also used when converters are used as [StreamTransformer]s. |
| 22 */ |
| 23 void add(T chunk); |
| 24 |
| 25 /** |
| 26 * Closes the sink. |
| 27 * |
| 28 * This signals the end of the chunked conversion. This method is called |
| 29 * when converters are used as [StreamTransformer]'s. |
| 30 */ |
| 31 void close(); |
| 32 } |
| 33 |
| 34 /** |
| 35 * This class accumulates all chunks and invokes a callback with a list of |
| 36 * the chunks when the sink is closed. |
| 37 * |
| 38 * This class can be used to terminate a chunked conversion. |
| 39 */ |
| 40 class _SimpleCallbackSink<T> extends ChunkedConversionSink<T> { |
| 41 final _ChunkedConversionCallback<List<T>> _callback; |
| 42 final List<T> _accumulated = <T>[]; |
| 43 |
| 44 _SimpleCallbackSink(this._callback); |
| 45 |
| 46 void add(T chunk) { _accumulated.add(chunk); } |
| 47 void close() { _callback(_accumulated); } |
| 48 } |
OLD | NEW |