Try to fix refetch

This commit is contained in:
Zhiyuan Zheng 2021-03-06 17:55:13 +01:00
parent 89ca588116
commit 17f15a199c
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
3 changed files with 55 additions and 24 deletions

View File

@ -62,11 +62,15 @@ const Timeline: React.FC<Props> = ({
android: ['dataUpdatedAt', 'isFetching', 'isLoading'] android: ['dataUpdatedAt', 'isFetching', 'isLoading']
}), }),
getNextPageParam: lastPage => getNextPageParam: lastPage =>
lastPage?.links?.next && { max_id: lastPage.links.next } lastPage?.links?.next && {
max_id: lastPage.links.next
}
} }
}) })
const flattenData = data?.pages ? data.pages.flatMap(d => [...d.body]) : [] const flattenData = data?.pages
? data.pages.flatMap(page => [...page.body])
: []
const ItemSeparatorComponent = useCallback( const ItemSeparatorComponent = useCallback(
({ leadingItem }) => ({ leadingItem }) =>
@ -134,7 +138,7 @@ const Timeline: React.FC<Props> = ({
ref={customFLRef || flRef} ref={customFLRef || flRef}
scrollEventThrottle={16} scrollEventThrottle={16}
onScroll={onScroll} onScroll={onScroll}
windowSize={8} windowSize={10}
data={flattenData} data={flattenData}
initialNumToRender={6} initialNumToRender={6}
maxToRenderPerBatch={3} maxToRenderPerBatch={3}

View File

@ -50,9 +50,9 @@ const TimelineRefresh: React.FC<Props> = ({
} }
const fetchingLatestIndex = useRef(0) const fetchingLatestIndex = useRef(0)
const refetchActive = useRef(false)
const { const {
data,
refetch, refetch,
isFetching, isFetching,
isLoading, isLoading,
@ -68,9 +68,17 @@ const TimelineRefresh: React.FC<Props> = ({
// https://github.com/facebook/react-native/issues/25239#issuecomment-731100372 // https://github.com/facebook/react-native/issues/25239#issuecomment-731100372
limit: '5' limit: '5'
}, },
select: data => {
if (refetchActive.current) {
data.pageParams = [data.pageParams[0]]
data.pages = [data.pages[0]]
refetchActive.current = false
}
return data
},
onSuccess: () => { onSuccess: () => {
if (fetchingLatestIndex.current > 0) { if (fetchingLatestIndex.current > 0) {
if (fetchingLatestIndex.current > 8) { if (fetchingLatestIndex.current > 5) {
clearFirstPage() clearFirstPage()
fetchingLatestIndex.current = 0 fetchingLatestIndex.current = 0
} else { } else {
@ -91,23 +99,41 @@ const TimelineRefresh: React.FC<Props> = ({
const { theme } = useTheme() const { theme } = useTheme()
const queryClient = useQueryClient() const queryClient = useQueryClient()
const clearFirstPage = useCallback(() => { const clearFirstPage = () => {
if (data?.pages[0].body.length === 0) { queryClient.setQueryData<InfiniteData<TimelineData> | undefined>(
queryClient.setQueryData<InfiniteData<TimelineData> | undefined>( queryKey,
queryKey, data => {
data => { if (data?.pages[0].body.length === 0) {
if (data?.pages[0].body.length === 0) { return {
return { pages: data.pages.slice(1),
pages: data.pages.slice(1), pageParams: data.pageParams.slice(1)
pageParams: data.pageParams.slice(1)
}
} else {
return data
} }
} else {
return data
} }
) }
} )
}, [data?.pages.length && data?.pages[0].body.length]) }
const prepareRefetch = () => {
refetchActive.current = true
queryClient.setQueryData<InfiniteData<TimelineData> | undefined>(
queryKey,
data => {
if (data) {
data.pageParams = [undefined]
const newFirstPage: TimelineData = { body: [] }
for (let page of data.pages) {
// @ts-ignore
newFirstPage.body.push(...page.body)
if (newFirstPage.body.length > 10) break
}
data.pages = [newFirstPage]
}
return data
}
)
}
const [textRight, setTextRight] = useState(0) const [textRight, setTextRight] = useState(0)
const arrowY = useAnimatedStyle(() => ({ const arrowY = useAnimatedStyle(() => ({
@ -178,9 +204,10 @@ const TimelineRefresh: React.FC<Props> = ({
}, },
[isFetching] [isFetching]
) )
const wrapper = () => { const wrapperStartLatest = () => {
fetchingLatestIndex.current = 1 fetchingLatestIndex.current = 1
} }
useAnimatedReaction( useAnimatedReaction(
() => { () => {
return fetchingType.value return fetchingType.value
@ -189,12 +216,12 @@ const TimelineRefresh: React.FC<Props> = ({
fetchingType.value = 0 fetchingType.value = 0
switch (data) { switch (data) {
case 1: case 1:
runOnJS(wrapper)() runOnJS(wrapperStartLatest)()
runOnJS(clearFirstPage)() runOnJS(clearFirstPage)()
runOnJS(fetchPreviousPage)() runOnJS(fetchPreviousPage)()
break break
case 2: case 2:
runOnJS(clearFirstPage)() runOnJS(prepareRefetch)()
runOnJS(refetch)() runOnJS(refetch)()
break break
} }

View File

@ -39,7 +39,7 @@ const SettingsTooot: React.FC = () => {
iconBack='ChevronRight' iconBack='ChevronRight'
onPress={() => { onPress={() => {
analytics('settings_support_press') analytics('settings_support_press')
Linking.openURL('https://www.patreon.com/xmflsct') Linking.openURL('https://www.buymeacoffee.com/xmflsct')
}} }}
/> />
{__DEV__ || {__DEV__ ||