Skip to content
Source
Sign inSign up
TanStack/db

[fix] synced data not appearing in derived collection during pending optimistic mutation (issue #1017) #1167

Closedclaude[bot](GitHub profile, opens in a new tab)wants to merge 1 commit intoai/issue-1017-repro fromai/issue-1017-fixopened

Showing 1 changed file with 62 additions and 0 deletions.

x
0 of 1 viewed, 1 left
62 additions and 0 deletionspackages/db/src/collection/state.tsModified
View file
@@ -825,6 +825,68 @@
if (!this.hasReceivedFirstCommit) {
this.hasReceivedFirstCommit = true
+ } else if (committedSyncedTransactions.length > 0) {
+ // When there's a persisting transaction, we can't update syncedData yet, but we
+ // should still notify subscribers about the new synced data. This ensures that
+ // derived collections (live queries) see the synced data immediately.
+ //
+ // We emit events for the committed sync operations without modifying syncedData.
+ // The syncedData will be updated when the persisting transaction completes.
+ const events: Array<ChangeMessage<TOutput, TKey>> = []
+​
+ for (const transaction of committedSyncedTransactions) {
+ for (const operation of transaction.operations) {
+ const key = operation.key as TKey
+ // Only emit events for keys that aren't affected by the pending optimistic mutation
+ // This ensures we don't double-emit or conflict with optimistic state
+ if (
+ !this.optimisticUpserts.has(key) &&
+ !this.optimisticDeletes.has(key)
+ ) {
+ // For inserts, check if the key already exists in syncedData to determine event type
+ if (operation.type === `insert`) {
+ if (this.syncedData.has(key)) {
+ // Key exists, this is effectively an update