browser-query-client.ts998 BView on GitHub
import type { QueryClient } from '@tanstack/react-query';

/**
 * The browser's single QueryClient, reachable from non-React code.
 *
 * The Cedar store's SSE response processors run outside the React tree but produce changes the
 * server has already persisted — so they need a way to tell React Query its cache is behind.
 * Without this they can only patch the store, and the next mount rehydrates it from a stale cached
 * response, silently undoing what the agent just did.
 *
 * Deliberately dependency-free (the QueryClient import is type-only): the store imports this, and
 * the provider imports the store's world, so anything heavier here would close an import cycle.
 * `QueryProvider` registers the client it builds; on the server nothing registers and this is null.
 */
let client: QueryClient | null = null;

export function setBrowserQueryClient(next: QueryClient | null): void {
  client = next;
}

export function getBrowserQueryClient(): QueryClient | null {
  return client;
}