Skip to content
Source
Sign inSign up
TanStack/db

exclude 'persisting' transactions from having their optimistic state re-applied during commitPendingTransactions() #817

ClosedKyleAMathews(GitHub profile, opens in a new tab)wants to merge 5 commits intomain fromclaude/investigate-issue-814-01PHGETuk6EWkSx1SgSbt6ygopened

Showing 3 changed files with 48 additions and 0 deletions (loading 1).

x
0 of 3 viewed, 3 left
39 additions and 0 deletions.changeset/fix-writeinsert-optimistic-state.mdAdded
View file
@@ -0,0 +1,39 @@
+---
+"@tanstack/db": patch
+---
+​
+Fix optimistic state not being replaced by server data when using writeInsert in mutation handlers
+​
+Previously, when using `writeInsert()` inside an `onInsert` handler to sync server-generated data back to the collection, the optimistic client-side data would not be replaced by the actual server data. This meant that temporary client-side values (like negative IDs) would persist even after the server returned the real values.
+​
+**Example of the issue:**
+​
+```ts
+const todosCollection = createCollection(
+ queryCollectionOptions({
+ // ...
+ onInsert: async ({ transaction }) => {
+ const newItems = transaction.mutations.map((m) => m.modified)
+ const serverItems = await createTodos(newItems)
+​
+ todosCollection.utils.writeBatch(() => {
+ serverItems.forEach((serverItem) => {
+ todosCollection.utils.writeInsert(serverItem)
+ })
+ })
+​
+ return { refetch: false }
+ },
+ })