fix(subscription): delete bookkeeping Map entries on last unmount#4262
Draft
spokodev wants to merge 1 commit into
Draft
fix(subscription): delete bookkeeping Map entries on last unmount#4262spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4261.
The cleanup callback at
src/subscription/index.ts:84-93callsdispose()when the last subscriber for a key unmounts, but never removes the matching entries from thesubscriptionsanddisposersMaps. In a long-lived app that cycles through many distinct subscription keys (paginated feeds, rotating live-data channels, user-navigated rooms), both Maps accumulate one dead entry per unique key and grow without bound.This change deletes the entry from both Maps right after calling
dispose():The functional behaviour is unchanged: a subsequent re-mount for the same key still hits the
if (!refCount)branch (becausesubscriptions.get(key) || 0is0whether the entry is missing or zero), so a fresh subscribe runs and a fresh disposer is registered.Tests
test/use-swr-subscription.test.tsxgets ateardown cleans up bookkeeping Maps (#4261)describe block with 7 new cases covering both axes of the fix.Because the bookkeeping Maps are module-private (closed over inside
subscription/index.tsvia a per-cacheWeakMap), a minimal@internalhelper is exported so the tests can read their sizes:The naming follows the standard
_unstable_prefix convention and the helper is annotated@internalso it does not enter the documented public API. Suggestions on a better location welcome.Positive (the fix's contract)
Negative (existing behavior preserved)
dispose()is still called exactly once on the final unmount.Edge
SWRConfig's subscription does not touch the bookkeeping of anotherSWRConfig.All 7 fail on
mainagainst the relevant assertions and pass on this branch. The 7 pre-existing tests in the same file are unchanged and still pass.Verification
Opened as draft until CI is green.