Chromium Code Reviews| Index: lib/src/stream_sink_transformer.dart |
| diff --git a/lib/src/stream_sink_transformer.dart b/lib/src/stream_sink_transformer.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..618ec612e888f97a82b6b39259fa7aa9d59ab746 |
| --- /dev/null |
| +++ b/lib/src/stream_sink_transformer.dart |
| @@ -0,0 +1,49 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library async.stream_sink_transformer; |
| + |
| +import 'dart:async'; |
| + |
| +import 'stream_sink_transformer/handler_transformer.dart'; |
| +import 'stream_sink_transformer/stream_transformer_wrapper.dart'; |
| + |
| +/// A [StreamSinkTransformer] transforms the events being passed to a sink. |
| +/// |
| +/// This works on the same principle as a [StreamTransformer]. Each transformer |
| +/// defines a [bind] method that takes in the original [StreamSink] and returns |
| +/// the transformed version. However, where a [StreamTransformer] transforms |
| +/// events after they leave the stream, this transforms them before they enter |
| +/// the sink. |
| +/// |
| +/// It's good practice to write transformers that can be used multiple times. |
|
Lasse Reichstein Nielsen
2016/01/07 07:37:01
Not sure what this line says?
I *think* it means t
nweiz
2016/01/07 21:32:26
Done.
I copied this line almost verbatim from the
Lasse Reichstein Nielsen
2016/01/08 07:23:06
Good point, I'll look at that.
|
| +abstract class StreamSinkTransformer<S, T> { |
|
Lasse Reichstein Nielsen
2016/01/07 07:37:01
Do we really need a class?
It has exactly one inst
nweiz
2016/01/07 21:32:26
My biggest worry with that is how inconsistent it
Lasse Reichstein Nielsen
2016/01/08 07:23:06
True. StreamTransformer is used on other classes t
|
| + /// Creates a [StreamSinkTransformer] that transforms events and errors |
| + /// using [transformer]. |
| + /// |
| + /// This is equivalent to piping all events from the outer sink through a |
| + /// stream transformed by [transformer] and from there into the inner sink. |
| + const factory StreamSinkTransformer.fromStreamTransformer( |
| + StreamTransformer<S, T> transformer) = |
| + StreamTransformerWrapper<S, T>; |
| + |
| + /// Creates a [StreamSinkTransformer] that delegates events to the given |
| + /// handlers. |
| + /// |
| + /// The handlers work exactly as they do for [StreamTransformer.fromHandlers]. |
| + /// They're called for each incoming event, and any actions on the sink |
| + /// they're passed are forwarded to the inner sink. |
|
Lasse Reichstein Nielsen
2016/01/07 07:37:01
Specify what happens if a handler is omitted (it s
nweiz
2016/01/07 21:32:26
Done.
|
| + factory StreamSinkTransformer.fromHandlers({ |
| + void handleData(S data, EventSink<T> sink), |
| + void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), |
| + void handleDone(EventSink<T> sink)}) { |
| + return new HandlerTransformer<S, T>(handleData, handleError, handleDone); |
| + } |
| + |
| + /// Transforms the events passed to [sink]. |
| + /// |
| + /// Creates a new sink. When events are passed to the returned sink, it will |
| + /// transform them and pass the transformed versions to [sink]. |
| + StreamSink<S> bind(StreamSink<T> sink); |
| +} |