| 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 * The basic `ChunkedConversionSink` is just a [Sink], and converters should | |
| 14 * work with a plain `Sink`, but may work more efficiently with certain | |
| 15 * specialized types of `ChunkedConversionSink`. | |
| 16 * | |
| 17 * It is recommended that implementations of `ChunkedConversionSink` extends | |
| 18 * this class, to inherit any further methods that may be added to the class. | |
| 19 */ | |
| 20 abstract class ChunkedConversionSink<T> implements Sink<T> { | |
| 21 ChunkedConversionSink(); | |
| 22 factory ChunkedConversionSink.withCallback( | |
| 23 void callback(List<T> accumulated)) = _SimpleCallbackSink; | |
| 24 | |
| 25 /** | |
| 26 * Adds chunked data to this sink. | |
| 27 * | |
| 28 * This method is also used when converters are used as [StreamTransformer]s. | |
| 29 */ | |
| 30 void add(T chunk); | |
| 31 | |
| 32 /** | |
| 33 * Closes the sink. | |
| 34 * | |
| 35 * This signals the end of the chunked conversion. This method is called | |
| 36 * when converters are used as [StreamTransformer]'s. | |
| 37 */ | |
| 38 void close(); | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * This class accumulates all chunks and invokes a callback with a list of | |
| 43 * the chunks when the sink is closed. | |
| 44 * | |
| 45 * This class can be used to terminate a chunked conversion. | |
| 46 */ | |
| 47 class _SimpleCallbackSink<T> extends ChunkedConversionSink<T> { | |
| 48 final _ChunkedConversionCallback<List<T>> _callback; | |
| 49 final List<T> _accumulated = <T>[]; | |
| 50 | |
| 51 _SimpleCallbackSink(this._callback); | |
| 52 | |
| 53 void add(T chunk) { _accumulated.add(chunk); } | |
| 54 void close() { _callback(_accumulated); } | |
| 55 } | |
| 56 | |
| 57 class _EventSinkAdapter<T> implements ChunkedConversionSink<T> { | |
| 58 final EventSink<T> _sink; | |
| 59 | |
| 60 _EventSinkAdapter(this._sink); | |
| 61 | |
| 62 void add(T data) => _sink.add(data); | |
| 63 void close() => _sink.close(); | |
| 64 } | |
| 65 | |
| 66 /** | |
| 67 * This class converts implements the logic for a chunked conversion as a | |
| 68 * stream transformer. | |
| 69 * | |
| 70 * It is used as strategy in the [EventTransformStream]. | |
| 71 * | |
| 72 * It also implements the [ChunkedConversionSink] interface so that it | |
| 73 * can be used as output sink in a chunked conversion. | |
| 74 */ | |
| 75 class _ConverterStreamEventSink<S, T> implements EventSink<S> { | |
| 76 /** The output sink for the converter. */ | |
| 77 final EventSink<T> _eventSink; | |
| 78 | |
| 79 /** | |
| 80 * The input sink for new data. All data that is received with | |
| 81 * [handleData] is added into this sink. | |
| 82 */ | |
| 83 ChunkedConversionSink _chunkedSink; | |
| 84 | |
| 85 _ConverterStreamEventSink(Converter converter, EventSink<T> sink) | |
| 86 : this._eventSink = sink, | |
| 87 _chunkedSink = converter.startChunkedConversion(sink); | |
| 88 | |
| 89 void add(S o) => _chunkedSink.add(o); | |
| 90 void addError(Object error, [StackTrace stackTrace]) { | |
| 91 _eventSink.addError(error, stackTrace); | |
| 92 } | |
| 93 void close() => _chunkedSink.close(); | |
| 94 } | |
| OLD | NEW |