Skip to content
Source
Sign inSign up
TanStack/query

fix(query-devtools): coalesce query and mutation cache events per microtask so 'clear' and 'removeQueries' update the lists once #12706

Mergedsukvvon(GitHub profile, opens in a new tab)merged 11 commits intomain fromrefs/pull/12706/headmerged by sukvvon(GitHub profile, opens in a new tab) View on GitHub

sukvvon(GitHub profile, opens in a new tab)commented

🎯 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 Solid batch. queryClient.clear() and queryClient.removeQueries() remove queries one by one and emit a removed event 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 setupMutationCacheSubscription the same way, through a shared createCoalescedSchedule helper. Before, each matching update ran in its own microtask, so queryClient.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 idle state is no longer applied, "Restore Loading" now stays disabled until the restored fetch finishes, as its restoringLoading signal intends. Add a test for it.
  • Remove the batch around query_cache().clear() in the clear cache button handler. The coalesced updates already run in one batch, so it no longer does anything.
  • Pass the subscription id to schedule in createCacheSubscriptionRegistry's notify, so repeated updates of the same subscription can be merged. Make schedule required, since both subscriptions now pass one.
  • Add tests that call queryClient.clear() with 1000 queries and queryClient.removeQueries() for 500 of them, and check that the list content's style changes 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:

CaseBeforeAfter
clear(), 250 queries24–63ms5–7ms
clear(), 1000 queries192–214ms11–13ms
clear(), 2000 queries508–516ms14–17ms
removeQueries() half, 1000 queries135–139ms5–10ms
removeQueries() half, 2000 queries330–337ms7–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:

CaseBeforeAfter
clear(), 250 mutations9–11ms1–2ms
clear(), 1000 mutations31–35ms2–3ms
clear(), 2000 mutations64–65ms4–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.

Sign in to join this conversation.