share method

Observable<T> share ()

Convert the current Observable into a new Observable 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.

Example

// Convert a single-subscription fromIterable stream into a broadcast
// stream
final observable = Observable.fromIterable([1, 2, 3]).share();

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

// Stop emitting items from the source stream and close the underlying
// PublishSubject
subscription.cancel();

Implementation

Observable<T> share() => publish().refCount();