Skip to content

Commit

Permalink
fix: add reset function to swr pages
Browse files Browse the repository at this point in the history
  • Loading branch information
innocentiv committed Feb 27, 2020
1 parent cd893dc commit 57b9cf2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export type pagesResponseInterface = {
isReachingEnd: boolean
isEmpty: boolean
loadMore: () => void
resetPages: () => void
}

export type actionType<Data, Error> = {
Expand Down
49 changes: 32 additions & 17 deletions src/use-swr-pages.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useCallback, useMemo, useState, useRef } from 'react'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'

import { cache } from './config'
import {
pagesResponseInterface,
responseInterface,
pageComponentType,
pageOffsetMapperType
pageOffsetMapperType,
pagesResponseInterface,
responseInterface
} from './types'

/*
Expand Down Expand Up @@ -53,7 +53,8 @@ function App () {
isLoadingMore,
isReachingEnd,
isEmpty,
loadMore
loadMore,
resetPages
} = useSWRPages(
'project-page', // key of this page
Expand Down Expand Up @@ -96,8 +97,8 @@ export function useSWRPages<OffsetType = any, Data = any, Error = any>(
SWRToOffset: pageOffsetMapperType<OffsetType, Data, Error>,
deps: any[] = []
): pagesResponseInterface {
const pageCountKey = `_swr_page_count_` + pageKey
const pageOffsetKey = `_swr_page_offset_` + pageKey
const pageCountKey = '_swr_page_count_' + pageKey
const pageOffsetKey = '_swr_page_offset_' + pageKey

const [pageCount, setPageCount] = useState<number>(
cache.get(pageCountKey) || 1
Expand All @@ -118,26 +119,39 @@ export function useSWRPages<OffsetType = any, Data = any, Error = any>(

// if dataList is [], we can assume this page is empty
// TODO: this API is not stable
if (dataList && !dataList.length) {
emptyPageRef.current = true
} else {
emptyPageRef.current = false
}

emptyPageRef.current = dataList && !dataList.length
return dataList
}, [])

const resetPages = useCallback(() => {
setPageSWRs([])
setPageCount(() => {
cache.set(pageCountKey, 1)
return 1
})
setPageOffsets(() => {
cache.set(pageOffsetKey, [null])
return [null]
})
pageCacheMap.set(pageKey, [])
}, [pageOffsetKey, pageCountKey])

// Reset page if the key changes
useEffect(() => resetPages(), [resetPages])

// Doesn't have a next page
const isReachingEnd = pageOffsets[pageCount] === null
const isLoadingMore = pageCount === pageOffsets.length
const isLoadingMore = pageCount > pageOffsets.length
const isEmpty = isReachingEnd && pageCount === 1 && emptyPageRef.current

const loadMore = useCallback(() => {
if (isLoadingMore || isReachingEnd) return
setPageCount(c => {
cache.set(pageCountKey, c + 1)
return c + 1
})
}, [isLoadingMore || isReachingEnd])
}, [isLoadingMore, isReachingEnd, pageCountKey])

const _pageFn = useCallback(pageFn, deps)
pageFnRef.current = _pageFn

Expand Down Expand Up @@ -205,7 +219,7 @@ export function useSWRPages<OffsetType = any, Data = any, Error = any>(
p.push(pageCache[i].component)
}
return p
}, [_pageFn, pageCount, pageSWRs, pageOffsets, pageKey])
}, [_pageFn, pageCount, pageSWRs, pageOffsets, pageKey, pageOffsetKey])

return {
pages,
Expand All @@ -214,6 +228,7 @@ export function useSWRPages<OffsetType = any, Data = any, Error = any>(
isLoadingMore,
isReachingEnd,
isEmpty,
loadMore
loadMore,
resetPages
}
}

0 comments on commit 57b9cf2

Please sign in to comment.