share

function stable

Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one Subscriber this Observable will be subscribed and emitting data. When all subscribers have unsubscribed it will unsubscribe from the source Observable. Because the Observable is multicasting it makes the stream hot. This is an alias for multicast(() => new Subject()), refCount().

share<T>(options: ShareConfig<T> = {}): MonoTypeOperatorFunction<T>

Parameters

options

Optional. Default is {}.

Type: ShareConfig.

Returns

MonoTypeOperatorFunction<T>: A function that returns an Observable that mirrors the source.

Description

The subscription to the underlying source Observable can be reset (unsubscribe and resubscribe for new subscribers), if the subscriber count to the shared observable drops to 0, or if the source Observable errors or completes. It is possible to use notifier factories for the resets to allow for behaviors like conditional or delayed resets. Please note that resetting on error or complete of the source Observable does not behave like a transparent retry or restart of the source because the error or complete will be forwarded to all subscribers and their subscription will be closed. Only new subscribers after a reset on error or complete happened will cause a fresh subscription to the source. To achieve transparent retries or restarts pipe the source through appropriate operators before sharing.

share marble diagram

Example

Generate new multicast Observable from the source Observable value

import { interval } from 'rxjs';
import { share, map } from 'rxjs/operators';

const source = interval(1000)
  .pipe(
        map((x: number) => {
            console.log('Processing: ', x);
            return x*x;
        }),
        share()
);

source.subscribe(x => console.log('subscription 1: ', x));
source.subscribe(x => console.log('subscription 1: ', x));

// Logs:
// Processing:  0
// subscription 1:  0
// subscription 1:  0
// Processing:  1
// subscription 1:  1
// subscription 1:  1
// Processing:  2
// subscription 1:  4
// subscription 1:  4
// Processing:  3
// subscription 1:  9
// subscription 1:  9
// ... and so on

Example with notifier factory: Delayed reset

import { interval } from 'rxjs';
import { share, take, timer } from 'rxjs/operators';

const source = interval(1000).pipe(take(3), share({ resetOnRefCountZero: () => timer(1000) }));

const subscriptionOne = source.subscribe(x => console.log('subscription 1: ', x));
setTimeout(() => subscriptionOne.unsubscribe(), 1300);

setTimeout(() => source.subscribe(x => console.log('subscription 2: ', x)), 1700);

setTimeout(() => source.subscribe(x => console.log('subscription 3: ', x)), 5000);

// Logs:
// subscription 1:  0
// (subscription 1 unsubscribes here)
// (subscription 2 subscribes here ~400ms later, source was not reset)
// subscription 2:  1
// subscription 2:  2
// (subscription 2 unsubscribes here)
// (subscription 3 subscribes here ~2000ms later, source did reset before)
// subscription 3:  0
// subscription 3:  1
// subscription 3:  2

See Also

© 2015–2021 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/share