Skip to content

Commit e59691a

Browse files
KyleAMathewsclaude
andcommitted
refactor: simplify restoration transaction cleanup
- Consolidate restorationTransactions.delete() to single point - Improve comments on restoration transaction purpose Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent ecb4013 commit e59691a

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

packages/offline-transactions/src/OfflineExecutor.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -486,35 +486,37 @@ export class OfflineExecutor {
486486
)
487487
}
488488

489-
// Clean up restoration transaction when offline transaction completes or fails
490489
private cleanupRestorationTransaction(
491490
transactionId: string,
492-
shouldRollback: boolean = false,
491+
shouldRollback = false,
493492
): void {
494493
const restorationTx = this.restorationTransactions.get(transactionId)
495-
if (!restorationTx) return
494+
if (!restorationTx) {
495+
return
496+
}
497+
498+
this.restorationTransactions.delete(transactionId)
496499

497500
if (shouldRollback) {
498-
// Rollback the restoration transaction to remove optimistic state
499501
restorationTx.rollback()
500-
} else {
501-
// Mark as completed so recomputeOptimisticState removes it from consideration
502-
// The actual data will come from the sync
503-
restorationTx.setState(`completed`)
504-
505-
// Manually remove from each collection's transaction map and recompute
506-
const touchedCollections = new Set<string>()
507-
for (const mutation of restorationTx.mutations) {
508-
const collectionId = mutation.collection.id
509-
if (!touchedCollections.has(collectionId)) {
510-
touchedCollections.add(collectionId)
511-
mutation.collection._state.transactions.delete(restorationTx.id)
512-
mutation.collection._state.recomputeOptimisticState(false)
513-
}
514-
}
502+
return
515503
}
516504

517-
this.restorationTransactions.delete(transactionId)
505+
// Mark as completed so recomputeOptimisticState removes it from consideration.
506+
// The actual data will come from the sync.
507+
restorationTx.setState(`completed`)
508+
509+
// Remove from each collection's transaction map and recompute
510+
const touchedCollections = new Set<string>()
511+
for (const mutation of restorationTx.mutations) {
512+
const collectionId = mutation.collection.id
513+
if (touchedCollections.has(collectionId)) {
514+
continue
515+
}
516+
touchedCollections.add(collectionId)
517+
mutation.collection._state.transactions.delete(restorationTx.id)
518+
mutation.collection._state.recomputeOptimisticState(false)
519+
}
518520
}
519521

520522
async removeFromOutbox(id: string): Promise<void> {

packages/offline-transactions/src/executor/TransactionExecutor.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,50 +248,44 @@ export class TransactionExecutor {
248248
}
249249

250250
/**
251-
* Restore optimistic state from loaded transactions
251+
* Restore optimistic state from loaded transactions.
252252
* Creates internal transactions to hold the mutations so the collection's
253-
* state manager can show optimistic data while waiting for sync
253+
* state manager can show optimistic data while waiting for sync.
254254
*/
255255
private restoreOptimisticState(
256256
transactions: Array<OfflineTransaction>,
257257
): void {
258258
for (const offlineTx of transactions) {
259-
if (offlineTx.mutations.length === 0) continue
259+
if (offlineTx.mutations.length === 0) {
260+
continue
261+
}
260262

261-
// Create a restoration transaction that will never commit
262-
// Its only purpose is to hold the mutations for optimistic state display
263+
// Create a restoration transaction that holds mutations for optimistic state display.
264+
// It will never commit - the real mutation is handled by the offline executor.
263265
const restorationTx = createTransaction({
264266
id: offlineTx.id,
265267
autoCommit: false,
266-
mutationFn: async () => {
267-
// This mutation function should never be called
268-
// The real mutation is handled by the offline executor
269-
},
268+
mutationFn: async () => {},
270269
})
271270

272-
// Apply mutations to the restoration transaction
273271
restorationTx.applyMutations(offlineTx.mutations)
274272

275273
// Register with each affected collection's state manager
276274
const touchedCollections = new Set<string>()
277275
for (const mutation of offlineTx.mutations) {
278276
const collectionId = mutation.collection.id
279-
if (!touchedCollections.has(collectionId)) {
280-
touchedCollections.add(collectionId)
281-
282-
// Register the transaction with the collection's state manager
283-
mutation.collection._state.transactions.set(
284-
restorationTx.id,
285-
restorationTx,
286-
)
287-
288-
// Recompute optimistic state to show the restored data
289-
mutation.collection._state.recomputeOptimisticState(true)
277+
if (touchedCollections.has(collectionId)) {
278+
continue
290279
}
280+
touchedCollections.add(collectionId)
281+
282+
mutation.collection._state.transactions.set(
283+
restorationTx.id,
284+
restorationTx,
285+
)
286+
mutation.collection._state.recomputeOptimisticState(true)
291287
}
292288

293-
// Store the restoration transaction so we can clean it up when the
294-
// offline transaction completes
295289
this.offlineExecutor.registerRestorationTransaction(
296290
offlineTx.id,
297291
restorationTx,

0 commit comments

Comments
 (0)