Skip to content

Commit 09228e0

Browse files
fix(react-db): handle rejected/stale setWindow promises in useLiveInfiniteQuery (#1269)
* fix(react-db): handle rejected/stale setWindow promises in useLiveInfiniteQuery Three issues in the useEffect that calls setWindow(): 1. No .catch() — if setWindow rejects, isFetchingNextPage stays true forever with no error surfaced. 2. No cleanup — if the effect re-runs while a previous promise is pending, the stale .then() can set isFetchingNextPage=false prematurely while new loading is in progress. 3. Unhandled promise rejection — the promise has no rejection handler, causing an unhandled rejection warning. Add .catch() with error logging, a cancelled flag with useEffect cleanup to ignore stale promise settlements. Fixes #1240 * add changeset * ci: apply automated fixes * refactor: use finally block for setIsFetchingNextPage * ci: apply automated fixes --------- Co-authored-by: Dmitrii Troitskii <jsleitor@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 60620b9 commit 09228e0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/react-db': patch
3+
---
4+
5+
fix(react-db): handle rejected/stale setWindow promises in useLiveInfiniteQuery

packages/react-db/src/useLiveInfiniteQuery.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,29 @@ export function useLiveInfiniteQuery<TContext extends Context>(
246246
if (!isCollection && !queryResult.isReady) return
247247

248248
// Adjust the window
249+
let cancelled = false
249250
const result = utils.setWindow({
250251
offset: expectedOffset,
251252
limit: expectedLimit,
252253
})
253254

254255
if (result !== true) {
255256
setIsFetchingNextPage(true)
256-
result.then(() => {
257-
setIsFetchingNextPage(false)
258-
})
257+
result
258+
.catch((error: unknown) => {
259+
if (!cancelled)
260+
console.error(`useLiveInfiniteQuery: setWindow failed:`, error)
261+
})
262+
.finally(() => {
263+
if (!cancelled) setIsFetchingNextPage(false)
264+
})
259265
} else {
260266
setIsFetchingNextPage(false)
261267
}
268+
269+
return () => {
270+
cancelled = true
271+
}
262272
}, [
263273
isCollection,
264274
queryResult.collection,

0 commit comments

Comments
 (0)