| 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.convert; | 5 part of dart.convert; |
| 6 | 6 |
| 7 typedef void _ChunkedConversionCallback<T>(T accumulated); | 7 typedef void _ChunkedConversionCallback<T>(T accumulated); |
| 8 | 8 |
| 9 /** | 9 /// This class is deprecated. Extend [Converter] directly. |
| 10 * A converter that supports chunked conversions. | 10 @deprecated |
| 11 * | |
| 12 * In addition to immediate conversions from [S] to [T], a chunked converter | |
| 13 * also supports longer-running conversions from [S2] to [T2]. | |
| 14 * | |
| 15 * Frequently, the source and target types are the same, but this is not a | |
| 16 * requirement. In particular, converters that work with lists in the | |
| 17 * immediate conversion, could flatten the type for the chunked conversion. | |
| 18 * | |
| 19 * For example, the [LineSplitter] class returns a `List<String>` for the | |
| 20 * immediate conversion, but returns individual `String`s in the chunked | |
| 21 * conversion. | |
| 22 */ | |
| 23 abstract class ChunkedConverter<S, T, S2, T2> extends Converter<S, T> { | 11 abstract class ChunkedConverter<S, T, S2, T2> extends Converter<S, T> { |
| 12 const ChunkedConverter(): super(); |
| 24 | 13 |
| 25 const ChunkedConverter(); | 14 dynamic bind(dynamic other) => super.bind(other); |
| 26 | 15 dynamic startChunkedConversion(dynamic sink) => |
| 27 /** | 16 super.startChunkedConversion(sink); |
| 28 * Starts a chunked conversion. | |
| 29 * | |
| 30 * The returned sink serves as input for the long-running conversion. The | |
| 31 * given [sink] serves as output. | |
| 32 */ | |
| 33 ChunkedConversionSink<S2> startChunkedConversion(Sink<T2> sink) { | |
| 34 throw new UnsupportedError( | |
| 35 "This converter does not support chunked conversions: $this"); | |
| 36 } | |
| 37 | |
| 38 Stream<T2> bind(Stream<S2> stream) { | |
| 39 return new Stream<T2>.eventTransformed( | |
| 40 stream, | |
| 41 (EventSink<T2> sink) => | |
| 42 new _ConverterStreamEventSink<S2, T2>(this, sink)); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Fuses this instance with the given [other] converter. | |
| 47 * | |
| 48 * If [other] is a ChunkedConverter (with matching generic types), returns a | |
| 49 * [ChunkedConverter]. | |
| 50 */ | |
| 51 Converter<S, dynamic> fuse(Converter<T, dynamic> other) { | |
| 52 if (other is ChunkedConverter<T, dynamic, T2, dynamic>) { | |
| 53 return new _FusedChunkedConverter<S, T, dynamic, S2, T2, dynamic>( | |
| 54 this, other); | |
| 55 } | |
| 56 return super.fuse(other); | |
| 57 } | |
| 58 } | 17 } |
| 59 | 18 |
| 60 /** | 19 /** |
| 61 * A [ChunkedConversionSink] is used to transmit data more efficiently between | 20 * A [ChunkedConversionSink] is used to transmit data more efficiently between |
| 62 * two converters during chunked conversions. | 21 * two converters during chunked conversions. |
| 63 * | 22 * |
| 64 * The basic `ChunkedConversionSink` is just a [Sink], and converters should | 23 * The basic `ChunkedConversionSink` is just a [Sink], and converters should |
| 65 * work with a plain `Sink`, but may work more efficiently with certain | 24 * work with a plain `Sink`, but may work more efficiently with certain |
| 66 * specialized types of `ChunkedConversionSink`. | 25 * specialized types of `ChunkedConversionSink`. |
| 67 * | 26 * |
| 68 * It is recommended that implementations of `ChunkedConversionSink` extends | 27 * It is recommended that implementations of `ChunkedConversionSink` extend |
| 69 * this class, to inherit any further methods that may be added to the class. | 28 * this class, to inherit any further methods that may be added to the class. |
| 70 */ | 29 */ |
| 71 abstract class ChunkedConversionSink<T> implements Sink<T> { | 30 abstract class ChunkedConversionSink<T> implements Sink<T> { |
| 72 ChunkedConversionSink(); | 31 ChunkedConversionSink(); |
| 73 factory ChunkedConversionSink.withCallback( | 32 factory ChunkedConversionSink.withCallback( |
| 74 void callback(List<T> accumulated)) = _SimpleCallbackSink; | 33 void callback(List<T> accumulated)) = _SimpleCallbackSink<T>; |
| 75 | 34 |
| 76 /** | 35 /** |
| 77 * Adds chunked data to this sink. | 36 * Adds chunked data to this sink. |
| 78 * | 37 * |
| 79 * This method is also used when converters are used as [StreamTransformer]s. | 38 * This method is also used when converters are used as [StreamTransformer]s. |
| 80 */ | 39 */ |
| 81 void add(T chunk); | 40 void add(T chunk); |
| 82 | 41 |
| 83 /** | 42 /** |
| 84 * Closes the sink. | 43 * Closes the sink. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 115 * can be used as output sink in a chunked conversion. | 74 * can be used as output sink in a chunked conversion. |
| 116 */ | 75 */ |
| 117 class _ConverterStreamEventSink<S, T> implements EventSink<S> { | 76 class _ConverterStreamEventSink<S, T> implements EventSink<S> { |
| 118 /** The output sink for the converter. */ | 77 /** The output sink for the converter. */ |
| 119 final EventSink<T> _eventSink; | 78 final EventSink<T> _eventSink; |
| 120 | 79 |
| 121 /** | 80 /** |
| 122 * The input sink for new data. All data that is received with | 81 * The input sink for new data. All data that is received with |
| 123 * [handleData] is added into this sink. | 82 * [handleData] is added into this sink. |
| 124 */ | 83 */ |
| 125 final ChunkedConversionSink<S> _chunkedSink; | 84 final Sink<S> _chunkedSink; |
| 126 | 85 |
| 127 _ConverterStreamEventSink( | 86 _ConverterStreamEventSink( |
| 128 Converter/*=ChunkedConverter<dynamic, dynamic, S, T>*/ converter, | 87 Converter/*=Converter<S, T>*/ converter, |
| 129 EventSink<T> sink) | 88 EventSink<T> sink) |
| 130 : this._eventSink = sink, | 89 : this._eventSink = sink, |
| 131 _chunkedSink = converter.startChunkedConversion(sink); | 90 _chunkedSink = converter.startChunkedConversion(sink); |
| 132 | 91 |
| 133 void add(S o) { _chunkedSink.add(o); } | 92 void add(S o) { _chunkedSink.add(o); } |
| 134 void addError(Object error, [StackTrace stackTrace]) { | 93 void addError(Object error, [StackTrace stackTrace]) { |
| 135 _eventSink.addError(error, stackTrace); | 94 _eventSink.addError(error, stackTrace); |
| 136 } | 95 } |
| 137 void close() { _chunkedSink.close(); } | 96 void close() { _chunkedSink.close(); } |
| 138 } | 97 } |
| 139 | |
| 140 /** | |
| 141 * Fuses two chunked converters. | |
| 142 */ | |
| 143 class _FusedChunkedConverter<S, M, T, S2, M2, T2> extends | |
| 144 ChunkedConverter<S, T, S2, T2> { | |
| 145 final ChunkedConverter<S, M, S2, M2> _first; | |
| 146 final ChunkedConverter<M, T, M2, T2> _second; | |
| 147 | |
| 148 _FusedChunkedConverter(this._first, this._second); | |
| 149 | |
| 150 T convert(S input) => _second.convert(_first.convert(input)); | |
| 151 | |
| 152 ChunkedConversionSink<S2> startChunkedConversion(Sink<T2> sink) { | |
| 153 return _first.startChunkedConversion(_second.startChunkedConversion(sink)); | |
| 154 } | |
| 155 } | |
| OLD | NEW |