Skip to content

Commit

Permalink
refactor: revert unnecessary diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Mar 10, 2021
1 parent 90b5548 commit 175b782
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/use-swr-infinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ function useSWRInfinite<Data = any, Error = any>(

const {
initialSize = 1,
// should revalidate pages when a refresh timer is invoked
revalidateAll = !!config.refreshInterval,
revalidateAll = false,
persistSize = false,
...extraConfig
} = config
Expand Down Expand Up @@ -84,26 +83,13 @@ function useSWRInfinite<Data = any, Error = any>(
() => cache.get(pageCountCacheKey) || initialSize,
[pageCountCacheKey, initialSize]
)
// this is used to support the persistSize option
const lastPageCountRef = useRef<number>(resolvePageCount())

// every time the key changes, we reset the page size if it's not persisted
useEffect(() => {
if (didMountRef.current) {
cache.set(
pageCountCacheKey,
persistSize ? lastPageCountRef.current : initialSize
)
swr.mutate()
} else {
didMountRef.current = true
}
// initialSize isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [firstPageKey])
// this is used to return the last fetched data while revalidating
const lastFetchedDataRef = useRef<Data[]>()

// keep the data inside a ref
const dataRef = useRef<Data[]>()
const lastFetchedDataRef = useRef<Data[]>()

// actual swr of all pages
const swr = useSWR<Data[], Error>(
Expand Down Expand Up @@ -134,10 +120,14 @@ function useSWRInfinite<Data = any, Error = any>(
// - `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 ||
(typeof force === 'undefined' &&
i === 0 &&
typeof dataRef.current !== 'undefined') ||
typeof pageData === 'undefined' ||
(originalData && !config.compare(originalData[i], pageData))

Expand All @@ -161,11 +151,7 @@ function useSWRInfinite<Data = any, Error = any>(
// return the data
return data
},
// do not pass initialData for updating to revalidate
{
...extraConfig,
initialData: didMountRef.current ? undefined : extraConfig.initialData
}
extraConfig
)

// update dataRef
Expand Down Expand Up @@ -206,6 +192,21 @@ function useSWRInfinite<Data = any, Error = any>(
[pageCountCacheKey, resolvePageCount, mutate]
)

// every time the key changes, we reset the page size if it's not persisted
useEffect(() => {
if (!didMountRef.current) {
didMountRef.current = true
return
}
cache.set(
pageCountCacheKey,
persistSize ? lastPageCountRef.current : initialSize
)
mutate(v => v)
// initialSize isn't allowed to change during the lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [firstPageKey])

// Use getter functions to avoid unnecessary re-renders caused by triggering all the getters of the returned swr object
const swrInfinite = { size: resolvePageCount(), setSize, mutate }
Object.defineProperties(swrInfinite, {
Expand All @@ -214,9 +215,7 @@ function useSWRInfinite<Data = any, Error = any>(
enumerable: true
},
data: {
get: () =>
// return the last data when revalidating
swr.data !== undefined ? swr.data : lastFetchedDataRef.current,
get: () => swr.data,
enumerable: true
},
// revalidate will be deprecated in the 1.x release
Expand Down

0 comments on commit 175b782

Please sign in to comment.