Skip to content

Commit b954d4e

Browse files
committed
Run prettier
1 parent afc182e commit b954d4e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

.changeset/fix-writeinsert-optimistic-state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const todosCollection = createCollection(
3030
// User inserts with temporary ID
3131
todosCollection.insert({
3232
id: -1234, // Temporary negative ID
33-
title: "Task"
33+
title: "Task",
3434
})
3535

3636
// Server returns real ID, but UI would still show -1234 instead of the real ID

packages/db/src/collection/state.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,23 @@ 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`, `persisting`].includes(transaction.state)) {
671+
if (![`completed`, `failed`].includes(transaction.state)) {
672672
for (const mutation of transaction.mutations) {
673673
if (
674674
this.isThisCollection(mutation.collection) &&
675675
mutation.optimistic
676676
) {
677+
// Skip re-applying optimistic state for persisting transactions
678+
// if the mutation key was just synced (prevents overwriting fresh server data)
679+
// EXCEPT during truncate operations where optimistic state should always be preserved
680+
if (
681+
!hasTruncateSync &&
682+
transaction.state === `persisting` &&
683+
changedKeys.has(mutation.key)
684+
) {
685+
continue
686+
}
687+
677688
switch (mutation.type) {
678689
case `insert`:
679690
case `update`:

0 commit comments

Comments
 (0)