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 | 10 |
10 /// A [StreamSinkTransformer] transforms the events being passed to a sink. | 11 /// A [StreamSinkTransformer] transforms the events being passed to a sink. |
11 /// | 12 /// |
12 /// This works on the same principle as a [StreamTransformer]. Each transformer | 13 /// This works on the same principle as a [StreamTransformer]. Each transformer |
13 /// 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 |
14 /// the transformed version. However, where a [StreamTransformer] transforms | 15 /// the transformed version. However, where a [StreamTransformer] transforms |
15 /// 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 |
16 /// the sink. | 17 /// the sink. |
17 /// | 18 /// |
18 /// Transformers must be able to have `bind` called used multiple times. | 19 /// Transformers must be able to have `bind` called used multiple times. |
(...skipping 19 matching lines...) Expand all Loading... |
38 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), | 39 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), |
39 void handleDone(EventSink<T> sink)}) { | 40 void handleDone(EventSink<T> sink)}) { |
40 return new HandlerTransformer<S, T>(handleData, handleError, handleDone); | 41 return new HandlerTransformer<S, T>(handleData, handleError, handleDone); |
41 } | 42 } |
42 | 43 |
43 /// Transforms the events passed to [sink]. | 44 /// Transforms the events passed to [sink]. |
44 /// | 45 /// |
45 /// Creates a new sink. When events are passed to the returned sink, it will | 46 /// Creates a new sink. When events are passed to the returned sink, it will |
46 /// transform them and pass the transformed versions to [sink]. | 47 /// transform them and pass the transformed versions to [sink]. |
47 StreamSink<S> bind(StreamSink<T> sink); | 48 StreamSink<S> bind(StreamSink<T> sink); |
| 49 |
| 50 /// Creates a wrapper that coerces the type of [transformer]. |
| 51 /// |
| 52 /// This soundly converts a [StreamSinkTransformer] to a |
| 53 /// `StreamSinkTransformer<S, T>`, regardless of its original generic type. |
| 54 /// 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 |
| 56 /// sink. |
| 57 static StreamSinkTransformer/*<S, T>*/ typed/*<S, T>*/( |
| 58 StreamSinkTransformer transformer) => |
| 59 transformer is StreamSinkTransformer/*<S, T>*/ |
| 60 ? transformer |
| 61 : new TypeSafeStreamSinkTransformer(transformer); |
48 } | 62 } |
OLD | NEW |