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

Class QueryClient

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected