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

Class QueryClient

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

Source from the content-addressed store, hash-verified

57// CLASS
58
59export class QueryClient {
60 #queryCache: QueryCache
61 #mutationCache: MutationCache
62 #defaultOptions: DefaultOptions
63 #queryDefaults: Map<string, QueryDefaults>
64 #mutationDefaults: Map<string, MutationDefaults>
65 #mountCount: number
66 #unsubscribeFocus?: () => void
67 #unsubscribeOnline?: () => void
68
69 constructor(config: QueryClientConfig = {}) {
70 this.#queryCache = config.queryCache || new QueryCache()
71 this.#mutationCache = config.mutationCache || new MutationCache()
72 this.#defaultOptions = config.defaultOptions || {}
73 this.#queryDefaults = new Map()
74 this.#mutationDefaults = new Map()
75 this.#mountCount = 0
76 }
77
78 mount(): void {
79 this.#mountCount++
80 if (this.#mountCount !== 1) return
81
82 this.#unsubscribeFocus = focusManager.subscribe(async (focused) => {
83 if (focused) {
84 await this.resumePausedMutations()
85 this.#queryCache.onFocus()
86 }
87 })
88 this.#unsubscribeOnline = onlineManager.subscribe(async (online) => {
89 if (online) {
90 await this.resumePausedMutations()
91 this.#queryCache.onOnline()
92 }
93 })
94 }
95
96 unmount(): void {
97 this.#mountCount--
98 if (this.#mountCount !== 0) return
99
100 this.#unsubscribeFocus?.()
101 this.#unsubscribeFocus = undefined
102
103 this.#unsubscribeOnline?.()
104 this.#unsubscribeOnline = undefined
105 }
106
107 isFetching<TQueryFilters extends QueryFilters<any> = QueryFilters>(
108 filters?: TQueryFilters,
109 ): number {
110 return this.#queryCache.findAll({ ...filters, fetchStatus: 'fetching' })
111 .length
112 }
113
114 isMutating<
115 TMutationFilters extends MutationFilters<any, any> = MutationFilters,
116 >(filters?: TMutationFilters): number {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected