shareReplay with refCount: stale replay on resubscribe versus leaked subscription — which trade-off for cached HTTP streams?
0 reputation · 03 Jun 2023, 11:50 UTC
0 reputation · 03 Jun 2023, 11:50 UTC
I am wrapping HTTP calls in a shared service where multiple components subscribe to the same response stream, and I am using shareReplay to avoid duplicate requests. The documented config-object form, shareReplay({ bufferSize, refCount, windowTime }), forces a decision I cannot resolve from the docs alone.
With refCount: false, the underlying subscription appears to stay alive after the last subscriber unsubscribes, which risks a leaked subscription for long-lived services. With refCount: true, the source resubscribes when a new subscriber arrives, but the internal ReplaySubject reportedly still replays previously buffered values first — so a component expecting a fresh fetch may receive stale data before (or instead of) a new request.
Assume RxJS 7.x; I am aware the overloads and defaults differ from pre-6.4 versions, so answers should target the config-object signature. There also seems to be no built-in "reset buffer on resubscribe" option, leaving custom reset logic as the only middle ground.
refCount: true guaranteed by the documented contract, or an implementation detail that could change?bufferSize, refCount, and windowTime is the intended idiom to get "replay while active, refetch after idle" semantics?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.