-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.ts
241 lines (210 loc) · 7.37 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
// 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,
KeyLoader,
Fetcher,
SWRHook,
MutatorCallback,
Middleware
} from 'swr'
import { useIsomorphicLayoutEffect } from '../src/utils/env'
import { serialize } from '../src/utils/serialize'
import { isUndefined, UNDEFINED } from '../src/utils/helper'
import { withMiddleware } from '../src/utils/with-middleware'
import { SWRInfiniteConfiguration, SWRInfiniteResponse } from './types'
export const infinite = ((<Data, Error>(useSWRNext: SWRHook) => (
getKey: KeyLoader<Data>,
fn: Fetcher<Data> | null,
config: typeof SWRConfig.default & SWRInfiniteConfiguration<Data, Error>
): SWRInfiniteResponse<Data, Error> => {
const {
cache,
initialSize = 1,
revalidateAll = false,
persistSize = false
} = config
// get the serialized key of the first page
let firstPageKey: string | null = null
try {
firstPageKey = serialize(getKey ? getKey(0, null) : null)[0]
} catch (err) {
// not ready
}
const rerender = useState({})[1]
// we use cache to pass extra info (context) to fetcher so it can be globally shared
// here we get the key of the fetcher context cache
let contextCacheKey: string | null = null
if (firstPageKey) {
contextCacheKey = '$ctx$' + firstPageKey
}
// page size is also cached to share the page data between hooks having the same key
let pageSizeCacheKey: string | null = null
if (firstPageKey) {
pageSizeCacheKey = '$len$' + firstPageKey
}
const didMountRef = useRef<boolean>(false)
const resolvePageSize = useCallback((): number => {
const cachedPageSize = cache.get(pageSizeCacheKey)
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize
// `cache` isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pageSizeCacheKey, 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 (firstPageKey) {
// If the key has been changed, we keep the current page size if persistSize is enabled
cache.set(
pageSizeCacheKey,
persistSize ? lastPageSizeRef.current : initialSize
)
}
// `initialSize` isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [firstPageKey])
// keep the data inside a ref
const dataRef = useRef<Data[]>()
// actual swr of all pages
const swr = useSWRNext<Data[], Error>(
firstPageKey ? '$inf$' + firstPageKey : null,
async () => {
// get the revalidate context
const { data: originalData, force } = cache.get(contextCacheKey) || {}
// return an array of page data
const data: Data[] = []
const pageSize = resolvePageSize()
let previousPageData = null
for (let i = 0; i < pageSize; ++i) {
const [pageKey, pageArgs] = serialize(
getKey ? getKey(i, previousPageData) : null
)
if (!pageKey) {
// pageKey is falsy, stop fetching next pages
break
}
// get the current page cache
let pageData = cache.get(pageKey)
// should fetch (or revalidate) if:
// - `revalidateAll` is enabled
// - `mutate()` called
// - the cache is missing
// - it's the first page and it's not the first render
// - cache has changed
const shouldFetchPage =
revalidateAll ||
force ||
isUndefined(pageData) ||
(isUndefined(force) && i === 0 && !isUndefined(dataRef.current)) ||
(originalData && !config.compare(originalData[i], pageData))
if (fn && shouldFetchPage) {
if (pageArgs !== null) {
pageData = await fn(...pageArgs)
} else {
pageData = await fn(pageKey)
}
cache.set(pageKey, pageData)
}
data.push(pageData)
previousPageData = pageData
}
// once we executed the data fetching based on the context, clear the context
cache.delete(contextCacheKey)
// return the data
return data
},
config
)
// update dataRef
useIsomorphicLayoutEffect(() => {
dataRef.current = swr.data
}, [swr.data])
const mutate = useCallback(
(data: MutatorCallback, shouldRevalidate = true) => {
// It is possible that the key is still falsy.
if (!contextCacheKey) return UNDEFINED
if (shouldRevalidate && !isUndefined(data)) {
// we only revalidate the pages that are changed
const originalData = dataRef.current
cache.set(contextCacheKey, { data: originalData, force: false })
} else if (shouldRevalidate) {
// calling `mutate()`, we revalidate all pages
cache.set(contextCacheKey, { force: true })
}
return swr.mutate(data, shouldRevalidate)
},
// swr.mutate is always the same reference
// eslint-disable-next-line react-hooks/exhaustive-deps
[contextCacheKey]
)
// extend the SWR API
const setSize = useCallback(
(arg: number | ((size: number) => number)) => {
// It is possible that the key is still falsy.
if (!pageSizeCacheKey) return UNDEFINED
let size
if (typeof arg === 'function') {
size = arg(resolvePageSize())
} else if (typeof arg === 'number') {
size = arg
}
if (typeof size === 'number') {
cache.set(pageSizeCacheKey, size)
lastPageSizeRef.current = size
}
rerender({})
return mutate(v => v)
},
// `cache` and `rerender` isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
[pageSizeCacheKey, resolvePageSize, mutate]
)
// Use getter functions to avoid unnecessary re-renders caused by triggering
// all the getters of the returned swr object.
return Object.defineProperties(
{ size: resolvePageSize(), setSize, mutate },
{
error: {
get: () => swr.error,
enumerable: true
},
data: {
get: () => swr.data,
enumerable: true
},
// revalidate will be deprecated in the 1.x release
// because mutate() covers the same use case of revalidate().
// This remains only for backward compatibility
revalidate: {
get: () => swr.revalidate,
enumerable: true
},
isValidating: {
get: () => swr.isValidating,
enumerable: true
}
}
) as SWRInfiniteResponse<Data, Error>
}) as unknown) as Middleware
type SWRInfiniteHook = <Data = any, Error = any>(
...args:
| readonly [KeyLoader<Data>]
| readonly [KeyLoader<Data>, Fetcher<Data> | null]
| readonly [
KeyLoader<Data>,
SWRInfiniteConfiguration<Data, Error> | undefined
]
| readonly [
KeyLoader<Data>,
Fetcher<Data> | null,
SWRInfiniteConfiguration<Data, Error> | undefined
]
) => SWRInfiniteResponse<Data, Error>
export default withMiddleware(useSWR, infinite) as SWRInfiniteHook
export { SWRInfiniteConfiguration, SWRInfiniteResponse }