Skip to content

Commit 051398c

Browse files
committed
Fix optimistic state not being replaced by server data in writeInsert
Fixes #814 When writeInsert() is called from within an onInsert handler, the transaction is in 'persisting' state. Previously, commitPendingTransactions() would re-apply optimistic state from persisting transactions after syncing server data, causing server-generated fields to be overwritten with optimistic client-side values. This fix excludes 'persisting' transactions from having their optimistic state re-applied during commitPendingTransactions(). This ensures that when writeInsert() syncs server data (e.g., server-generated IDs), that data is not immediately overwritten by the optimistic state. The flow now works correctly: 1. User inserts item with temporary ID -> optimistic state shows temp ID 2. Transaction commits -> onInsert handler called (state = 'persisting') 3. Handler calls writeInsert(serverItem) with real ID 4. commitPendingTransactions() syncs server data 5. Optimistic state from persisting transaction is NOT re-applied 6. UI now shows server data with real ID 7. Transaction completes -> optimistic state is cleared
1 parent 5fb76fe commit 051398c

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

packages/db/src/collection/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ export class CollectionStateManager<
668668
// Always overlay any still-active optimistic transactions so mutations that started
669669
// after the truncate snapshot are preserved.
670670
for (const transaction of this.transactions.values()) {
671-
if (![`completed`, `failed`].includes(transaction.state)) {
671+
if (![`completed`, `failed`, `persisting`].includes(transaction.state)) {
672672
for (const mutation of transaction.mutations) {
673673
if (
674674
this.isThisCollection(mutation.collection) &&

0 commit comments

Comments
 (0)