publishReplay method

ReplayConnectableObservable<T> publishReplay ({int maxSize })

Convert the current Observable into a ReplayConnectableObservable that can be listened to multiple times. It will not begin emitting items from the original Observable until the connect method is invoked.

This is useful for converting a single-subscription stream into a broadcast Stream that replays a given number of items to any new listener. It also provides access to the emitted values synchronously.


final source = Observable.fromIterable([1, 2, 3]);
final connectable = source.publishReplay();

// Does not print anything at first

// Start listening to the source Observable. Will cause the previous
// line to start printing 1, 2, 3
final subscription = connectable.connect();

// Late subscribers will receive the emitted value, up to a specified
// maxSize
connectable.listen(print); // Prints 1, 2, 3

// Can access a list of the emitted values synchronously. Prints [1, 2, 3]

// Stop emitting items from the source stream and close the underlying
// ReplaySubject


ReplayConnectableObservable<T> publishReplay({int maxSize}) =>
    ReplayConnectableObservable<T>(this, maxSize: maxSize);