switchLatest<T> static method

Stream<T> switchLatest<T>(
  1. Stream<Stream<T>> streams
)

Convert a Stream that emits Streams (aka a 'Higher Order Stream') into a single Stream that emits the items emitted by the most-recently-emitted of those Streams.

This Stream will unsubscribe from the previously-emitted Stream when a new Stream is emitted from the source Stream and subscribe to the new Stream.

Example

final switchLatestStream = SwitchLatestStream<String>(
  Stream.fromIterable(<Stream<String>>[
    Rx.timer('A', Duration(seconds: 2)),
    Rx.timer('B', Duration(seconds: 1)),
    Stream.value('C'),
  ]),
);

// Since the first two Streams do not emit data for 1-2 seconds, and the
// 3rd Stream will be emitted before that time, only data from the 3rd
// Stream will be emitted to the listener.
switchLatestStream.listen(print); // prints 'C'

Implementation

static Stream<T> switchLatest<T>(Stream<Stream<T>> streams) =>
    SwitchLatestStream<T>(streams);