@mvuijs/core / rx / shareReplay
shareReplay() #
Very similar to
share
, but resetOnRefCountZero
is false by default and
previous emissions will be replayed for new subscribers.
Relation to share #
This is the entire source code for shareReplay:
export function shareReplay<T>(options?: ShareReplayConfig): OperatorFunction<T, T> {
const _options = {
bufferSize: Infinity, windowTime: Infinity, refCount: false,
...options,
};
return share<T>({
connector: () => new ReplayStream(_options.bufferSize, _options.windowTime),
resetOnError: true,
resetOnComplete: false,
resetOnRefCountZero: _options.refCount,
});
}
Example #
const s = rx.interval(50).pipe(
rx.tap(_ => { count++; }),
rx.shareReplay({ bufferSize: 10 }),
);
s.subscribe(v => console.log('subscription 1: ', v));
await sleep(160);
// logs:
// [nothing for 50ms]
// subscription 1: 0
// [nothing for 50ms]
// subscription 1: 1
// [nothing for 50ms]
// subscription 1: 2
s.subscribe(v => console.log('subscription 2: ', v));
// logs:
// subscription 2: 0
// subscription 2: 1
// subscription 2: 2
// [nothing for 50ms]
// subscription 1: 3
// subscription 2: 3
// [nothing for 50ms]
// subscription 1: 4
// subscription 2: 4
// ...
Signature #
shareReplay<T>(options?: ShareReplayConfig): OperatorFunction<T, T>;
Type parameters #
T
Parameters #
Name | Type |
---|---|
options? |
ShareReplayConfig
|
Returns #
OperatorFunction
<T
, T
>
Defined in: packages/core/src/rx/operators/share.ts:194