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

Class QueryClient

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected