fix(react-db): refresh live query snapshot after subscribe#1516
fix(react-db): refresh live query snapshot after subscribe#1516lalitkapoor wants to merge 2 commits intoTanStack:mainfrom
Conversation
Force one post-subscribe snapshot refresh even when a collection is still loading so rows hydrated between render and subscription attachment are visible immediately. Add a regression test for the loading collection race and include a react-db patch changeset.
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Hi @lalitkapoor, Thank you for opening this PR. Here are the concerns: 1. Extra render on every subscribe (minor performance). 2. Unconditional bump even when nothing changed. 3. Test fragility — write-during-render.
4. Test coverage gap.
Both are likely fine given how minimal the change is, but a one-line follow-up assertion would lock this in. |
🎯 Changes
Fixes a
useLiveQuerysubscription timing bug where the client could fail to see rows that were already in the collection.The failure mode was:
useLiveQueryread a render-time snapshot while the collection was still empty.useSyncExternalStoresubscription. In our app, this is the shape of local/persisted rows hydrating while upstream sync is still pending.loading, the hook skipped its post-subscribe refresh.The fix is to force one snapshot refresh immediately after subscribing regardless of collection status. That makes rows written during the render-to-subscribe gap visible while
isLoadingcan still remain true for upstream sync.Adds a regression test with a real collection that writes a row through sync callbacks during that gap and verifies
useLiveQueryreturns the row while still loading.✅ Checklist
pnpm test.🚀 Release Impact