share()

@mvuijs/core / rx / share

share() #

Share a Stream, such that multiple subscribers will subscribe to the same underlying Stream. On a basic level, one can think of the share operator as transforming a Stream to a MulticastStream (which see for more explanations).

Example #

const s = rx.interval(100).pipe(
  rx.delay(500),
  rx.share(),
);

s.subscribe(v => console.log('subscription 1: ', v));
await sleep(1000);

// logs:
// [nothing for 600ms]
// subscriber 1: 0
// [nothing for 100ms]
// subscriber 1: 1
// [nothing for 100ms]
// subscriber 1: 2
// [nothing for 100ms]

s.subscribe(v => console.log('subscription 2: ', v));

// logs:
// subscriber 1: 3
// subscriber 2: 3
// [nothing for 100ms]
// subscriber 1: 4
// subscriber 2: 4
// ...

// notice how there is no delay after subscribing for a second time. without the
// `share` operator, there would have been.

Signature #

share<T>(options?: ShareConfig<T>): OperatorFunction<T, T>;

Type parameters #

  • T

Parameters #

Name Type
options? ShareConfig <T>

Returns #

OperatorFunction <T, T>

Defined in: packages/core/src/rx/operators/share.ts:66