Skip to main content

shareObservablePipe

Alternative: share$$$

Inlined: shareObservable, share$$

Types

function shareObservablePipe<GValue>(
options?: IShareObservableOptions<GValue>,
): IObservablePipe<GValue, GValue>

Definition

This ObservablePipe uses the function sourceObservablePipe with a IMulticastSource.

It counts the number of subscriptions and subscribes (only once) to the original Observable if the number of subscriptions is equal to 1. The received value and all the following are then emitted to all the subscribers.

If the number of subscriptions is equal to 0, it unsubscribes from the Observable.

In one line: this pipe subscribes to the original observable on the first subscription, and unsubscribes of it when no one remains, while sharing all the received values.

This is partially equivalent to the multicast and refCount operators, but it is more generic.

See sourceObservablePipe and createMulticastSource.

caution

You will probably never user directly this ObservablePipe, instead you may use: shareObservablePipeWithMulticastReplaySource or shareObservablePipeWithMulticastReplayLastSource.

Example

Sharing the same Observable

const subscribe = pipe$$(interval(1000), [
scan$$$<void, number>(count => (count + 1), 0),
share$$$<number>(),
]);

subscribe((value: number) => {
console.log('value - A:', value);
});

subscribe((value: number) => {
console.log('value - B:', value);
});

Output:

value - A: 0
value - B: 0
value - A: 1
value - B: 1
...