From fcd37a9d166b1e0381c05932392ded467c4e5a04 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 13:17:53 +0900 Subject: [PATCH] perf(query-cache): invalidate namespaces in parallel (Q-6.3-8) TanStack 5 invalidateQueries resolves only after the active refetches finish. The coordinator awaited each namespace in a for loop, so one invalidation (and lease.release(), which waits on it) took the sum of every namespace's refetch time. The namespaces now run through Promise.allSettled; each rejected one is still reported once as "invalidate". Verified: new test holds the first namespace's refetch open and failed RED (second invalidateQueries never started); it now passes and checks that a failure is reported once. All lane gates pass except the known removal-fixture test; provider-guardian-transaction timed out once under load and passed on rerun. Co-Authored-By: Claude Opus 5.5 --- .../query-cache/tanstack-cache-coordinator.ts | 17 ++++++---- tests/unit/tanstack-cache-coordinator.test.ts | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/adapters/query-cache/tanstack-cache-coordinator.ts b/src/adapters/query-cache/tanstack-cache-coordinator.ts index 8855fa56..305db5d7 100644 --- a/src/adapters/query-cache/tanstack-cache-coordinator.ts +++ b/src/adapters/query-cache/tanstack-cache-coordinator.ts @@ -76,16 +76,19 @@ export function createTanStackCacheCoordinator( namespaces.set(queryNamespaceIdentityKey(namespace), namespace); } } - for (const namespace of namespaces.values()) { - try { - await dependencies.queryClient.invalidateQueries({ + // Q-6.3-8. invalidateQueries resolves only after the active refetches + // finish, so the namespaces run side by side instead of one after another. + const results = await Promise.allSettled( + [...namespaces.values()].map(async (namespace) => + dependencies.queryClient.invalidateQueries({ queryKey: createQueryInvalidationPrefix(namespace), exact: false, refetchType: "active", - }); - } catch { - report("invalidate"); - } + }), + ), + ); + for (const result of results) { + if (result.status === "rejected") report("invalidate"); } } diff --git a/tests/unit/tanstack-cache-coordinator.test.ts b/tests/unit/tanstack-cache-coordinator.test.ts index 4a9603af..07ac65c1 100644 --- a/tests/unit/tanstack-cache-coordinator.test.ts +++ b/tests/unit/tanstack-cache-coordinator.test.ts @@ -212,6 +212,40 @@ describe("TanStack cross-context cache coordinator", () => { expect(invalidate).toHaveBeenCalledTimes(3); }); + it("starts every namespace refetch at once and reports each failure", async () => { + // Q-6.3-8. invalidateQueries resolves only after active refetches finish, + // so awaiting namespaces one by one added their refetch times together. + const client = createClient(); + const pending: Array<() => void> = []; + const invalidate = vi + .spyOn(client, "invalidateQueries") + .mockImplementationOnce( + () => new Promise((resolve) => pending.push(resolve)), + ) + .mockImplementationOnce(() => Promise.reject(new Error("refetch failed"))); + const record = vi.fn(); + const coordinator = createTanStackCacheCoordinator({ + queryClient: client, + invalidationIndex: invalidationIndex(), + topicVersions: topicVersions(), + diagnostics: { record } as never, + }); + + let settled = false; + const done = coordinator.invalidate([TOPIC_A]).then(() => { + settled = true; + }); + await vi.waitFor(() => expect(invalidate).toHaveBeenCalledTimes(2)); + expect(settled).toBe(false); + + pending.forEach((resolve) => resolve()); + await done; + expect(record).toHaveBeenCalledOnce(); + expect(record.mock.calls[0]?.[0]).toMatchObject({ + context: { operation: "invalidate" }, + }); + }); + it("coalesces remote hints while a local mutation lease is held", async () => { const client = createClient(); const harness = crossContextHarness();