Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library async.stream_sink_transformer; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'stream_sink_transformer/handler_transformer.dart'; | |
| 10 import 'stream_sink_transformer/stream_transformer_wrapper.dart'; | |
| 11 | |
| 12 /// A [StreamSinkTransformer] transforms the events being passed to a sink. | |
| 13 /// | |
| 14 /// This works on the same principle as a [StreamTransformer]. Each transformer | |
| 15 /// defines a [bind] method that takes in the original [StreamSink] and returns | |
| 16 /// the transformed version. However, where a [StreamTransformer] transforms | |
| 17 /// events after they leave the stream, this transforms them before they enter | |
| 18 /// the sink. | |
| 19 /// | |
| 20 /// Transformers must be able to be used multiple times. | |
|
Lasse Reichstein Nielsen
2016/01/08 07:23:06
-> Transformers must be able to have `bind` called
nweiz
2016/01/11 21:06:14
Done.
| |
| 21 abstract class StreamSinkTransformer<S, T> { | |
| 22 /// Creates a [StreamSinkTransformer] that transforms events and errors | |
| 23 /// using [transformer]. | |
| 24 /// | |
| 25 /// This is equivalent to piping all events from the outer sink through a | |
| 26 /// stream transformed by [transformer] and from there into the inner sink. | |
| 27 const factory StreamSinkTransformer.fromStreamTransformer( | |
| 28 StreamTransformer<S, T> transformer) = | |
| 29 StreamTransformerWrapper<S, T>; | |
| 30 | |
| 31 /// Creates a [StreamSinkTransformer] that delegates events to the given | |
| 32 /// handlers. | |
| 33 /// | |
| 34 /// The handlers work exactly as they do for [StreamTransformer.fromHandlers]. | |
| 35 /// They're called for each incoming event, and any actions on the sink | |
| 36 /// they're passed are forwarded to the inner sink. If a handler is omitted, | |
| 37 /// the event is passed through unaltered. | |
| 38 factory StreamSinkTransformer.fromHandlers({ | |
| 39 void handleData(S data, EventSink<T> sink), | |
| 40 void handleError(Object error, StackTrace stackTrace, EventSink<T> sink), | |
| 41 void handleDone(EventSink<T> sink)}) { | |
| 42 return new HandlerTransformer<S, T>(handleData, handleError, handleDone); | |
| 43 } | |
| 44 | |
| 45 /// Transforms the events passed to [sink]. | |
| 46 /// | |
| 47 /// Creates a new sink. When events are passed to the returned sink, it will | |
| 48 /// transform them and pass the transformed versions to [sink]. | |
| 49 StreamSink<S> bind(StreamSink<T> sink); | |
| 50 } | |
| OLD | NEW |