Skip to content

Commit 3b32e48

Browse files
ci: apply automated fixes
1 parent 57a7d0d commit 3b32e48

3 files changed

Lines changed: 61 additions & 16 deletions

File tree

.changeset/fix-optimistic-insert-cleanup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"@tanstack/db": patch
2+
'@tanstack/db': patch
33
---
44

55
fix: clean up optimistic state when server returns a different key than the optimistic insert

packages/db/src/collection/state.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,10 @@ export class CollectionStateManager<
492492
isDirectTransaction &&
493493
!this.processedCompletedTransactions.has(transaction.id) &&
494494
!this.directTransactionsWithSyncWrites.has(transaction.id)
495-
if (isDirectTransaction && !this.processedCompletedTransactions.has(transaction.id)) {
495+
if (
496+
isDirectTransaction &&
497+
!this.processedCompletedTransactions.has(transaction.id)
498+
) {
496499
this.processedCompletedTransactions.add(transaction.id)
497500
}
498501

packages/query-db-collection/tests/query.test.ts

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6385,7 +6385,11 @@ describe(`QueryCollection`, () => {
63856385
await vi.waitFor(() => expect(collection.status).toBe(`ready`))
63866386

63876387
const clientId = -999
6388-
const tx = collection.insert({ id: clientId, text: `Buy milk`, completed: false })
6388+
const tx = collection.insert({
6389+
id: clientId,
6390+
text: `Buy milk`,
6391+
completed: false,
6392+
})
63896393
expect(collection.has(clientId)).toBe(true)
63906394

63916395
await tx.isPersisted.promise
@@ -6395,7 +6399,9 @@ describe(`QueryCollection`, () => {
63956399

63966400
expect(collection._state.syncedData.has(100)).toBe(true)
63976401
expect(collection.has(clientId)).toBe(false)
6398-
expect(collection._state.pendingOptimisticDirectUpserts.has(clientId)).toBe(false)
6402+
expect(collection._state.pendingOptimisticDirectUpserts.has(clientId)).toBe(
6403+
false,
6404+
)
63996405
expect(collection.size).toBe(1)
64006406

64016407
await liveQuery.cleanup()
@@ -6406,7 +6412,12 @@ describe(`QueryCollection`, () => {
64066412
// Client and server use the same ID, but writeInsert adds
64076413
// server-computed fields. After the handler completes the item
64086414
// should be $synced: true and the server data should be visible.
6409-
type Todo = { id: number; text: string; completed: boolean; createdAt?: string }
6415+
type Todo = {
6416+
id: number
6417+
text: string
6418+
completed: boolean
6419+
createdAt?: string
6420+
}
64106421

64116422
const serverTodos: Array<Todo> = []
64126423
const queryFn = vi.fn().mockImplementation(() => [...serverTodos])
@@ -6428,7 +6439,10 @@ describe(`QueryCollection`, () => {
64286439
onInsert: async ({ transaction }) => {
64296440
const items = transaction.mutations.map((m) => m.modified)
64306441
await new Promise((r) => setTimeout(r, 10))
6431-
const saved = items.map((t) => ({ ...t, createdAt: `2024-01-01T00:00:00Z` }))
6442+
const saved = items.map((t) => ({
6443+
...t,
6444+
createdAt: `2024-01-01T00:00:00Z`,
6445+
}))
64326446
serverTodos.push(...saved)
64336447
collection.utils.writeInsert(saved)
64346448
return { refetch: false }
@@ -6451,7 +6465,9 @@ describe(`QueryCollection`, () => {
64516465
await flushPromises()
64526466

64536467
expect(collection._state.syncedData.has(1)).toBe(true)
6454-
expect(collection._state.syncedData.get(1)?.createdAt).toBe(`2024-01-01T00:00:00Z`)
6468+
expect(collection._state.syncedData.get(1)?.createdAt).toBe(
6469+
`2024-01-01T00:00:00Z`,
6470+
)
64556471
expect(collection._state.optimisticUpserts.has(1)).toBe(false)
64566472
expect(collection._state.pendingOptimisticDirectUpserts.has(1)).toBe(false)
64576473
expect(collection.size).toBe(1)
@@ -6501,7 +6517,11 @@ describe(`QueryCollection`, () => {
65016517
await vi.waitFor(() => expect(collection.status).toBe(`ready`))
65026518

65036519
const clientId = -777
6504-
const tx = collection.insert({ id: clientId, text: `Walk dog`, completed: false })
6520+
const tx = collection.insert({
6521+
id: clientId,
6522+
text: `Walk dog`,
6523+
completed: false,
6524+
})
65056525
expect(collection.has(clientId)).toBe(true)
65066526

65076527
await tx.isPersisted.promise
@@ -6511,7 +6531,9 @@ describe(`QueryCollection`, () => {
65116531

65126532
expect(collection._state.syncedData.has(500)).toBe(true)
65136533
expect(collection.has(clientId)).toBe(false)
6514-
expect(collection._state.pendingOptimisticDirectUpserts.has(clientId)).toBe(false)
6534+
expect(collection._state.pendingOptimisticDirectUpserts.has(clientId)).toBe(
6535+
false,
6536+
)
65156537
expect(collection.size).toBe(1)
65166538

65176539
await liveQuery.cleanup()
@@ -6521,9 +6543,16 @@ describe(`QueryCollection`, () => {
65216543
it(`should clean up optimistic state when writeUpdate is called in onUpdate handler`, async () => {
65226544
// When an onUpdate handler calls writeUpdate to sync the server response,
65236545
// the optimistic update should be removed and $synced should become true.
6524-
type Todo = { id: number; text: string; completed: boolean; updatedAt?: string }
6546+
type Todo = {
6547+
id: number
6548+
text: string
6549+
completed: boolean
6550+
updatedAt?: string
6551+
}
65256552

6526-
const serverTodos: Array<Todo> = [{ id: 1, text: `Buy milk`, completed: false }]
6553+
const serverTodos: Array<Todo> = [
6554+
{ id: 1, text: `Buy milk`, completed: false },
6555+
]
65276556
const queryFn = vi.fn().mockImplementation(() => [...serverTodos])
65286557

65296558
const testQueryClient = new QueryClient({
@@ -6543,7 +6572,10 @@ describe(`QueryCollection`, () => {
65436572
onUpdate: async ({ transaction }) => {
65446573
const items = transaction.mutations.map((m) => m.modified)
65456574
await new Promise((r) => setTimeout(r, 10))
6546-
const saved = items.map((t) => ({ ...t, updatedAt: `2024-06-01T00:00:00Z` }))
6575+
const saved = items.map((t) => ({
6576+
...t,
6577+
updatedAt: `2024-06-01T00:00:00Z`,
6578+
}))
65476579
for (const s of saved) {
65486580
const idx = serverTodos.findIndex((t) => t.id === s.id)
65496581
if (idx >= 0) serverTodos[idx] = s as Todo
@@ -6561,14 +6593,18 @@ describe(`QueryCollection`, () => {
65616593
await vi.waitFor(() => expect(collection.status).toBe(`ready`))
65626594
expect(collection._state.syncedData.has(1)).toBe(true)
65636595

6564-
const tx = collection.update(1, (draft) => { draft.completed = true })
6596+
const tx = collection.update(1, (draft) => {
6597+
draft.completed = true
6598+
})
65656599

65666600
await tx.isPersisted.promise
65676601
await flushPromises()
65686602
await new Promise((r) => setTimeout(r, 200))
65696603
await flushPromises()
65706604

6571-
expect(collection._state.syncedData.get(1)?.updatedAt).toBe(`2024-06-01T00:00:00Z`)
6605+
expect(collection._state.syncedData.get(1)?.updatedAt).toBe(
6606+
`2024-06-01T00:00:00Z`,
6607+
)
65726608
expect(collection._state.optimisticUpserts.has(1)).toBe(false)
65736609
expect(collection._state.pendingOptimisticDirectUpserts.has(1)).toBe(false)
65746610
expect(collection.size).toBe(1)
@@ -6620,7 +6656,11 @@ describe(`QueryCollection`, () => {
66206656
await vi.waitFor(() => expect(collection.status).toBe(`ready`))
66216657

66226658
const clientId = -888
6623-
const tx = collection.insert({ id: clientId, text: `Test batch`, completed: false })
6659+
const tx = collection.insert({
6660+
id: clientId,
6661+
text: `Test batch`,
6662+
completed: false,
6663+
})
66246664
expect(collection.has(clientId)).toBe(true)
66256665

66266666
await tx.isPersisted.promise
@@ -6630,7 +6670,9 @@ describe(`QueryCollection`, () => {
66306670

66316671
expect(collection._state.syncedData.has(200)).toBe(true)
66326672
expect(collection.has(clientId)).toBe(false)
6633-
expect(collection._state.pendingOptimisticDirectUpserts.has(clientId)).toBe(false)
6673+
expect(collection._state.pendingOptimisticDirectUpserts.has(clientId)).toBe(
6674+
false,
6675+
)
66346676
expect(collection.size).toBe(1)
66356677

66366678
await liveQuery.cleanup()

0 commit comments

Comments
 (0)