You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(db): useLiveInfiniteQuery pagination with async on-demand loadSubset (#1209)
* test: add failing test for useLiveInfiniteQuery peek-ahead limit bug
This test documents a bug where useLiveInfiniteQuery doesn't request
pageSize+1 items from loadSubset for hasNextPage peek-ahead detection.
The bug causes hasNextPage to always return false when using on-demand
sync mode with Electric collections, because:
1. useLiveInfiniteQuery calls setWindow({ limit: pageSize + 1 }) in useEffect
2. But subscribeToOrderedChanges calls requestLimitedSnapshot BEFORE the
useEffect runs, using the original compiled limit (pageSize)
3. The loadSubset function receives limit=pageSize instead of limit=pageSize+1
4. This prevents the peek-ahead strategy from working correctly
Related: Discord bug report about useLiveInfiniteQuery + Electric on-demand
https://claude.ai/code/session_01FskX2noxNAj1zCiALFQXnC
* ci: apply automated fixes
* fix(react-db): use pageSize+1 in initial query for peek-ahead detection
The initial query was using `.limit(pageSize)` but `setWindow` expects
`pageSize + 1` for peek-ahead detection. This caused a race condition
where the first `requestLimitedSnapshot` was called with `limit = pageSize`
before `setWindow` could adjust it to `pageSize + 1`.
The fix uses `pageSize + 1` from the start so the compiled query includes
the peek-ahead limit, ensuring `loadSubset` receives the correct limit
for `hasNextPage` detection.
https://claude.ai/code/session_01FskX2noxNAj1zCiALFQXnC
* ci: apply automated fixes
* refactor(tests): simplify useLiveInfiniteQuery test assertions
- Fix unused parameter lint warnings (allPages -> _allPages)
- Simplify test logic using .find() instead of .filter()[0]
- Condense redundant comments
Co-Authored-By: Claude Opus 4.5 <[email protected]>
* chore: add changeset for peek-ahead fix
Co-Authored-By: Claude Opus 4.5 <[email protected]>
* test: add e2e test for useLiveInfiniteQuery with on-demand collection
Replaces the previous implementation-detail test with a proper e2e test
that verifies the actual behavior of useLiveInfiniteQuery with on-demand
collections:
- Initial page loads correctly with hasNextPage=true
- fetchNextPage() actually loads more data via loadSubset
- Multiple pages can be fetched with correct items
- hasNextPage correctly reflects when no more data exists
This test catches bugs where the incremental sync doesn't properly
fetch data from the backend when paginating.
https://claude.ai/code/session_01FskX2noxNAj1zCiALFQXnC
* test: add failing test for async loadSubset pagination bug
This test reproduces a bug where useLiveInfiniteQuery doesn't fetch
subsequent pages when loadSubset returns a Promise (async mode).
Root cause identified in collection-subscriber.ts:
- When loadSubset returns a Promise, pendingOrderedLoadPromise is set
- loadMoreIfNeeded returns early while the promise is pending
- When the promise resolves, pendingOrderedLoadPromise is cleared
- BUT loadMoreIfNeeded is NOT re-triggered to check if more data is needed
This affects Electric on-demand mode where all data comes from async
loadSubset calls. The initial page loads correctly, but fetchNextPage
fails to trigger additional loadSubset calls.
https://claude.ai/code/session_01FskX2noxNAj1zCiALFQXnC
* fix(db): re-trigger loadMoreIfNeeded when async loadSubset completes
When useLiveInfiniteQuery uses an on-demand collection with async
loadSubset, the second page was never loaded because:
1. When setWindow() was called for the next page, maybeRunGraph's
callback was never called because the graph had no pending work
This fix ensures the graph run callback is called at least once even
when there's no pending work, so setWindow() can trigger loadMoreIfNeeded
for lazy loading scenarios.
https://claude.ai/code/session_01FskX2noxNAj1zCiALFQXnC
* refactor: simplify tests, extend async coverage, fix changeset
- Extract createOnDemandCollection helper to reduce test duplication
- Extend async on-demand test to verify all 3 pages and hasNextPage=false
- Add peek-ahead boundary test for pageSize+1 items
- Replace silent catch block with explicit re-throw in test helper
- Add @tanstack/db to changeset (independently versioned)
- Remove redundant comments and tighten callback-guarantee comment
Co-Authored-By: Claude Opus 4.6 <[email protected]>
* ci: apply automated fixes
---------
Co-authored-by: Claude <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Fix `useLiveInfiniteQuery` peek-ahead detection for `hasNextPage`. The initial query now correctly requests `pageSize + 1` items to detect whether additional pages exist, matching the behavior of subsequent page loads.
7
+
8
+
Fix async on-demand pagination by ensuring the graph callback fires at least once even when there is no pending graph work, so that `loadMoreIfNeeded` is triggered after `setWindow()` increases the limit.
0 commit comments