Skip to content
Source
Sign inSign up
TanStack/db

fix(db): publish one event per key for metadata-only sync commits #1795

Closedelylucas(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/1795/headopened

Showing 4 changed files with 129 additions and 23 deletions (loading 1).

x
0 of 4 viewed, 4 left
5 additions and 0 deletions.changeset/fix-metadata-only-sync-duplicate-events.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/db': patch
+---
+​
+Capture pre-sync visible state for keys that a pending sync transaction only writes metadata for, so retiring an optimistic mutation publishes one change event per key instead of two. The duplicated event corrupted multiplicity bookkeeping in live queries, making a later optimistic write on the same key fail with "Query contributors with the same row key are not congruent".
32 additions and 23 deletionspackages/db/src/collection/state.tsModified
View file
@@ -825,6 +825,32 @@
​
/**
+ * Collects every key the given sync transactions can publish a change event
+ * for.
+ *
+ * Row metadata writes count even when the transaction carries no row
+ * operation: committing one still retires a completed optimistic mutation for
+ * that key, which flips virtual props such as `$synced`. Every site that asks
+ * "which keys does this sync commit touch" must agree on this set. When they
+ * disagree, `recomputeOptimisticState` and `commitPendingTransactions` each
+ * emit the same transition and the published batch names one key twice.
+ */
+ private collectAffectedKeys(
+ transactions: Iterable<PendingSyncedTransaction<TOutput, TKey>>,
+ ): Set<TKey> {