fix(query-devtools): coalesce query and mutation cache events per microtask so 'clear' and 'removeQueries' update the lists once #12706
Merged
sukvvon(GitHub profile, opens in a new tab)merged 11 commits into
main fromrefs/pull/12706/head·merged by sukvvon(GitHub profile, opens in a new tab) View on GitHub🎯 Changes#
- Coalesce query cache events in
setupQueryCacheSubscription. Each event queues the matching subscription updates by subscription id, and a single microtask runs them all inside one Solidbatch.queryClient.clear()andqueryClient.removeQueries()remove queries one by one and emit aremovedevent for each, so the query list was recomputed and re-rendered once per removed query when app code called them. Now the list updates once. Every subscription callback reads the cache when it runs, so skipping the intermediate events doesn't change the result. - Clear the pending updates when the subscription is cleaned up, so updates queued for the previous client don't apply after the client changes. Add a test that switches the client right after a cache change.
- Coalesce mutation cache events in
setupMutationCacheSubscriptionthe same way, through a sharedcreateCoalescedSchedulehelper. Before, each matching update ran in its own microtask, soqueryClient.clear()with 1000 mutations read the mutation cache about 3000 times; now it reads it once per subscription. Add a test that checks clearing 1000 mutations reads the mutation cache as often as clearing one. - Because the intermediate
idlestate is no longer applied, "Restore Loading" now stays disabled until the restored fetch finishes, as itsrestoringLoadingsignal intends. Add a test for it. - Remove the
batcharoundquery_cache().clear()in the clear cache button handler. The coalesced updates already run in onebatch, so it no longer does anything. - Pass the subscription id to
scheduleincreateCacheSubscriptionRegistry'snotify, so repeated updates of the same subscription can be merged. Makeschedulerequired, since both subscriptions now pass one. - Add tests that call
queryClient.clear()with 1000 queries andqueryClient.removeQueries()for 500 of them, and check that the list content'sstylechanges once. Without the fix it changes 1000 and 500 times. - Await the microtask in existing tests that assert right after a cache change.
Measured in Chromium with a production build of examples/react/simple (using @tanstack/react-query-devtools/production), timing a direct queryClient.clear() / queryClient.removeQueries() call until the devtools update finishes, 3 runs each:
| Case | Before | After |
|---|---|---|
clear(), 250 queries | 24–63ms | 5–7ms |
clear(), 1000 queries | 192–214ms | 11–13ms |
clear(), 2000 queries | 508–516ms | 14–17ms |
removeQueries() half, 1000 queries | 135–139ms | 5–10ms |
removeQueries() half, 2000 queries | 330–337ms | 7–14ms |
For the mutation cache, measured the same way with the Mutations view open, timing mutationCache.clear() until the devtools update finishes, 3 runs each. Before is the commit before the mutation change:
| Case | Before | After |
|---|---|---|
clear(), 250 mutations | 9–11ms | 1–2ms |
clear(), 1000 mutations | 31–35ms | 2–3ms |
clear(), 2000 mutations | 64–65ms | 4–5ms |
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- The query list now refreshes once when queries are cleared or removed, reducing redundant updates.
- Query list updates are more consistent after cache changes, and pending updates from a previous client no longer appear after switching clients.
- Clearing mutations refreshes the displayed list efficiently, even when many mutations are present.
- “Restore Loading” remains disabled until the restored fetch completes.
- 1
- 2
- 3
Pushed4d0836d
sukvvon(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.