MCPcopy Create free account
hub / github.com/TanStack/query / QueryClient

Class QueryClient

packages/query-core/src/queryClient.ts:78–1176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76 * ```
77 */
78export class QueryClient {
79 #queryCache: QueryCache
80 #mutationCache: MutationCache
81 #defaultOptions: DefaultOptions
82 #queryDefaults: Map<string, QueryDefaults>
83 #mutationDefaults: Map<string, MutationDefaults>
84 #mountCount: number
85 #unsubscribeFocus?: () => void
86 #unsubscribeOnline?: () => void
87
88 constructor(config: QueryClientConfig = {}) {
89 this.#queryCache = config.queryCache || new QueryCache()
90 this.#mutationCache = config.mutationCache || new MutationCache()
91 this.#defaultOptions = config.defaultOptions || {}
92 this.#queryDefaults = new Map()
93 this.#mutationDefaults = new Map()
94 this.#mountCount = 0
95 }
96
97 /**
98 * Called by a framework adapter's `QueryClientProvider`-equivalent when it mounts, to start
99 * listening for focus/online events and resume paused mutations. Ref-counted via an internal
100 * mount count, so nested or multiple providers sharing the same `QueryClient` don't tear down
101 * the shared listeners until the last one unmounts.
102 */
103 mount(): void {
104 this.#mountCount++
105 if (this.#mountCount !== 1) return
106
107 this.#unsubscribeFocus = focusManager.subscribe(async (focused) => {
108 if (focused) {
109 await this.resumePausedMutations()
110 this.#queryCache.onFocus()
111 }
112 })
113 this.#unsubscribeOnline = onlineManager.subscribe(async (online) => {
114 if (online) {
115 await this.resumePausedMutations()
116 this.#queryCache.onOnline()
117 }
118 })
119 }
120
121 /**
122 * The inverse of {@link QueryClient#mount} — called by a framework adapter's
123 * `QueryClientProvider`-equivalent when it unmounts. Only tears down the focus/online
124 * listeners once the mount count returns to `0`.
125 */
126 unmount(): void {
127 this.#mountCount--
128 if (this.#mountCount !== 0) return
129
130 this.#unsubscribeFocus?.()
131 this.#unsubscribeFocus = undefined
132
133 this.#unsubscribeOnline?.()
134 this.#unsubscribeOnline = undefined
135 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected