shareReplay method

ReplayStream<T> shareReplay (
  1. {int maxSize}
)

Convert the current Stream into a new ReplayStream that can be listened to multiple times. It will automatically begin emitting items when first listened to, and shut down when no listeners remain.

This is useful for converting a single-subscription stream into a broadcast Stream. It's also useful for gaining access to the l

It will replay the emitted values to any new listener, up to a given maxSize.

Example

// Convert a single-subscription fromIterable stream into a broadcast
// stream that will emit the latest value to any new listeners
final stream =  Stream.fromIterable([1, 2, 3]).shareReplay();

// Start listening to the source Stream. Will start printing 1, 2, 3
final subscription = stream.listen(print);

// Synchronously print the emitted values up to a given maxSize
// Prints [1, 2, 3]
print(stream.values);

// Subscribe again later. This will print 1, 2, 3 because it receives the
// last emitted value.
final subscription2 = stream.listen(print);

// Stop emitting items from the source stream and close the underlying
// ReplaySubject by cancelling all subscriptions.
subscription.cancel();
subscription2.cancel();

Implementation

ReplayStream<T> shareReplay({int maxSize}) =>
    publishReplay(maxSize: maxSize).refCount();