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