BACK
ReferenceshareReplay
Reference · 38

shareReplay

shareReplay

rxjs.dev

Share + a cache of the last N values for future subscribers.

Signature

shareReplay(config: { bufferSize: number; refCount: boolean; windowTime?: number }): MonoTypeOperatorFunction

What it does

Like share, but uses a ReplaySubject(bufferSize) — late subscribers instantly get the last bufferSize values. refCount: true auto-unsubscribes the source at zero subscribers. windowTime limits the "freshness" of the cached values.

When to use

Caching HTTP responses (user profile, config, feature flags), a shared state stream (theme$, currentRoute$), any data you "compute once and hand to everyone".

Gotcha

Without refCount: true, the source can stay "hanging" after everyone unsubscribes. In Angular this leaks with shareReplay(1) and no config.

See also

share, ReplaySubject, BehaviorSubject

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...