-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
use-swr.ts
549 lines (486 loc) · 17.6 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
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
import { useCallback, useRef, useDebugValue } from 'react'
import { defaultConfig } from './utils/config'
import { SWRGlobalState, GlobalState } from './utils/global-state'
import { IS_SERVER, rAF, useIsomorphicLayoutEffect } from './utils/env'
import { serialize } from './utils/serialize'
import {
isUndefined,
UNDEFINED,
OBJECT,
mergeObjects,
isFunction
} from './utils/helper'
import ConfigProvider from './utils/config-context'
import { useStateWithDeps } from './utils/state'
import { withArgs } from './utils/resolve-args'
import { subscribeCallback } from './utils/subscribe-key'
import { broadcastState } from './utils/broadcast-state'
import { getTimestamp } from './utils/timestamp'
import { internalMutate } from './utils/mutate'
import * as revalidateEvents from './constants/revalidate-events'
import {
State,
Fetcher,
Key,
SWRResponse,
RevalidatorOptions,
FullConfiguration,
SWRConfiguration,
SWRHook,
StateUpdateCallback,
RevalidateEvent
} from './types'
const WITH_DEDUPE = { dedupe: true }
export const useSWRHandler = <Data = any, Error = any>(
_key: Key,
fetcher: Fetcher<Data> | null,
config: typeof defaultConfig & SWRConfiguration<Data, Error>
) => {
const {
cache,
compare,
fallbackData,
suspense,
revalidateOnMount,
refreshInterval,
refreshWhenHidden,
refreshWhenOffline
} = config
const [EVENT_REVALIDATORS, STATE_UPDATERS, MUTATION, FETCH] =
SWRGlobalState.get(cache) as GlobalState
// `key` is the identifier of the SWR `data` state, `keyInfo` holds extra
// states such as `error` and `isValidating` inside,
// all of them are derived from `_key`.
// `fnArgs` is an array of arguments parsed from the key, which will be passed
// to the fetcher.
const [key, fnArgs, keyInfo] = serialize(_key)
// If it's the initial render of this hook.
const initialMountedRef = useRef(false)
// If the hook is unmounted already. This will be used to prevent some effects
// to be called after unmounting.
const unmountedRef = useRef(false)
// Refs to keep the key and config.
const keyRef = useRef(key)
const fetcherRef = useRef(fetcher)
const configRef = useRef(config)
const getConfig = () => configRef.current
const isActive = () => getConfig().isVisible() && getConfig().isOnline()
const patchFetchInfo = (info: { isValidating?: boolean; error?: any }) =>
cache.set(keyInfo, mergeObjects(cache.get(keyInfo), info))
// Get the current state that SWR should return.
const cached = cache.get(key)
const fallback = isUndefined(fallbackData)
? config.fallback[key]
: fallbackData
const data = isUndefined(cached) ? fallback : cached
const info = cache.get(keyInfo) || {}
const error = info.error
// - Suspense mode and there's stale data for the initial render.
// - Not suspense mode and there is no fallback data and `revalidateIfStale` is enabled.
// - `revalidateIfStale` is enabled but `data` is not defined.
const shouldRevalidateOnMount = () => {
// If `revalidateOnMount` is set, we take the value directly.
if (!isUndefined(revalidateOnMount)) return revalidateOnMount
// If it's paused, we skip revalidation.
if (getConfig().isPaused()) return false
return suspense
? // Under suspense mode, it will always fetch on render if there is no
// stale data so no need to revalidate immediately on mount again.
!isUndefined(data)
: // If there is no stale data, we need to revalidate on mount;
// If `revalidateIfStale` is set to true, we will always revalidate.
isUndefined(data) || config.revalidateIfStale
}
// Resolve the current validating state.
const resolveValidating = () => {
if (!key || !fetcher) return false
if (info.isValidating) return true
// If it's not mounted yet and it should revalidate on mount, revalidate.
return !initialMountedRef.current && shouldRevalidateOnMount()
}
const isValidating = resolveValidating()
const [stateRef, stateDependencies, setState] = useStateWithDeps<Data, Error>(
{
data,
error,
isValidating
},
unmountedRef
)
// The revalidation function is a carefully crafted wrapper of the original
// `fetcher`, to correctly handle the many edge cases.
const revalidate = useCallback(
async (revalidateOpts?: RevalidatorOptions): Promise<boolean> => {
const currentFetcher = fetcherRef.current
if (
!key ||
!currentFetcher ||
unmountedRef.current ||
getConfig().isPaused()
) {
return false
}
let newData: Data
let startAt: number
let loading = true
const opts = revalidateOpts || {}
// If there is no ongoing concurrent request, or `dedupe` is not set, a
// new request should be initiated.
const shouldStartNewRequest = !FETCH[key] || !opts.dedupe
// Do unmount check for calls:
// If key has changed during the revalidation, or the component has been
// unmounted, old dispatch and old event callbacks should not take any
// effect.
const isCurrentKeyMounted = () =>
!unmountedRef.current &&
key === keyRef.current &&
initialMountedRef.current
const cleanupState = () => {
// Check if it's still the same request before deleting.
const requestInfo = FETCH[key]
if (requestInfo && requestInfo[1] === startAt) {
delete FETCH[key]
}
}
// The new state object when request finishes.
const newState: State<Data, Error> = { isValidating: false }
const finishRequestAndUpdateState = () => {
patchFetchInfo({ isValidating: false })
// We can only set state if it's safe (still mounted with the same key).
if (isCurrentKeyMounted()) {
setState(newState)
}
}
// Start fetching. Change the `isValidating` state, update the cache.
patchFetchInfo({
isValidating: true
})
setState({ isValidating: true })
try {
if (shouldStartNewRequest) {
// Tell all other hooks to change the `isValidating` state.
broadcastState(
cache,
key,
stateRef.current.data,
stateRef.current.error,
true
)
// If no cache being rendered currently (it shows a blank page),
// we trigger the loading slow event.
if (config.loadingTimeout && !cache.get(key)) {
setTimeout(() => {
if (loading && isCurrentKeyMounted()) {
getConfig().onLoadingSlow(key, config)
}
}, config.loadingTimeout)
}
// Start the request and save the timestamp.
FETCH[key] = [currentFetcher(...fnArgs), getTimestamp()]
}
// Wait until the ongoing request is done. Deduplication is also
// considered here.
;[newData, startAt] = FETCH[key]
newData = await newData
if (shouldStartNewRequest) {
// If the request isn't interrupted, clean it up after the
// deduplication interval.
setTimeout(cleanupState, config.dedupingInterval)
}
// If there're other ongoing request(s), started after the current one,
// we need to ignore the current one to avoid possible race conditions:
// req1------------------>res1 (current one)
// req2---------------->res2
// the request that fired later will always be kept.
// The timestamp maybe be `undefined` or a number
if (!FETCH[key] || FETCH[key][1] !== startAt) {
if (shouldStartNewRequest) {
if (isCurrentKeyMounted()) {
getConfig().onDiscarded(key)
}
}
return false
}
// Clear error.
patchFetchInfo({
error: UNDEFINED
})
newState.error = UNDEFINED
// If there're other mutations(s), overlapped with the current revalidation:
// case 1:
// req------------------>res
// mutate------>end
// case 2:
// req------------>res
// mutate------>end
// case 3:
// req------------------>res
// mutate-------...---------->
// we have to ignore the revalidation result (res) because it's no longer fresh.
// meanwhile, a new revalidation should be triggered when the mutation ends.
const mutationInfo = MUTATION[key]
if (
!isUndefined(mutationInfo) &&
// case 1
(startAt <= mutationInfo[0] ||
// case 2
startAt <= mutationInfo[1] ||
// case 3
mutationInfo[1] === 0)
) {
finishRequestAndUpdateState()
if (shouldStartNewRequest) {
if (isCurrentKeyMounted()) {
getConfig().onDiscarded(key)
}
}
return false
}
// Deep compare with latest state to avoid extra re-renders.
// For local state, compare and assign.
if (!compare(stateRef.current.data, newData)) {
newState.data = newData
} else {
// data and newData are deeply equal
// it should be safe to broadcast the stale data
newState.data = stateRef.current.data
// At the end of this function, `brocastState` invokes the `onStateUpdate` function,
// which takes care of avoiding the re-render
}
// For global state, it's possible that the key has changed.
// https://github.com/vercel/swr/pull/1058
if (!compare(cache.get(key), newData)) {
cache.set(key, newData)
}
// Trigger the successful callback if it's the original request.
if (shouldStartNewRequest) {
if (isCurrentKeyMounted()) {
getConfig().onSuccess(newData, key, config)
}
}
} catch (err) {
cleanupState()
// Not paused, we continue handling the error. Otherwise discard it.
if (!getConfig().isPaused()) {
// Get a new error, don't use deep comparison for errors.
patchFetchInfo({ error: err })
newState.error = err as Error
// Error event and retry logic. Only for the actual request, not
// deduped ones.
if (shouldStartNewRequest && isCurrentKeyMounted()) {
getConfig().onError(err, key, config)
if (
(typeof config.shouldRetryOnError === 'boolean' &&
config.shouldRetryOnError) ||
(isFunction(config.shouldRetryOnError) &&
config.shouldRetryOnError(err as Error))
) {
// When retrying, dedupe is always enabled
if (isActive()) {
// If it's active, stop. It will auto revalidate when refocusing
// or reconnecting.
getConfig().onErrorRetry(err, key, config, revalidate, {
retryCount: (opts.retryCount || 0) + 1,
dedupe: true
})
}
}
}
}
}
// Mark loading as stopped.
loading = false
// Update the current hook's state.
finishRequestAndUpdateState()
// Here is the source of the request, need to tell all other hooks to
// update their states.
if (isCurrentKeyMounted() && shouldStartNewRequest) {
broadcastState(cache, key, newState.data, newState.error, false)
}
return true
},
// `setState` is immutable, and `eventsCallback`, `fnArgs`, `keyInfo`,
// and `keyValidating` are depending on `key`, so we can exclude them from
// the deps array.
//
// FIXME:
// `fn` and `config` might be changed during the lifecycle,
// but they might be changed every render like this.
// `useSWR('key', () => fetch('/api/'), { suspense: true })`
// So we omit the values from the deps array
// even though it might cause unexpected behaviors.
// eslint-disable-next-line react-hooks/exhaustive-deps
[key]
)
// Similar to the global mutate, but bound to the current cache and key.
// `cache` isn't allowed to change during the lifecycle.
// eslint-disable-next-line react-hooks/exhaustive-deps
const boundMutate: SWRResponse<Data, Error>['mutate'] = useCallback(
// By using `bind` we don't need to modify the size of the rest arguments.
internalMutate.bind(UNDEFINED, cache, () => keyRef.current),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
)
// Always update fetcher and config refs.
useIsomorphicLayoutEffect(() => {
fetcherRef.current = fetcher
configRef.current = config
})
// After mounted or key changed.
useIsomorphicLayoutEffect(() => {
if (!key) return
// Not the initial render.
const keyChanged = initialMountedRef.current
const softRevalidate = revalidate.bind(UNDEFINED, WITH_DEDUPE)
// Expose state updater to global event listeners. So we can update hook's
// internal state from the outside.
const onStateUpdate: StateUpdateCallback<Data, Error> = (
updatedData,
updatedError,
updatedIsValidating
) => {
setState(
mergeObjects(
{
error: updatedError,
isValidating: updatedIsValidating
},
// Since `setState` only shallowly compares states, we do a deep
// comparison here.
compare(stateRef.current.data, updatedData)
? UNDEFINED
: {
data: updatedData
}
)
)
}
// Expose revalidators to global event listeners. So we can trigger
// revalidation from the outside.
let nextFocusRevalidatedAt = 0
const onRevalidate = (type: RevalidateEvent) => {
if (type == revalidateEvents.FOCUS_EVENT) {
const now = Date.now()
if (
getConfig().revalidateOnFocus &&
now > nextFocusRevalidatedAt &&
isActive()
) {
nextFocusRevalidatedAt = now + getConfig().focusThrottleInterval
softRevalidate()
}
} else if (type == revalidateEvents.RECONNECT_EVENT) {
if (getConfig().revalidateOnReconnect && isActive()) {
softRevalidate()
}
} else if (type == revalidateEvents.MUTATE_EVENT) {
return revalidate()
}
return
}
const unsubUpdate = subscribeCallback(key, STATE_UPDATERS, onStateUpdate)
const unsubEvents = subscribeCallback(key, EVENT_REVALIDATORS, onRevalidate)
// Mark the component as mounted and update corresponding refs.
unmountedRef.current = false
keyRef.current = key
initialMountedRef.current = true
// When `key` updates, reset the state to the initial value
// and trigger a rerender if necessary.
if (keyChanged) {
setState({
data,
error,
isValidating
})
}
// Trigger a revalidation.
if (shouldRevalidateOnMount()) {
if (isUndefined(data) || IS_SERVER) {
// Revalidate immediately.
softRevalidate()
} else {
// Delay the revalidate if we have data to return so we won't block
// rendering.
rAF(softRevalidate)
}
}
return () => {
// Mark it as unmounted.
unmountedRef.current = true
unsubUpdate()
unsubEvents()
}
}, [key, revalidate])
// Polling
useIsomorphicLayoutEffect(() => {
let timer: any
function next() {
// Use the passed interval
// ...or invoke the function with the updated data to get the interval
const interval = isFunction(refreshInterval)
? refreshInterval(data)
: refreshInterval
// We only start next interval if `refreshInterval` is not 0, and:
// - `force` is true, which is the start of polling
// - or `timer` is not 0, which means the effect wasn't canceled
if (interval && timer !== -1) {
timer = setTimeout(execute, interval)
}
}
function execute() {
// Check if it's OK to execute:
// Only revalidate when the page is visible, online and not errored.
if (
!stateRef.current.error &&
(refreshWhenHidden || getConfig().isVisible()) &&
(refreshWhenOffline || getConfig().isOnline())
) {
revalidate(WITH_DEDUPE).then(next)
} else {
// Schedule next interval to check again.
next()
}
}
next()
return () => {
if (timer) {
clearTimeout(timer)
timer = -1
}
}
}, [refreshInterval, refreshWhenHidden, refreshWhenOffline, revalidate])
// Display debug info in React DevTools.
useDebugValue(data)
// In Suspense mode, we can't return the empty `data` state.
// If there is `error`, the `error` needs to be thrown to the error boundary.
// If there is no `error`, the `revalidation` promise needs to be thrown to
// the suspense boundary.
if (suspense && isUndefined(data) && key) {
// Always update fetcher and config refs even with the Suspense mode.
fetcherRef.current = fetcher
configRef.current = config
throw isUndefined(error) ? revalidate(WITH_DEDUPE) : error
}
return {
mutate: boundMutate,
get data() {
stateDependencies.data = true
return data
},
get error() {
stateDependencies.error = true
return error
},
get isValidating() {
stateDependencies.isValidating = true
return isValidating
}
} as SWRResponse<Data, Error>
}
export const SWRConfig = OBJECT.defineProperty(ConfigProvider, 'default', {
value: defaultConfig
}) as typeof ConfigProvider & {
default: FullConfiguration
}
export const unstable_serialize = (key: Key) => serialize(key)[0]
export default withArgs<SWRHook>(useSWRHandler)