Request pages until we receive more items

This commit is contained in:
Dan Abramov
2024-07-31 16:25:51 +01:00
parent c75bb65bef
commit c0c1572719
2 changed files with 10 additions and 28 deletions
+5 -14
View File
@@ -59,7 +59,6 @@ export function useNotificationFeedQuery(opts?: {
const moderationOpts = useModerationOpts() const moderationOpts = useModerationOpts()
const unreads = useUnreadNotificationsApi() const unreads = useUnreadNotificationsApi()
const enabled = opts?.enabled !== false const enabled = opts?.enabled !== false
const lastPageCountRef = useRef(0)
const gate = useGate() const gate = useGate()
// false: force showing all notifications // false: force showing all notifications
@@ -121,29 +120,21 @@ export function useNotificationFeedQuery(opts?: {
}, },
}) })
const lastItemsCountRef = useRef(0)
useEffect(() => { useEffect(() => {
const {isFetching, hasNextPage, data} = query const {isFetching, hasNextPage, data} = query
if (isFetching || !hasNextPage) { if (isFetching || !hasNextPage) {
return return
} }
// avoid double-fires of fetchNextPage()
if (
lastPageCountRef.current !== 0 &&
lastPageCountRef.current === data?.pages?.length
) {
return
}
// fetch next page if we haven't gotten a full page of content
let count = 0 let count = 0
for (const page of data?.pages || []) { for (const page of data?.pages || []) {
count += page.items.length count += page.items.length
} }
if (count < PAGE_SIZE && (data?.pages.length || 0) < 6) { if (count > lastItemsCountRef.current) {
query.fetchNextPage() lastItemsCountRef.current = count
lastPageCountRef.current = data?.pages?.length || 0 return
} }
query.fetchNextPage()
}, [query]) }, [query])
return query return query
+5 -14
View File
@@ -134,7 +134,6 @@ export function usePostFeedQuery(
args: typeof selectArgs args: typeof selectArgs
result: InfiniteData<FeedPage> result: InfiniteData<FeedPage>
} | null>(null) } | null>(null)
const lastPageCountRef = useRef(0)
const isDiscover = feedDesc.includes(DISCOVER_FEED_URI) const isDiscover = feedDesc.includes(DISCOVER_FEED_URI)
// Make sure this doesn't invalidate unless really needed. // Make sure this doesn't invalidate unless really needed.
@@ -376,31 +375,23 @@ export function usePostFeedQuery(
), ),
}) })
const lastItemsCountRef = useRef(0)
useEffect(() => { useEffect(() => {
const {isFetching, hasNextPage, data} = query const {isFetching, hasNextPage, data} = query
if (isFetching || !hasNextPage) { if (isFetching || !hasNextPage) {
return return
} }
// avoid double-fires of fetchNextPage()
if (
lastPageCountRef.current !== 0 &&
lastPageCountRef.current === data?.pages?.length
) {
return
}
// fetch next page if we haven't gotten a full page of content
let count = 0 let count = 0
for (const page of data?.pages || []) { for (const page of data?.pages || []) {
for (const slice of page.slices) { for (const slice of page.slices) {
count += slice.items.length count += slice.items.length
} }
} }
if (count < PAGE_SIZE && (data?.pages.length || 0) < 6) { if (count > lastItemsCountRef.current) {
query.fetchNextPage() lastItemsCountRef.current = count
lastPageCountRef.current = data?.pages?.length || 0 return
} }
query.fetchNextPage()
}, [query]) }, [query])
return query return query