-
Notifications
You must be signed in to change notification settings - Fork 104
Add loadSubset State Tracking and On-Demand Sync Mode #669
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f329fb1
wip
samwillis dc34f5b
refactor so both CollectionEventsManager and CollectionSubscription s…
samwillis b7f45c8
changeset
samwillis d6c6e7e
rename loadMore to loadSubset
samwillis 7b584b1
feed the subscription object through to the loadSubset call, and add …
samwillis 5636c46
feed subscription through to the loadSubset callback, add unsubscribe…
samwillis b1aeceb
fix types
samwillis 53fa027
add sync mode to base colleciton
samwillis f78c8e1
loadSubset fn return promise or true
samwillis 9ad1169
add comment on setting is loading
samwillis 0a0bbb0
address review
samwillis 8b1c68d
Merge branch 'main' into samwillis/load-more-tracking
samwillis f8cd6fe
remove public trackLoadPromise
samwillis 1c54b1b
setWindow returns a promise when it triggers loading subset
samwillis f31a67e
feat: implement useLiveInfiniteQuery hook for React (#666)
KyleAMathews File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tanstack/db": patch | ||
--- | ||
|
||
Added `isLoadingMore` property and `loadingMore:change` events to collections and live queries, enabling UIs to display loading indicators when more data is being fetched via `syncMore`. Each live query maintains its own isolated loading state based on its subscriptions, preventing loading status "bleed" between independent queries that share the same source collections. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
"@tanstack/react-db": patch | ||
--- | ||
|
||
Add `useLiveInfiniteQuery` hook for infinite scrolling with live updates. | ||
|
||
The new `useLiveInfiniteQuery` hook provides an infinite query pattern similar to TanStack Query's `useInfiniteQuery`, but with live updates from your local collection. It uses `liveQueryCollection.utils.setWindow()` internally to efficiently paginate through ordered data without recreating the query on each page fetch. | ||
|
||
**Key features:** | ||
|
||
- Automatic live updates as data changes in the collection | ||
- Efficient pagination using dynamic window adjustment | ||
- Peek-ahead mechanism to detect when more pages are available | ||
- Compatible with TanStack Query's infinite query API patterns | ||
|
||
**Example usage:** | ||
|
||
```tsx | ||
import { useLiveInfiniteQuery } from "@tanstack/react-db" | ||
|
||
function PostList() { | ||
const { data, pages, fetchNextPage, hasNextPage, isLoading } = | ||
useLiveInfiniteQuery( | ||
(q) => | ||
q | ||
.from({ posts: postsCollection }) | ||
.orderBy(({ posts }) => posts.createdAt, "desc"), | ||
{ | ||
pageSize: 20, | ||
getNextPageParam: (lastPage, allPages) => | ||
lastPage.length === 20 ? allPages.length : undefined, | ||
} | ||
) | ||
|
||
if (isLoading) return <div>Loading...</div> | ||
|
||
return ( | ||
<div> | ||
{pages.map((page, i) => ( | ||
<div key={i}> | ||
{page.map((post) => ( | ||
<PostCard key={post.id} post={post} /> | ||
))} | ||
</div> | ||
))} | ||
{hasNextPage && ( | ||
<button onClick={() => fetchNextPage()}>Load More</button> | ||
)} | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
**Requirements:** | ||
|
||
- Query must include `.orderBy()` for the window mechanism to work | ||
- Returns flattened `data` array and `pages` array for flexible rendering | ||
- Automatically detects new pages when data is synced to the collection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would rather keep this method such that we can keep the
_sync
property privateThere 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.
I disagree,
syncMore
is an internal method that shouldering be presented on the front he public api. The managers having the underscore marks them as "internal implementation but exposed for debugging and internal communication". I would prefer to not presentsyncMore
on the prompts when people are interacting with a collection.