Skip to content

Commit 1e92b37

Browse files
committed
#1097 useInfiniteScroll's loaded-pages tracking desyncs from pruned data, permanently blocking refetch of evicted pages FIXED
1 parent be0246e commit 1e92b37

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

mobile/src/hooks/useInfiniteScroll.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ export const useInfiniteScroll = (config: InfiniteScrollConfig) => {
5858
/**
5959
* Cleanup and memory optimization
6060
*/
61+
const cleanLoadedPages = useCallback((removedCount: number) => {
62+
if (maxItems === 0 || removedCount <= 0) return;
63+
for (const page of Array.from(loadedPagesRef.current)) {
64+
if (page > 0 && page * pageSize <= removedCount) {
65+
loadedPagesRef.current.delete(page);
66+
}
67+
}
68+
}, [maxItems, pageSize]);
69+
6170
const pruneData = useCallback((items: any[], maxKeep: number) => {
6271
if (maxKeep === 0 || items.length <= maxKeep) {
6372
return items;
@@ -92,9 +101,12 @@ export const useInfiniteScroll = (config: InfiniteScrollConfig) => {
92101
try {
93102
const newItems = await pendingRequestRef.current.promise;
94103
if (isMountedRef.current) {
104+
const combined = [...state.data, ...newItems];
105+
const pruned = pruneData(combined, maxItems);
106+
cleanLoadedPages(combined.length - pruned.length);
95107
setState((prev) => ({
96108
...prev,
97-
data: pruneData([...prev.data, ...newItems], maxItems),
109+
data: pruned,
98110
page: nextPage,
99111
isFetching: false,
100112
}));
@@ -128,9 +140,12 @@ export const useInfiniteScroll = (config: InfiniteScrollConfig) => {
128140
}
129141

130142
const hasMoreData = newItems.length === pageSize;
143+
const combined = [...state.data, ...newItems];
144+
const pruned = pruneData(combined, maxItems);
145+
cleanLoadedPages(combined.length - pruned.length);
131146
setState((prev) => ({
132147
...prev,
133-
data: pruneData([...prev.data, ...newItems], maxItems),
148+
data: pruned,
134149
page: nextPage,
135150
hasMore: hasMoreData,
136151
isFetching: false,
@@ -149,7 +164,7 @@ export const useInfiniteScroll = (config: InfiniteScrollConfig) => {
149164
pendingRequestRef.current = null;
150165
}
151166
}
152-
}, [state.page, state.isFetching, state.isLoading, state.hasMore, onLoadMore, pageSize, maxItems, onError, pruneData]);
167+
}, [state.page, state.isFetching, state.isLoading, state.hasMore, onLoadMore, pageSize, maxItems, onError, pruneData, cleanLoadedPages]);
153168

154169
/**
155170
* Refresh data (reset to page 1)

0 commit comments

Comments
 (0)