Open History to see the last commit
1import { hashQueryKeyByOptions, matchQuery, parseFilterArgs } from './utils'
2import { Query } from './query'
3import { notifyManager } from './notifyManager'
4import { Subscribable } from './subscribable'
5import type { QueryFilters } from './utils'
6import type { Action, QueryState } from './query'
7import type { NotifyEvent, OmitKeyof, QueryKey, QueryOptions } from './types'
8import type { QueryClient } from './queryClient'
9import type { QueryObserver } from './queryObserver'
10β
11// TYPES
12β
13interface QueryCacheConfig {
14 onError?: (error: unknown, query: Query<unknown, unknown, unknown>) => void
15 onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void
16 onSettled?: (
17 data: unknown | undefined,
18 error: unknown | null,
19 query: Query<unknown, unknown, unknown>,
20 ) => void
21}
22β
23interface QueryHashMap {
24 [hash: string]: Query<any, any, any, any>
25}
26β
27interface NotifyEventQueryAdded extends NotifyEvent {
28 type: 'added'
29 query: Query<any, any, any, any>
30}
31β
32interface NotifyEventQueryRemoved extends NotifyEvent {
33 type: 'removed'
34 query: Query<any, any, any, any>
35}
36β
37interface NotifyEventQueryUpdated extends NotifyEvent {
38 type: 'updated'
39 query: Query<any, any, any, any>
40 action: Action<any, any>
41}
42β
43interface NotifyEventQueryObserverAdded extends NotifyEvent {
44 type: 'observerAdded'
45 query: Query<any, any, any, any>
46 observer: QueryObserver<any, any, any, any, any>
47}
48β
49interface NotifyEventQueryObserverRemoved extends NotifyEvent {
50 type: 'observerRemoved'
51 query: Query<any, any, any, any>
52 observer: QueryObserver<any, any, any, any, any>
53}
54β
55interface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {
56 type: 'observerResultsUpdated'
57 query: Query<any, any, any, any>
58}
59β
60interface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {
61 type: 'observerOptionsUpdated'
62 query: Query<any, any, any, any>
63 observer: QueryObserver<any, any, any, any, any>
64}
65β
66export type QueryCacheNotifyEvent =
67 | NotifyEventQueryAdded
68 | NotifyEventQueryRemoved
69 | NotifyEventQueryUpdated
70 | NotifyEventQueryObserverAdded
71 | NotifyEventQueryObserverRemoved
72 | NotifyEventQueryObserverResultsUpdated
73 | NotifyEventQueryObserverOptionsUpdated
74β
75type QueryCacheListener = (event: QueryCacheNotifyEvent) => void
76β
77// CLASS
78β
79export class QueryCache extends Subscribable<QueryCacheListener> {
80 config: QueryCacheConfig
81β
82 private queries: Query<any, any, any, any>[]
83 private queriesMap: QueryHashMap
84β
85 constructor(config?: QueryCacheConfig) {
86 super()
87 this.config = config || {}
88 this.queries = []
89 this.queriesMap = {}
90 }
91β
92 build<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(
93 client: QueryClient,
94 options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
95 state?: QueryState<TData, TError>,
96 ): Query<TQueryFnData, TError, TData, TQueryKey> {
97 const queryKey = options.queryKey!
98 const queryHash =
99 options.queryHash ?? hashQueryKeyByOptions(queryKey, options)
100 let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)
101β
102 if (!query) {
103 query = new Query({
104 cache: this,
105 logger: client.getLogger(),
106 queryKey,
107 queryHash,
108 options: client.defaultQueryOptions(options),
109 state,
110 defaultOptions: client.getQueryDefaults(queryKey),
111 })
112 this.add(query)
113 }
114β
115 return query
116 }
117β
118 add(query: Query<any, any, any, any>): void {
119 if (!this.queriesMap[query.queryHash]) {
120 this.queriesMap[query.queryHash] = query
121 this.queries.push(query)
122 this.notify({
123 type: 'added',
124 query,
125 })
126 }
127 }
128β
129 remove(query: Query<any, any, any, any>): void {
130 const queryInMap = this.queriesMap[query.queryHash]
131β
132 if (queryInMap) {
133 query.destroy()
134β
135 this.queries = this.queries.filter((x) => x !== query)
136β
137 if (queryInMap === query) {
138 delete this.queriesMap[query.queryHash]
139 }
140β
141 this.notify({ type: 'removed', query })
142 }
143 }
144β
145 clear(): void {
146 notifyManager.batch(() => {
147 this.queries.forEach((query) => {
148 this.remove(query)
149 })
150 })
151 }
152β
153 get<
154 TQueryFnData = unknown,
155 TError = unknown,
156 TData = TQueryFnData,
157 TQueryKey extends QueryKey = QueryKey,
158 >(
159 queryHash: string,
160 ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {
161 return this.queriesMap[queryHash]
162 }
163β
164 getAll(): Query[] {
165 return this.queries
166 }
167β
168 find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
169 filters: QueryFilters,
170 ): Query<TQueryFnData, TError, TData> | undefined
171 /**
172 * @deprecated This method should be used with only one object argument.
173 */
174 find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
175 queryKey: QueryKey,
176 filters?: OmitKeyof<QueryFilters, 'queryKey'>,
177 ): Query<TQueryFnData, TError, TData> | undefined
178 /**
179 * @deprecated This method should be used with only one object argument.
180 */
181 find<TQueryFnData = unknown, TError = unknown, TData = TQueryFnData>(
182 arg1: QueryKey | QueryFilters,
183 arg2?: OmitKeyof<QueryFilters, 'queryKey'>,
184 ): Query<TQueryFnData, TError, TData> | undefined {
185 const [filters] = parseFilterArgs(arg1, arg2)
186β
187 if (typeof filters.exact === 'undefined') {
188 filters.exact = true
189 }
190β
191 return this.queries.find((query) => matchQuery(filters, query))
192 }
193β
194 findAll(filters?: QueryFilters): Query[]
195 /**
196 * @deprecated This method should be used with only one object argument.
197 */
198 findAll(
199 queryKey?: QueryKey,
200 filters?: OmitKeyof<QueryFilters, 'queryKey'>,
201 ): Query[]
202 /**
203 * @deprecated This method should be used with only one object argument.
204 */
205 findAll(
206 arg1?: QueryKey | QueryFilters,
207 arg2?: OmitKeyof<QueryFilters, 'queryKey'>,
208 ): Query[]
209 /**
210 * @deprecated This method should be used with only one object argument.
211 */
212 findAll(
213 arg1?: QueryKey | QueryFilters,
214 arg2?: OmitKeyof<QueryFilters, 'queryKey'>,
215 ): Query[] {
216 const [filters] = parseFilterArgs(arg1, arg2)
217 return Object.keys(filters).length > 0
218 ? this.queries.filter((query) => matchQuery(filters, query))
219 : this.queries
220 }
221β
222 notify(event: QueryCacheNotifyEvent) {
223 notifyManager.batch(() => {
224 this.listeners.forEach(({ listener }) => {
225 listener(event)
226 })
227 })
228 }
229β
230 onFocus(): void {
231 notifyManager.batch(() => {
232 this.queries.forEach((query) => {
233 query.onFocus()
234 })
235 })
236 }
237β
238 onOnline(): void {
239 notifyManager.batch(() => {
240 this.queries.forEach((query) => {
241 query.onOnline()
242 })
243 })
244 }
245}