-
Notifications
You must be signed in to change notification settings - Fork 113
feat: implement useLiveInfiniteQuery hook for React #666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement useLiveInfiniteQuery hook for React #666
Conversation
🦋 Changeset detectedLatest commit: 6f7161c The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 83.6 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 1.46 kB ℹ️ View Unchanged
|
cf96419 to
248bdf4
Compare
652af67 to
58d7af8
Compare
248bdf4 to
42a9dc6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews I rebased this on main after merging the utils.setWindow() pr #663, then updated this to use that + added a couple of extra tests.
I think it's good to go, but want to check if you want to do anything docs wise?
I will add the isLoadingMore as part of #669
improve types add test that checks that we detect new pages on more rows syncing changeset tweaks
a29d881 to
9146c76
Compare
9146c76 to
6f7161c
Compare
|
I've changed this to be stacked on #669 and use the promise from |
* wip * refactor so both CollectionEventsManager and CollectionSubscription subclass the same event emiiter implimetation * changeset * rename loadMore to loadSubset * feed the subscription object through to the loadSubset call, and add an unsunbscribed event to it * feed subscription through to the loadSubset callback, add unsubscribe event to the subscription, fix types * add sync mode to base colleciton * loadSubset fn return promise or true * add comment on setting is loading * address review * remove public trackLoadPromise * setWindow returns a promise when it triggers loading subset * feat: implement useLiveInfiniteQuery hook for React (#666) * feat: implement useLiveInfiniteQuery hook for React * use the new utils.setWindow to page through the results improve types add test that checks that we detect new pages on more rows syncing changeset tweaks * isFetchingNextPage set by promise from setWindow --------- Co-authored-by: Sam Willis <[email protected]> --------- Co-authored-by: Kyle Mathews <[email protected]>
stacked on #669
Summary
Implements
useLiveInfiniteQueryhook for React, providing infinite scrolling/pagination with live updates and async loading state tracking.This PR adds a new
useLiveInfiniteQueryhook that combines TanStack Query's infinite query pattern with TanStack DB's live query reactivity. The hook provides a familiar API for pagination while automatically updating pages as underlying data changes, with full async loading state management.What's Included
New
useLiveInfiniteQueryhook with TanStack Query-compatible APIpages: Array of page arraysdata: Flattened array of all loaded itemspageParams: Array of page parameters usedfetchNextPage(): Load the next pagehasNextPage: Boolean indicating if more pages are availableisFetchingNextPage: Boolean tracking async loading state fromsetWindowuseLiveQueryproperties (status, isLoading, isReady, collection, etc.)Automatic live updates: Pages update reactively when underlying data changes
Async loading state tracking: Properly tracks when
setWindowis loading additional dataType-safe utils checking: Custom type guard for
LiveQueryCollectionUtilsusing TypeScript'siskeywordDependency tracking: Resets pagination when query dependencies change
Intelligent next page detection: Automatically detects when new data becomes available
Concurrent fetch prevention: Prevents multiple simultaneous page loads
Comprehensive test suite: 17 tests covering pagination, live updates, async loading, sync detection, and edge cases
Implementation Details
Efficient Window Management:
liveQueryCollection.utils.setWindow()to dynamically adjust pagination window.orderBy())Async Loading State Management:
setWindowreturnstruewhen data is immediately available (synchronous)setWindowreturnsPromise<void>when loading additional data (asynchronous)isFetchingNextPagestate tracks promise resolutionLive Reactivity:
hasNextPagewhen data availability changesType Safety:
isLiveQueryCollectionUtilstype guard ensures proper typingispredicate for accurate type narrowingMemory Efficient:
Example Usage
Test Coverage
All 17 tests passing:
isFetchingNextPagewith immediate dataisFetchingNextPagewith async loadingisFetchingNextPageis true95% statement coverage on
useLiveInfiniteQuery.Closes #613