File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed
packages/db/src/collection Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ const todosCollection = createCollection(
3030// User inserts with temporary ID
3131todosCollection .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
Original file line number Diff line number Diff 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` :
You can’t perform that action at this time.
0 commit comments