forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
284 lines (239 loc) · 8.65 KB
/
index.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
// We have to several type castings here because `useSWRInfinite` is a special
// hook where `key` and return type are not like the normal `useSWR` types.
import { useRef, useState, useCallback } from 'react'
import useSWR, {
SWRConfig,
SWRHook,
MutatorCallback,
Middleware,
BareFetcher
} from 'swr'
import { useIsomorphicLayoutEffect } from '../src/utils/env'
import { serialize } from '../src/utils/serialize'
import { isUndefined, isFunction, UNDEFINED } from '../src/utils/helper'
import { withMiddleware } from '../src/utils/with-middleware'
import { createCacheHelper } from '../src/utils/cache'
import type {
SWRInfiniteConfiguration,
SWRInfiniteResponse,
SWRInfiniteHook,
SWRInfiniteKeyLoader,
SWRInfiniteFetcher
} from './types'
const INFINITE_PREFIX = '$inf$'
const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => {
return serialize(getKey ? getKey(0, null) : null)[0]
}
export const unstable_serialize = (getKey: SWRInfiniteKeyLoader) => {
return INFINITE_PREFIX + getFirstPageKey(getKey)
}
export const infinite = (<Data, Error>(useSWRNext: SWRHook) =>
(
getKey: SWRInfiniteKeyLoader,
fn: BareFetcher<Data> | null,
config: Omit<typeof SWRConfig.default, 'fetcher'> &
Omit<SWRInfiniteConfiguration<Data, Error>, 'fetcher'>
): SWRInfiniteResponse<Data, Error> => {
const rerender = useState({})[1]
const didMountRef = useRef<boolean>(false)
const dataRef = useRef<Data[]>()
const {
cache,
initialSize = 1,
revalidateAll = false,
persistSize = false,
revalidateFirstPage = true,
revalidateOnMount = false
} = config
// The serialized key of the first page. This key will be used to store
// metadata of this SWR infinite hook.
let infiniteKey: string | undefined
try {
infiniteKey = getFirstPageKey(getKey)
if (infiniteKey) infiniteKey = INFINITE_PREFIX + infiniteKey
} catch (err) {
// Not ready yet.
}
const [get, set] = createCacheHelper<
Data,
{
// We use cache to pass extra info (context) to fetcher so it can be globally
// shared. The key of the context data is based on the first page key.
$ctx: [boolean] | [boolean, Data[] | undefined]
// Page size is also cached to share the page data between hooks with the
// same key.
$len: number
}
>(cache, infiniteKey)
const resolvePageSize = useCallback((): number => {
const cachedPageSize = get().$len
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize
// `cache` isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [infiniteKey, initialSize])
// keep the last page size to restore it with the persistSize option
const lastPageSizeRef = useRef<number>(resolvePageSize())
// When the page key changes, we reset the page size if it's not persisted
useIsomorphicLayoutEffect(() => {
if (!didMountRef.current) {
didMountRef.current = true
return
}
if (infiniteKey) {
// If the key has been changed, we keep the current page size if persistSize is enabled
set({ $len: persistSize ? lastPageSizeRef.current : initialSize })
}
// `initialSize` isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [infiniteKey])
// Needs to check didMountRef during mounting, not in the fetcher
const shouldRevalidateOnMount = revalidateOnMount && !didMountRef.current
// Actual SWR hook to load all pages in one fetcher.
const swr = useSWRNext<Data[], Error>(
infiniteKey,
async () => {
// get the revalidate context
const [forceRevalidateAll, originalData] = get().$ctx || []
// return an array of page data
const data: Data[] = []
const pageSize = resolvePageSize()
let previousPageData = null
for (let i = 0; i < pageSize; ++i) {
const [pageKey, pageArg] = serialize(getKey(i, previousPageData))
if (!pageKey) {
// `pageKey` is falsy, stop fetching new pages.
break
}
// Get the cached page data.
let pageData = cache.get(pageKey)?.data
// should fetch (or revalidate) if:
// - `revalidateAll` is enabled
// - `mutate()` called
// - the cache is missing
// - it's the first page and it's not the initial render
// - `revalidateOnMount` is enabled and it's on mount
// - cache for that page has changed
const shouldFetchPage =
revalidateAll ||
forceRevalidateAll ||
isUndefined(pageData) ||
(revalidateFirstPage && !i && !isUndefined(dataRef.current)) ||
shouldRevalidateOnMount ||
(originalData &&
!isUndefined(originalData[i]) &&
!config.compare(originalData[i], pageData))
if (fn && shouldFetchPage) {
pageData = await fn(pageArg)
cache.set(pageKey, { ...cache.get(pageKey), data: pageData })
}
data.push(pageData)
previousPageData = pageData
}
// once we executed the data fetching based on the context, clear the context
set({ $ctx: UNDEFINED })
// return the data
return data
},
config
)
// update dataRef
useIsomorphicLayoutEffect(() => {
dataRef.current = swr.data
}, [swr.data])
const mutate = useCallback(
(
...args:
| []
| [undefined | Data[] | Promise<Data[]> | MutatorCallback<Data[]>]
| [
undefined | Data[] | Promise<Data[]> | MutatorCallback<Data[]>,
boolean
]
) => {
const data = args[0]
// Default to true.
const shouldRevalidate = args[1] !== false
// It is possible that the key is still falsy.
if (!infiniteKey) return
if (shouldRevalidate) {
if (!isUndefined(data)) {
// We only revalidate the pages that are changed
const originalData = dataRef.current
set({ $ctx: [false, originalData] })
} else {
// Calling `mutate()`, we revalidate all pages
set({ $ctx: [true] })
}
}
return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate()
},
// swr.mutate is always the same reference
// eslint-disable-next-line react-hooks/exhaustive-deps
[infiniteKey]
)
// Function to load pages data from the cache based on the page size.
const resolvePagesFromCache = (pageSize: number): Data[] | undefined => {
// return an array of page data
const data: Data[] = []
let previousPageData = null
for (let i = 0; i < pageSize; ++i) {
const [pageKey] = serialize(getKey(i, previousPageData))
// Get the cached page data.
const pageData = pageKey ? cache.get(pageKey)?.data : UNDEFINED
// Return the current data if we can't get it from the cache.
if (isUndefined(pageData)) return dataRef.current
data.push(pageData)
previousPageData = pageData
}
// Return the data
return data
}
// Extend the SWR API
const setSize = useCallback(
(arg: number | ((size: number) => number)) => {
// It is possible that the key is still falsy.
if (!infiniteKey) return
let size
if (isFunction(arg)) {
size = arg(resolvePageSize())
} else if (typeof arg == 'number') {
size = arg
}
if (typeof size != 'number') return
set({ $len: size })
lastPageSizeRef.current = size
rerender({})
return mutate(resolvePagesFromCache(size))
},
// `cache` and `rerender` isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
[infiniteKey, resolvePageSize, mutate]
)
// Use getter functions to avoid unnecessary re-renders caused by triggering
// all the getters of the returned swr object.
return {
size: resolvePageSize(),
setSize,
mutate,
get data() {
return swr.data
},
get error() {
return swr.error
},
get isValidating() {
return swr.isValidating
},
get isLoading() {
return swr.isLoading
}
} as SWRInfiniteResponse<Data, Error>
}) as unknown as Middleware
export default withMiddleware(useSWR, infinite) as SWRInfiniteHook
export {
SWRInfiniteConfiguration,
SWRInfiniteResponse,
SWRInfiniteHook,
SWRInfiniteKeyLoader,
SWRInfiniteFetcher
}