Skip to content

Conversation

@ibarakaiev
Copy link
Contributor

When a must-refetch (409) occurs in on-demand sync mode, the collection receives a truncate which clears all data and resets the loadSubset deduplication state. However, subscriptions were not re-requesting their previously loaded subsets, leaving the collection empty.

This fix adds a truncate event listener to CollectionSubscription that:

  1. Resets pagination/snapshot tracking state (but NOT sentKeys)
  2. Re-requests all previously loaded subsets

Intentionally keeping sentKeys intact because the truncate event is emitted BEFORE delete events are sent to subscribers. If we cleared sentKeys, delete events would be filtered by filterAndFlipChanges.

@changeset-bot
Copy link

changeset-bot bot commented Dec 16, 2025

⚠️ No Changeset found

Latest commit: 0d96676

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 16, 2025

More templates

@tanstack/angular-db

npm i https://pkg.pr.new/@tanstack/angular-db@1025

@tanstack/db

npm i https://pkg.pr.new/@tanstack/db@1025

@tanstack/db-ivm

npm i https://pkg.pr.new/@tanstack/db-ivm@1025

@tanstack/electric-db-collection

npm i https://pkg.pr.new/@tanstack/electric-db-collection@1025

@tanstack/offline-transactions

npm i https://pkg.pr.new/@tanstack/offline-transactions@1025

@tanstack/powersync-db-collection

npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1025

@tanstack/query-db-collection

npm i https://pkg.pr.new/@tanstack/query-db-collection@1025

@tanstack/react-db

npm i https://pkg.pr.new/@tanstack/react-db@1025

@tanstack/rxdb-db-collection

npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1025

@tanstack/solid-db

npm i https://pkg.pr.new/@tanstack/solid-db@1025

@tanstack/svelte-db

npm i https://pkg.pr.new/@tanstack/svelte-db@1025

@tanstack/trailbase-db-collection

npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1025

@tanstack/vue-db

npm i https://pkg.pr.new/@tanstack/vue-db@1025

commit: 0d96676

@ibarakaiev
Copy link
Contributor Author

Note: the current implementation still causes a flicker, i.e. when 409 is received, data is emptied then once fetching completes it is repopulated, which is noticeable in the UI. Not sure what's the best way around that.

When a must-refetch (409) occurs in on-demand sync mode, the collection
receives a truncate which clears all data and resets the loadSubset
deduplication state. However, subscriptions were not re-requesting
their previously loaded subsets, leaving the collection empty.

This fix adds a truncate event listener to CollectionSubscription that:
1. Resets pagination/snapshot tracking state (but NOT sentKeys)
2. Re-requests all previously loaded subsets

We intentionally keep sentKeys intact because the truncate event is
emitted BEFORE delete events are sent to subscribers. If we cleared
sentKeys, delete events would be filtered by filterAndFlipChanges.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@ibarakaiev ibarakaiev force-pushed the fix/resubscribe-after-truncate branch from eb081e5 to 0d96676 Compare December 17, 2025 21:28
@samwillis
Copy link
Collaborator

Hey @ibarakaiev, this was a really great start. I ended up forking it to add buffering on the live query subscriptions after a truncate such that there is no flash of no content at all. #1043 replaces this.

@samwillis samwillis closed this Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants