forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-swr.ts
511 lines (441 loc) · 13.7 KB
/
use-swr.ts
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
import {
useState,
useEffect,
useLayoutEffect,
useRef,
useContext,
useCallback
} from 'react'
import { unstable_batchedUpdates } from './libs/reactBatchedUpdates'
import throttle from 'lodash/throttle'
import deepEqual from 'fast-deep-equal'
import {
keyInterface,
ConfigInterface,
RevalidateOptionInterface,
updaterInterface,
triggerInterface,
mutateInterface,
broadcastStateInterface,
responseInterface,
fetcherFn
} from './types'
import defaultConfig, {
CONCURRENT_PROMISES,
CONCURRENT_PROMISES_TS,
FOCUS_REVALIDATORS,
CACHE_REVALIDATORS,
MUTATION_TS,
cacheGet,
cacheSet
} from './config'
import SWRConfigContext from './swr-config-context'
import isDocumentVisible from './libs/is-document-visible'
import useHydration from './libs/use-hydration'
import hash from './libs/hash'
const IS_SERVER = typeof window === 'undefined'
// TODO: introduce namepsace for the cache
const getErrorKey = key => (key ? 'err@' + key : '')
const getKeyArgs = _key => {
let key: string
let args = null
if (typeof _key === 'function') {
try {
key = _key()
} catch (err) {
// dependencies not ready
key = ''
}
} else if (Array.isArray(_key)) {
// args array
key = hash(_key)
args = _key
} else {
// convert null to ''
key = String(_key || '')
}
return [key, args]
}
const trigger: triggerInterface = (_key, shouldRevalidate = true) => {
const [key] = getKeyArgs(_key)
if (!key) return
const updaters = CACHE_REVALIDATORS[key]
if (key && updaters) {
const currentData = cacheGet(key)
const currentError = cacheGet(getErrorKey(key))
for (let i = 0; i < updaters.length; ++i) {
updaters[i](shouldRevalidate, currentData, currentError)
}
}
}
const broadcastState: broadcastStateInterface = (key, data, error) => {
const updaters = CACHE_REVALIDATORS[key]
if (key && updaters) {
for (let i = 0; i < updaters.length; ++i) {
updaters[i](false, data, error)
}
}
}
const mutate: mutateInterface = (_key, data, shouldRevalidate = true) => {
const [key] = getKeyArgs(_key)
if (!key) return
// update timestamp
MUTATION_TS[key] = Date.now() - 1
// update cached data
cacheSet(key, data)
// update existing SWR Hooks' state
const updaters = CACHE_REVALIDATORS[key]
if (updaters) {
for (let i = 0; i < updaters.length; ++i) {
updaters[i](shouldRevalidate, data)
}
}
}
function useSWR<Data = any, Error = any>(
key: keyInterface
): responseInterface<Data, Error>
function useSWR<Data = any, Error = any>(
key: keyInterface,
config?: ConfigInterface<Data, Error>
): responseInterface<Data, Error>
function useSWR<Data = any, Error = any>(
key: keyInterface,
fn?: fetcherFn<Data>,
config?: ConfigInterface<Data, Error>
): responseInterface<Data, Error>
function useSWR<Data = any, Error = any>(
...args
): responseInterface<Data, Error> {
let _key: keyInterface,
fn: fetcherFn<Data> | undefined,
config: ConfigInterface<Data, Error> = {}
if (args.length >= 1) {
_key = args[0]
}
if (typeof args[1] === 'function') {
fn = args[1]
} else if (typeof args[1] === 'object') {
config = args[1]
}
if (typeof args[2] === 'object') {
config = args[2]
}
// we assume `key` as the identifier of the request
// `key` can change but `fn` shouldn't
// (because `revalidate` only depends on `key`)
const [key, fnArgs] = getKeyArgs(_key)
// `keyErr` is the cache key for error objects
const keyErr = getErrorKey(key)
config = Object.assign(
{},
defaultConfig,
useContext(SWRConfigContext),
config
)
if (typeof fn === 'undefined') {
// use a global fetcher
fn = config.fetcher
}
// it is fine to call `useHydration` conditionally here
// because `config.suspense` should never change
const shouldReadCache = config.suspense || !useHydration()
// stale: get from cache
let [data, setData] = useState(config.initialData || (shouldReadCache ? cacheGet(key) : undefined))
let [error, setError] = useState(
shouldReadCache ? cacheGet(keyErr) : undefined
)
// if there's any ongoing request
let [isValidating, setIsValidating] = useState(false)
// error ref inside revalidate (is last request errored?)
const unmountedRef = useRef(false)
const keyRef = useRef(key)
const errorRef = useRef(error)
const dataRef = useRef(data)
// start a revalidation
const revalidate = useCallback(
async (
revalidateOpts: RevalidateOptionInterface = {}
): Promise<boolean> => {
if (!key) return false
if (unmountedRef.current) return false
revalidateOpts = Object.assign({ dedupe: false }, revalidateOpts)
let loading = true
let shouldDeduping =
typeof CONCURRENT_PROMISES[key] !== 'undefined' && revalidateOpts.dedupe
// start fetching
try {
setIsValidating(true)
let newData
let startAt
if (shouldDeduping) {
// there's already an ongoing request,
// this one needs to be deduplicated.
startAt = CONCURRENT_PROMISES_TS[key]
newData = await CONCURRENT_PROMISES[key]
} else {
// if no cache being rendered currently (it shows a blank page),
// we trigger the loading slow event.
if (config.loadingTimeout && !cacheGet(key)) {
setTimeout(() => {
if (loading) config.onLoadingSlow(key, config)
}, config.loadingTimeout)
}
if (fnArgs !== null) {
CONCURRENT_PROMISES[key] = fn(...fnArgs)
} else {
CONCURRENT_PROMISES[key] = fn(key)
}
CONCURRENT_PROMISES_TS[key] = startAt = Date.now()
setTimeout(() => {
delete CONCURRENT_PROMISES[key]
delete CONCURRENT_PROMISES_TS[key]
}, config.dedupingInterval)
newData = await CONCURRENT_PROMISES[key]
// trigger the success event,
// only do this for the original request.
config.onSuccess(newData, key, config)
}
// if the revalidation happened earlier than the local mutation,
// we have to ignore the result because it could override.
// meanwhile, a new revalidation should be triggered by the mutation.
if (MUTATION_TS[key] && startAt <= MUTATION_TS[key]) {
setIsValidating(false)
return false
}
cacheSet(key, newData)
cacheSet(keyErr, undefined)
keyRef.current = key
// batch state updates
unstable_batchedUpdates(() => {
setIsValidating(false)
if (typeof errorRef.current !== 'undefined') {
// we don't have an error
setError(undefined)
errorRef.current = undefined
}
if (deepEqual(dataRef.current, newData)) {
// deep compare to avoid extra re-render
// do nothing
} else {
// data changed
setData(newData)
dataRef.current = newData
}
if (!shouldDeduping) {
// also update other hooks
broadcastState(key, newData, undefined)
}
})
} catch (err) {
delete CONCURRENT_PROMISES[key]
delete CONCURRENT_PROMISES_TS[key]
cacheSet(keyErr, err)
keyRef.current = key
// get a new error
// don't use deep equal for errors
if (errorRef.current !== err) {
errorRef.current = err
unstable_batchedUpdates(() => {
// we keep the stale data
setIsValidating(false)
setError(err)
})
if (!shouldDeduping) {
// also broadcast to update other hooks
broadcastState(key, undefined, err)
}
}
// events and retry
config.onError(err, key, config)
if (config.shouldRetryOnError) {
// when retrying, we always enable deduping
const retryCount = (revalidateOpts.retryCount || 0) + 1
config.onErrorRetry(
err,
key,
config,
revalidate,
Object.assign({ dedupe: true }, revalidateOpts, { retryCount })
)
}
}
loading = false
return true
},
[key]
)
// React currently throws a warning when using useLayoutEffect on the server.
// To get around it, we can conditionally useEffect on the server (no-op) and
// useLayoutEffect in the browser.
const useIsomorphicLayoutEffect = IS_SERVER ? useEffect : useLayoutEffect
// mounted (client side rendering)
useIsomorphicLayoutEffect(() => {
if (!key) return undefined
// after `key` updates, we need to mark it as mounted
unmountedRef.current = false
// after the component is mounted (hydrated),
// we need to update the data from the cache
// and trigger a revalidation
const currentHookData = dataRef.current
const latestKeyedData = cacheGet(key)
// update the state if the key changed or cache updated
if (
keyRef.current !== key ||
!deepEqual(currentHookData, latestKeyedData)
) {
setData(latestKeyedData)
dataRef.current = latestKeyedData
keyRef.current = key
}
// revalidate with deduping
const softRevalidate = () => revalidate({ dedupe: true })
// trigger a revalidation
if (
typeof latestKeyedData !== 'undefined' &&
window['requestIdleCallback']
) {
// delay revalidate if there's cache
// to not block the rendering
window['requestIdleCallback'](softRevalidate)
} else {
softRevalidate()
}
// whenever the window gets focused, revalidate
let onFocus
if (config.revalidateOnFocus) {
// throttle: avoid being called twice from both listeners
// and tabs being switched quickly
onFocus = throttle(softRevalidate, config.focusThrottleInterval)
if (!FOCUS_REVALIDATORS[key]) {
FOCUS_REVALIDATORS[key] = [onFocus]
} else {
FOCUS_REVALIDATORS[key].push(onFocus)
}
}
// register global cache update listener
const onUpdate: updaterInterface<Data, Error> = (
shouldRevalidate = true,
updatedData,
updatedError
) => {
// update hook state
unstable_batchedUpdates(() => {
if (
typeof updatedData !== 'undefined' &&
!deepEqual(dataRef.current, updatedData)
) {
setData(updatedData)
dataRef.current = updatedData
}
// always update error
// because it can be `undefined`
if (errorRef.current !== updatedError) {
setError(updatedError)
errorRef.current = updatedError
}
keyRef.current = key
})
if (shouldRevalidate) {
return softRevalidate()
}
return false
}
// add updater to listeners
if (!CACHE_REVALIDATORS[key]) {
CACHE_REVALIDATORS[key] = [onUpdate]
} else {
CACHE_REVALIDATORS[key].push(onUpdate)
}
// set up polling
let timeout = null
if (config.refreshInterval) {
const tick = async () => {
if (
!errorRef.current &&
(config.refreshWhenHidden || isDocumentVisible())
) {
// only revalidate when the page is visible
// if API request errored, we stop polling in this round
// and let the error retry function handle it
await softRevalidate()
}
const interval = config.refreshInterval
timeout = setTimeout(tick, interval)
}
timeout = setTimeout(tick, config.refreshInterval)
}
return () => {
// cleanup
setData = () => null
setError = () => null
setIsValidating = () => null
// mark it as unmounted
unmountedRef.current = true
if (onFocus && FOCUS_REVALIDATORS[key]) {
const index = FOCUS_REVALIDATORS[key].indexOf(onFocus)
if (index >= 0) FOCUS_REVALIDATORS[key].splice(index, 1)
}
if (CACHE_REVALIDATORS[key]) {
const index = CACHE_REVALIDATORS[key].indexOf(onUpdate)
if (index >= 0) CACHE_REVALIDATORS[key].splice(index, 1)
}
if (timeout !== null) {
clearTimeout(timeout)
}
}
}, [key, config.refreshInterval, revalidate])
// suspense
if (config.suspense) {
if (IS_SERVER)
throw new Error('Suspense on server side is not yet supported!')
// in suspense mode, we can't return empty state
// (it should be suspended)
// try to get data and error from cache
let latestData = cacheGet(key)
let latestError = cacheGet(keyErr)
if (
typeof latestData === 'undefined' &&
typeof latestError === 'undefined'
) {
// need to start the request if it hasn't
if (!CONCURRENT_PROMISES[key]) {
// trigger revalidate immediately
// to get the promise
revalidate()
}
if (
CONCURRENT_PROMISES[key] &&
typeof CONCURRENT_PROMISES[key].then === 'function'
) {
// if it is a promise
throw CONCURRENT_PROMISES[key]
}
// it's a value, return it directly (override)
latestData = CONCURRENT_PROMISES[key]
}
if (typeof latestData === 'undefined' && latestError) {
// in suspense mode, throw error if there's no content
throw latestError
}
// return the latest data / error from cache
// in case `key` has changed
return {
error: latestError,
data: latestData,
revalidate,
isValidating
}
}
return {
error,
// `key` might be changed in the upcoming hook re-render,
// but the previous state will stay
// so we need to match the latest key and data
data: keyRef.current === key ? data : undefined,
revalidate, // handler
isValidating
}
}
const SWRConfig = SWRConfigContext.Provider
export { trigger, mutate, SWRConfig }
export default useSWR