Skip to content

Commit c8c3df8

Browse files
Fix: Skip all optimistic state from persisting transactions, not just synced keys
The previous fix only skipped re-applying optimistic state for keys that were just synced. This failed when the key itself changed (e.g., temp ID -1234 → server ID 1000), because changedKeys.has(mutation.key) would return false. Now we skip re-applying optimistic state for ALL mutations in persisting transactions. This ensures that when a mutation handler writes server data via sync functions, that data becomes visible immediately without being masked by stale optimistic state. This fixes the issue where temp IDs would persist even after the server returned real IDs. Co-authored-by: Sam Willis <[email protected]>
1 parent f4826f0 commit c8c3df8

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

packages/db/src/collection/state.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -669,22 +669,20 @@ export class CollectionStateManager<
669669
// after the truncate snapshot are preserved.
670670
for (const transaction of this.transactions.values()) {
671671
if (![`completed`, `failed`].includes(transaction.state)) {
672+
// Skip re-applying optimistic state for persisting transactions entirely
673+
// (not just for changed keys). When a transaction is persisting, its mutation
674+
// handler may write server data via sync functions. That synced data should be
675+
// visible immediately, not masked by stale optimistic state.
676+
// EXCEPT during truncate operations where optimistic state should always be preserved.
677+
if (!hasTruncateSync && transaction.state === `persisting`) {
678+
continue
679+
}
680+
672681
for (const mutation of transaction.mutations) {
673682
if (
674683
this.isThisCollection(mutation.collection) &&
675684
mutation.optimistic
676685
) {
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-
688686
switch (mutation.type) {
689687
case `insert`:
690688
case `update`:

0 commit comments

Comments
 (0)