Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cleanup-discarded-rows-stay-cleared.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/db': patch
---

An explicit `cleanup()` now keeps the optimistic rows of a transaction that is still persisting cleared after the Collection restarts. Previously, a write made after the restart brought those rows back, so a row that cleanup had discarded reappeared. The transaction still holds its Collection until it settles.
2 changes: 1 addition & 1 deletion docs/contributing/oracle-coverage.md

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions packages/db/src/collection/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export class CollectionStateManager<

// Core state - make public for testing
public transactions: SortedMap<string, Transaction<any>>
// Transactions whose rows cleanup discarded. They keep their hold, but they
// never overlay this Collection again, even after a restart.
private discardedTransactions = new WeakSet<Transaction<any>>()
public pendingSyncedTransactions: Array<
PendingSyncedTransaction<TOutput, TKey>
> = []
Expand Down Expand Up @@ -709,8 +712,13 @@ export class CollectionStateManager<
continue
}
// Settlement can outlive cleanup. Keep pruning settled transactions,
// but do not republish a sibling from the discarded sync run.
if (this.lifecycle.hasStartedCleanup()) continue
// but do not republish a sibling from the discarded sync run, or any
// write that arrives while that cleanup is still running.
if (
this.lifecycle.isCleaningUp() ||
this.discardedTransactions.has(transaction)
)
continue
for (const mutation of transaction.mutations) {
if (mutation.collection !== this.collection) continue
this.pendingLocalChanges.add(mutation.key)
Expand Down Expand Up @@ -1647,6 +1655,8 @@ export class CollectionStateManager<
*/
public cleanup(): void {
this.syncRunGeneration++
for (const transaction of this.transactions.values())
this.discardedTransactions.add(transaction)
for (const transaction of this.pendingSyncedTransactions) {
transaction.applied.reject(new SyncTransactionAbortedError())
}
Expand Down
Loading
Loading