OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'stream_sink_transformer/handler_transformer.dart'; | 7 import 'stream_sink_transformer/handler_transformer.dart'; |
8 import 'stream_sink_transformer/stream_transformer_wrapper.dart'; | 8 import 'stream_sink_transformer/stream_transformer_wrapper.dart'; |
9 import 'stream_sink_transformer/typed.dart'; | 9 import 'stream_sink_transformer/typed.dart'; |
10 | 10 |
11 /// A [StreamSinkTransformer] transforms the events being passed to a sink. | 11 /// A [StreamSinkTransformer] transforms the events being passed to a sink. |
12 /// | 12 /// |
13 /// This works on the same principle as a [StreamTransformer]. Each transformer | 13 /// This works on the same principle as a [StreamTransformer]. Each transformer |
14 /// defines a [bind] method that takes in the original [StreamSink] and returns | 14 /// defines a [bind] method that takes in the original [StreamSink] and returns |
15 /// the transformed version. However, where a [StreamTransformer] transforms | 15 /// the transformed version. However, where a [StreamTransformer] transforms |
16 /// events after they leave the stream, this transforms them before they enter | 16 /// events after they leave the stream, this transforms them before they enter |
17 /// the sink. | 17 /// the sink. |
18 /// | 18 /// |
19 /// Transformers must be able to have `bind` called used multiple times. | 19 /// Transformers must be able to have `bind` called used multiple times. |
20 abstract class StreamSinkTransformer<S, T> { | 20 abstract class StreamSinkTransformer<S, T> { |
21 /// Creates a [StreamSinkTransformer] that transforms events and errors | 21 /// Creates a [StreamSinkTransformer] that transforms events and errors |
22 /// using [transformer]. | 22 /// using [transformer]. |
23 /// | 23 /// |
24 /// This is equivalent to piping all events from the outer sink through a | 24 /// This is equivalent to piping all events from the outer sink through a |
25 /// stream transformed by [transformer] and from there into the inner sink. | 25 /// stream transformed by [transformer] and from there into the inner sink. |
26 const factory StreamSinkTransformer.fromStreamTransformer( | 26 const factory StreamSinkTransformer.fromStreamTransformer( |
27 StreamTransformer<S, T> transformer) = | 27 StreamTransformer<S, T> transformer) = StreamTransformerWrapper<S, T>; |
28 StreamTransformerWrapper<S, T>; | |
29 | 28 |
30 /// Creates a [StreamSinkTransformer] that delegates events to the given | 29 /// Creates a [StreamSinkTransformer] that delegates events to the given |
31 /// handlers. | 30 /// handlers. |
32 /// | 31 /// |
33 /// The handlers work exactly as they do for [StreamTransformer.fromHandlers]. | 32 /// The handlers work exactly as they do for [StreamTransformer.fromHandlers]. |
34 /// They're called for each incoming event, and any actions on the sink | 33 /// They're called for each incoming event, and any actions on the sink |
35 /// they're passed are forwarded to the inner sink. If a handler is omitted, | 34 /// they're passed are forwarded to the inner sink. If a handler is omitted, |
36 /// the event is passed through unaltered. | 35 /// the event is passed through unaltered. |
37 factory StreamSinkTransformer.fromHandlers({ | 36 factory StreamSinkTransformer.fromHandlers( |
38 void handleData(S data, EventSink<T> sink), | 37 {void handleData(S data, EventSink<T> sink), |
39 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), | 38 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), |
40 void handleDone(EventSink<T> sink)}) { | 39 void handleDone(EventSink<T> sink)}) { |
41 return new HandlerTransformer<S, T>(handleData, handleError, handleDone); | 40 return new HandlerTransformer<S, T>(handleData, handleError, handleDone); |
42 } | 41 } |
43 | 42 |
44 /// Transforms the events passed to [sink]. | 43 /// Transforms the events passed to [sink]. |
45 /// | 44 /// |
46 /// Creates a new sink. When events are passed to the returned sink, it will | 45 /// Creates a new sink. When events are passed to the returned sink, it will |
47 /// transform them and pass the transformed versions to [sink]. | 46 /// transform them and pass the transformed versions to [sink]. |
48 StreamSink<S> bind(StreamSink<T> sink); | 47 StreamSink<S> bind(StreamSink<T> sink); |
49 | 48 |
50 /// Creates a wrapper that coerces the type of [transformer]. | 49 /// Creates a wrapper that coerces the type of [transformer]. |
51 /// | 50 /// |
52 /// This soundly converts a [StreamSinkTransformer] to a | 51 /// This soundly converts a [StreamSinkTransformer] to a |
53 /// `StreamSinkTransformer<S, T>`, regardless of its original generic type. | 52 /// `StreamSinkTransformer<S, T>`, regardless of its original generic type. |
54 /// This means that calls to [StreamSink.add] on the returned sink may throw a | 53 /// This means that calls to [StreamSink.add] on the returned sink may throw a |
55 /// [CastError] if the argument type doesn't match the reified type of the | 54 /// [CastError] if the argument type doesn't match the reified type of the |
56 /// sink. | 55 /// sink. |
57 static StreamSinkTransformer/*<S, T>*/ typed/*<S, T>*/( | 56 static StreamSinkTransformer<S, T> |
58 StreamSinkTransformer transformer) => | 57 typed<S, T>(StreamSinkTransformer transformer) => transformer |
59 transformer is StreamSinkTransformer/*<S, T>*/ | 58 is StreamSinkTransformer<S, T> |
60 ? transformer | 59 ? transformer |
61 : new TypeSafeStreamSinkTransformer(transformer); | 60 : new TypeSafeStreamSinkTransformer(transformer); |
62 } | 61 } |
OLD | NEW |