fix: use latest reuseMaps value in map instance cleanup - #1042
Open
zigzagdev wants to merge 2 commits into
Open
Conversation
reuseMaps was read directly from the effect's closure, but the effect intentionally excludes it from its dependency array (toggling it alone shouldn't recreate the map). As a result, if reuseMaps changed between mount and unmount, the cleanup used the stale value captured at mount, either leaking an instance onto the static cache stack or discarding one that should have been cached. Track it in a ref that's kept current via its own effect, and read that ref in both the create and cleanup paths instead.
Adds a regression test for the stale-closure bug fixed in the previous commit: mounts with reuseMaps enabled, rerenders with it disabled (without changing mapId/renderingType/colorScheme, so the map-creation effect intentionally doesn't rerun), then asserts that unmounting clears the instance's listeners instead of pushing it onto the cache stack.
zigzagdev
marked this pull request as draft
August 3, 2026 11:44
zigzagdev
marked this pull request as ready for review
August 3, 2026 13:34
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.
Summary
Fixes a stale-closure bug in
useMapInstancewhere toggling thereuseMapsprop right before unmounting a<Map>would silently do the wrong thing: an instance meant to be discarded could get cached indefinitely (leak), or an instance meant to be cached could have its listeners cleared instead.What I have done
reuseMapsRefinsrc/components/map/use-map-instance.ts, kept current via its ownuseEffect, and read it (instead of the closed-overreuseMapsprop) in both the map-creation and cleanup logic of the main effect.eslint-disable-next-line react-hooks/set-state-in-effectcomment that became unused once the new effect was added above it.src/components/__tests__/map.test.tsxthat mounts withreuseMaps={true}, rerenders withreuseMaps={false}(keepingmapId/renderingType/colorSchemeunchanged, since the effect intentionally doesn't rerun onreuseMapsalone), then unmounts and assertsclearInstanceListenerswas called instead of the instance being pushed onto the internal cache stack.Motivation
useMapInstance's map-creation effect intentionally excludesreuseMapsfrom its dependency array — toggling that prop alone shouldn't force the map to be torn down and recreated.However, the effect's cleanup closure was reading
reuseMapsdirectly, so it kept using whatever value was captured the last time the effect actually ran, not the current prop.Concretely: mount with
reuseMaps={true}, later switch toreuseMaps={false}, then unmount — the cleanup still saw the staletrueand pushed the map instance onto the module-levelCachedMapStack, leaking it indefinitely instead of clearing its listeners as intended.Test Plans
reuseMapsis toggled off (with the other cache-key props unchanged) callsclearInstanceListenersrather than caching the instance.map.test.tsxsuite and the fulljestsuite to check for regressions.eslintandtsc --noEmiton the changed files.Test Results
Note: verification was run locally inside a
node:22-bullseyeDocker container (npm install+jest/eslint/tsc) — this Docker setup is local only and was not pushed to the repository.