Skip to content
Source
Sign inSign up
TanStack/db

fix(db): clean up optimistic state when server returns a different key #1465

Closedgoatrenterguy(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1465/headopened

Showing 3 changed files with 120 additions and 7 deletions (loading 1).

x
0 of 3 viewed, 3 left
7 additions and 0 deletions.changeset/fix-optimistic-insert-cleanup.mdAdded
View file
@@ -0,0 +1,7 @@
+---
+'@tanstack/db': patch
+---
+​
+fix: clean up optimistic state when server returns a different key than the optimistic insert
+​
+When an `onInsert`/`onUpdate`/`onDelete` handler syncs server data back to the collection (via `writeInsert`, `writeUpdate`, `writeUpsert`, `writeDelete`, or `refetch`), the optimistic state under the original client key is now correctly removed if the server returns a different key. Previously, the client-key item persisted forever alongside the server-key item, causing duplication and stale `$synced: false` state.
113 additions and 7 deletionspackages/db/src/collection/state.tsModified
View file
@@ -83,6 +83,8 @@
public pendingOptimisticDeletes = new Set<TKey>()
public pendingOptimisticDirectUpserts = new Set<TKey>()
public pendingOptimisticDirectDeletes = new Set<TKey>()
+ public directTransactionsWithSyncWrites = new Set<string>()
+ public processedCompletedTransactions = new Set<string>()
​
/**
* Tracks the origin of confirmed changes for each row.
@@ -483,6 +485,20 @@
const isDirectTransaction =
transaction.metadata[DIRECT_TRANSACTION_METADATA_KEY] === true
if (transaction.state === `completed`) {