-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
queryCache.js
599 lines (498 loc) · 14.8 KB
/
queryCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import React from 'react'
import {
isServer,
functionalUpdate,
cancelledError,
isDocumentVisible,
statusLoading,
statusSuccess,
statusError,
getQueryArgs,
deepIncludes,
noop,
} from './utils'
import { defaultConfigRef } from './config'
export const queryCache = makeQueryCache()
export const queryCacheContext = React.createContext(queryCache)
export const queryCaches = [queryCache]
export function useQueryCache() {
return React.useContext(queryCacheContext)
}
export function ReactQueryCacheProvider({ queryCache, children }) {
const cache = React.useMemo(() => queryCache || makeQueryCache(), [
queryCache,
])
React.useEffect(() => {
queryCaches.push(cache)
return () => {
// remove the cache from the active list
const i = queryCaches.indexOf(cache)
if (i >= 0) {
queryCaches.splice(i, 1)
}
// if the cache was created by us, we need to tear it down
if (queryCache == null) {
cache.clear()
}
}
}, [cache, queryCache])
return (
<queryCacheContext.Provider value={cache}>
{children}
</queryCacheContext.Provider>
)
}
const actionInit = {}
const actionFailed = {}
const actionMarkStale = {}
const actionMarkGC = {}
const actionFetch = {}
const actionSuccess = {}
const actionError = {}
const actionSetState = {}
export function makeQueryCache() {
const listeners = []
const cache = {
queries: {},
isFetching: 0,
}
const notifyGlobalListeners = () => {
cache.isFetching = Object.values(cache.queries).reduce(
(acc, query) => (query.state.isFetching ? acc + 1 : acc),
0
)
listeners.forEach(d => d(cache))
}
cache.subscribe = cb => {
listeners.push(cb)
return () => {
listeners.splice(listeners.indexOf(cb), 1)
}
}
cache.clear = () => {
Object.values(cache.queries).forEach(query => query.clear())
cache.queries = {}
notifyGlobalListeners()
}
const findQueries = (predicate, { exact } = {}) => {
if (typeof predicate !== 'function') {
const [
queryHash,
queryKey,
] = defaultConfigRef.current.queryKeySerializerFn(predicate)
predicate = d =>
exact ? d.queryHash === queryHash : deepIncludes(d.queryKey, queryKey)
}
return Object.values(cache.queries).filter(predicate)
}
cache.getQueries = findQueries
cache.getQuery = queryKey => findQueries(queryKey, { exact: true })[0]
cache.getQueryData = queryKey => cache.getQuery(queryKey)?.state.data
cache.removeQueries = (predicate, { exact } = {}) => {
const foundQueries = findQueries(predicate, { exact })
foundQueries.forEach(query => {
clearTimeout(query.staleTimeout)
delete cache.queries[query.queryHash]
})
if (foundQueries.length) {
notifyGlobalListeners()
}
}
cache.cancelQueries = (predicate, { exact } = {}) => {
const foundQueries = findQueries(predicate, { exact })
foundQueries.forEach(query => {
query.cancel()
})
if (foundQueries.length) {
notifyGlobalListeners()
}
}
cache.refetchQueries = async (
predicate,
{ exact, throwOnError, force } = {}
) => {
const foundQueries =
predicate === true
? Object.values(cache.queries)
: findQueries(predicate, { exact })
try {
return await Promise.all(
foundQueries.map(query => query.fetch({ force }))
)
} catch (err) {
if (throwOnError) {
throw err
}
}
}
cache._buildQuery = (userQueryKey, queryVariables, queryFn, config) => {
let [queryHash, queryKey] = config.queryKeySerializerFn(userQueryKey)
let query = cache.queries[queryHash]
if (query) {
Object.assign(query, { queryVariables, queryFn })
query.config = { ...query.config, ...config }
} else {
query = makeQuery({
cache,
queryKey,
queryHash,
queryVariables,
queryFn,
config,
})
// If the query started with data, schedule
// a stale timeout
if (!isServer && query.state.data) {
query.scheduleStaleTimeout()
// Simulate a query healing process
query.heal()
// Schedule for garbage collection in case
// nothing subscribes to this query
query.scheduleGarbageCollection()
}
if (query.queryHash) {
if (!isServer) {
cache.queries[queryHash] = query
// Here, we setTimeout so as to not trigger
// any setState's in parent components in the
// middle of the render phase.
setTimeout(() => {
notifyGlobalListeners()
})
}
}
}
return query
}
cache.prefetchQuery = async (...args) => {
let [
queryKey,
queryVariables,
queryFn,
{ force, ...config },
] = getQueryArgs(args)
config = {
...defaultConfigRef.current,
...config,
}
const query = cache._buildQuery(queryKey, queryVariables, queryFn, config)
// Don't prefetch queries that are fresh, unless force is passed
if (query.state.isStale || force) {
// Trigger a fetch and return the promise
try {
const res = await query.fetch({ force })
query.wasPrefetched = true
return res
} catch (err) {
if (config.throwOnError) {
throw err
}
}
}
return query.state.data
}
cache.setQueryData = (queryKey, updater, { exact, ...config } = {}) => {
let queries = findQueries(queryKey, { exact })
if (!queries.length && typeof queryKey !== 'function') {
queries = [
cache._buildQuery(queryKey, undefined, () => new Promise(noop), {
...defaultConfigRef.current,
...config,
}),
]
}
queries.forEach(d => d.setData(updater))
}
function makeQuery(options) {
const queryCache = options.cache
const reducer = options.config.queryReducer || defaultQueryReducer
const noQueryHash = typeof options.queryHash === 'undefined'
const initialData =
typeof options.config.initialData === 'function'
? options.config.initialData()
: options.config.initialData
const hasInitialData = typeof initialData !== 'undefined'
const isStale = noQueryHash ? true : !hasInitialData
const manual = options.config.manual
const initialStatus =
noQueryHash || manual || hasInitialData ? statusSuccess : statusLoading
const query = {
...options,
instances: [],
state: reducer(undefined, {
type: actionInit,
initialStatus,
initialData,
hasInitialData,
isStale,
manual,
}),
}
const dispatch = action => {
query.state = reducer(query.state, action)
query.instances.forEach(d => d.onStateUpdate(query.state))
notifyGlobalListeners()
}
query.scheduleStaleTimeout = () => {
if (query.config.staleTime === Infinity) {
return
}
query.staleTimeout = setTimeout(() => {
if (queryCache.getQuery(query.queryKey)) {
dispatch({ type: actionMarkStale })
}
}, query.config.staleTime)
}
query.scheduleGarbageCollection = () => {
if (query.config.cacheTime === Infinity) {
return
}
dispatch({ type: actionMarkGC })
query.cacheTimeout = setTimeout(
() => {
cache.removeQueries(
d =>
d.state.markedForGarbageCollection &&
d.queryHash === query.queryHash
)
},
typeof query.state.data === 'undefined' &&
query.state.status !== 'error'
? 0
: query.config.cacheTime
)
}
query.heal = () => {
// Stop the query from being garbage collected
clearTimeout(query.cacheTimeout)
// Mark the query as not cancelled
query.cancelled = null
}
query.cancel = () => {
query.cancelled = cancelledError
if (query.cancelPromises) {
query.cancelPromises()
}
delete query.promise
notifyGlobalListeners()
}
query.updateInstance = instance => {
let found = query.instances.find(d => d.id === instance.id)
if (found) {
Object.assign(found, instance)
} else {
found = {
onStateUpdate: noop,
...instance,
}
query.instances.push(instance)
}
}
query.subscribe = instanceId => {
query.heal()
// Return the unsubscribe function
return () => {
query.instances = query.instances.filter(d => d.id !== instanceId)
if (!query.instances.length) {
query.cancel()
// Schedule garbage collection
query.scheduleGarbageCollection()
}
}
}
// Set up the fetch function
const tryFetchData = async (queryFn, ...args) => {
try {
// Perform the query
const promise = queryFn(...query.config.queryFnParamsFilter(args))
query.cancelPromises = () => promise.cancel?.()
const data = await promise
delete query.cancelPromises
if (query.cancelled) throw query.cancelled
return data
} catch (error) {
delete query.cancelPromises
if (query.cancelled) throw query.cancelled
// If we fail, increase the failureCount
dispatch({ type: actionFailed })
// Do we need to retry the request?
if (
query.config.retry === true ||
query.state.failureCount <= query.config.retry ||
(typeof query.config.retry === 'function' &&
query.config.retry(query.state.failureCount, error))
) {
// Only retry if the document is visible
if (!isDocumentVisible()) {
// set this flag to continue fetch retries on focus
query.shouldContinueRetryOnFocus = true
return new Promise(noop)
}
delete query.shouldContinueRetryOnFocus
// Determine the retryDelay
const delay = functionalUpdate(
query.config.retryDelay,
query.state.failureCount
)
// Return a new promise with the retry
return await new Promise((resolve, reject) => {
// Keep track of the retry timeout
setTimeout(async () => {
if (query.cancelled) return reject(query.cancelled)
try {
const data = await tryFetchData(queryFn, ...args)
if (query.cancelled) return reject(query.cancelled)
resolve(data)
} catch (error) {
if (query.cancelled) return reject(query.cancelled)
reject(error)
}
}, delay)
})
}
throw error
}
}
query.fetch = async ({ force, __queryFn = query.queryFn } = {}) => {
// Don't refetch fresh queries that don't have a queryHash
if (!query.queryHash || (!query.state.isStale && !force)) {
return
}
// Create a new promise for the query cache if necessary
if (!query.promise) {
query.promise = (async () => {
// If there are any retries pending for this query, kill them
query.cancelled = null
const callbackInstances = [...query.instances]
if (query.wasSuspended) {
callbackInstances.unshift(query.suspenseInstance)
}
try {
// Set up the query refreshing state
dispatch({ type: actionFetch })
// Try to fetch
let data = await tryFetchData(
__queryFn,
...query.queryKey,
...query.queryVariables
)
query.setData(old =>
query.config.isDataEqual(old, data) ? old : data
)
callbackInstances.forEach(
instance =>
instance.onSuccess && instance.onSuccess(query.state.data)
)
callbackInstances.forEach(
instance =>
instance.onSettled && instance.onSettled(query.state.data, null)
)
delete query.promise
return data
} catch (error) {
dispatch({
type: actionError,
cancelled: error === query.cancelled,
error,
})
delete query.promise
if (error !== query.cancelled) {
callbackInstances.forEach(
instance => instance.onError && instance.onError(error)
)
callbackInstances.forEach(
instance =>
instance.onSettled && instance.onSettled(undefined, error)
)
throw error
}
}
})()
}
return query.promise
}
query.setState = updater => dispatch({ type: actionSetState, updater })
query.setData = updater => {
// Set data and mark it as cached
dispatch({ type: actionSuccess, updater })
// Schedule a fresh invalidation!
clearTimeout(query.staleTimeout)
query.scheduleStaleTimeout()
}
query.clear = () => {
clearTimeout(query.staleTimeout)
clearTimeout(query.cacheTimeout)
query.cancel()
}
return query
}
return cache
}
export function defaultQueryReducer(state, action) {
switch (action.type) {
case actionInit:
return {
status: action.initialStatus,
error: null,
isFetching:
action.hasInitialData || action.manual
? false
: action.initialStatus === 'loading',
canFetchMore: false,
failureCount: 0,
isStale: action.isStale,
markedForGarbageCollection: false,
data: action.initialData,
updatedAt: action.hasInitialData ? Date.now() : 0,
}
case actionFailed:
return {
...state,
failureCount: state.failureCount + 1,
}
case actionMarkStale:
return {
...state,
isStale: true,
}
case actionMarkGC: {
return {
...state,
markedForGarbageCollection: true,
}
}
case actionFetch:
return {
...state,
status: state.status === statusError ? statusLoading : state.status,
isFetching: true,
failureCount: 0,
}
case actionSuccess:
return {
...state,
status: statusSuccess,
data: functionalUpdate(action.updater, state.data),
error: null,
isStale: false,
isFetching: false,
canFetchMore: action.canFetchMore,
updatedAt: Date.now(),
failureCount: 0,
}
case actionError:
return {
...state,
isFetching: false,
isStale: true,
...(!action.cancelled && {
status: statusError,
error: action.error,
}),
}
case actionSetState:
return functionalUpdate(action.updater, state)
default:
throw new Error()
}
}