Open History to see the last commit
1import { createContext } from '@lit/context'
2import type { QueryClient } from '@tanstack/query-core'
3β
4/**
5 * Lit context key used by `QueryClientProvider` and host-bound APIs to share a
6 * `QueryClient` through the DOM tree.
7 *
8 * Most applications use `QueryClientProvider` instead of interacting with this
9 * context directly.
10 */
11export const queryClientContext = createContext<QueryClient>(
12 Symbol.for('tanstack-query-client'),
13)
14β
15const missingQueryClientMessage =
16 'No QueryClient available. Pass one explicitly or render within QueryClientProvider.'
17const ambiguousQueryClientMessage =
18 'Multiple QueryClients are mounted. Pass one explicitly instead of relying on global QueryClient helpers.'
19β
20const registeredClients = new Map<QueryClient, number>()
21let defaultClient: QueryClient | undefined
22β
23/**
24 * Registers a `QueryClient` as a process-local fallback for APIs that resolve a
25 * client without an explicit argument.
26 *
27 * `QueryClientProvider` calls this automatically while it is connected. Prefer
28 * passing an explicit client or rendering under a provider when possible.
29 *
30 * @param client - The query client to register as the current default.
31 */
32export function registerDefaultQueryClient(client: QueryClient): void {
33 registeredClients.set(client, (registeredClients.get(client) ?? 0) + 1)
34 defaultClient = client
35}
36β
37/**
38 * Unregisters a client previously registered with `registerDefaultQueryClient`.
39 *
40 * `QueryClientProvider` calls this automatically when it disconnects.
41 *
42 * @param client - The query client registration to release.
43 */
44export function unregisterDefaultQueryClient(client: QueryClient): void {
45 const count = registeredClients.get(client)
46 if (count === undefined) {
47 return
48 }
49β
50 if (count > 1) {
51 registeredClients.set(client, count - 1)
52 return
53 }
54β
55 registeredClients.delete(client)
56 if (defaultClient !== client) {
57 return
58 }
59β
60 const remaining = [...registeredClients.keys()]
61 defaultClient = remaining.at(-1)
62}
63β
64/**
65 * Returns the registered default `QueryClient`, if exactly one default client
66 * is available.
67 *
68 * @returns The default query client, or `undefined` when there is no registered
69 * client or more than one registered client.
70 */
71export function getDefaultQueryClient(): QueryClient | undefined {
72 if (registeredClients.size > 1) {
73 return undefined
74 }
75β
76 return defaultClient
77}
78β
79/**
80 * Creates the error thrown when no `QueryClient` is available.
81 *
82 * @returns A new `Error` explaining that no `QueryClient` is available.
83 */
84export function createMissingQueryClientError(): Error {
85 return new Error(missingQueryClientMessage)
86}
87β
88/**
89 * Creates the error thrown when multiple `QueryClient`s are mounted and the
90 * default one would be ambiguous.
91 *
92 * @returns A new `Error` explaining that the default `QueryClient` is
93 * ambiguous.
94 */
95function createAmbiguousQueryClientError(): Error {
96 return new Error(ambiguousQueryClientMessage)
97}
98β
99/**
100 * Resolves the current default `QueryClient` registered by a connected
101 * `QueryClientProvider`.
102 *
103 * This helper is useful outside a Lit reactive controller when a single
104 * provider is mounted. It throws if no client is registered or if multiple
105 * clients are mounted and the default would be ambiguous.
106 *
107 * @returns The single registered query client.
108 * @throws {Error} If no `QueryClient` is registered, or if multiple are mounted
109 * and the default is ambiguous.
110 */
111export function useQueryClient(): QueryClient {
112 const client = getDefaultQueryClient()
113 if (client) {
114 return client
115 }
116β
117 if (registeredClients.size > 1) {
118 throw createAmbiguousQueryClientError()
119 }
120β
121 throw createMissingQueryClientError()
122}
123β
124/**
125 * Resolves an explicit `QueryClient` or falls back to `useQueryClient`.
126 *
127 * @param explicit - Optional client supplied by the caller.
128 * @returns The explicit client when provided, otherwise the current default
129 * client.
130 */
131export function resolveQueryClient(explicit?: QueryClient): QueryClient {
132 return explicit ?? useQueryClient()
133}