Commit e766740
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 all persisting transactions after syncing
server data, causing server-generated fields to be overwritten with
optimistic client-side values.
This fix prevents re-applying optimistic state from persisting transactions
for keys that were just synced, but only for non-truncate operations. Truncate
operations still preserve optimistic state as expected.
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 for that key
5. Optimistic state from persisting transaction is NOT re-applied for that key
6. UI now shows server data with real ID
7. Transaction completes -> optimistic state is cleared
For truncate operations, optimistic state is always preserved to ensure
transactions started after the truncate snapshot are maintained.1 parent 5fb76fe commit e766740
File tree
2 files changed
+50
-0
lines changed- .changeset
- packages/db/src/collection
2 files changed
+50
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
674 | 674 | | |
675 | 675 | | |
676 | 676 | | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
677 | 688 | | |
678 | 689 | | |
679 | 690 | | |
| |||
0 commit comments