Stream.eventTransformed(Stream source, EventSink mapSink(EventSink<T> sink))

Creates a stream where all events of an existing stream are piped through a sink-transformation.

The given mapSink closure is invoked when the returned stream is listened to. All events from the source are added into the event sink that is returned from the invocation. The transformation puts all transformed events into the sink the mapSink closure received during its invocation. Conceptually the mapSink creates a transformation pipe with the input sink being the returned EventSink and the output sink being the sink it received.

This constructor is frequently used to build transformers.

Example use for a duplicating transformer:

class DuplicationSink implements EventSink<String> {
  final EventSink<String> _outputSink;
  DuplicationSink(this._outputSink);

  void add(String data) {
    _outputSink.add(data);
    _outputSink.add(data);
  }

  void addError(e, [st]) { _outputSink.addError(e, st); }
  void close() { _outputSink.close(); }
}

class DuplicationTransformer implements StreamTransformer<String, String> {
  // Some generic types ommitted for brevety.
  Stream bind(Stream stream) => new Stream<String>.eventTransformed(
      stream,
      (EventSink sink) => new DuplicationSink(sink));
}

stringStream.transform(new DuplicationTransformer());

The resulting stream is a broadcast stream if source is.

Source

factory Stream.eventTransformed(
    Stream source, EventSink mapSink(EventSink<T> sink)) {
  return new _BoundSinkStream(source, mapSink);
}