preserve optimistic mutations during truncate operations#659
Merged
Conversation
🦋 Changeset detectedLatest commit: 01977a7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Contributor
|
Size Change: +59 B (+0.08%) Total Size: 76.5 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 1.47 kB ℹ️ View Unchanged
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
issue found by @balegas
Problem
When a collection receives a truncate operation (typically triggered by a "must-refetch" signal from the server), optimistic mutations could be permanently lost if their async handlers (
onInsert,onUpdate,onDelete) completed during the truncate processing.Root Causes Identified
Bug #1: Optimistic Data Loss During Truncate
commitPendingTransactionslogic was rebuilding optimistic state from "active" transactions onlypending→completedduring truncate processing, it was skipped during re-applicationBug #2: Missing Delete Events for Optimistic-Only Data
syncedData.keys()to emit delete eventsoptimisticUpserts(not yet synced) didn't get delete eventsSolution
Snapshot-Based Approach
The fix implements a snapshot mechanism that captures optimistic state at the exact moment
truncate()is called:Capture snapshot when truncate is called (
packages/db/src/collection/sync.ts)Use snapshot for delete events (
packages/db/src/collection/state.ts)Restore snapshot, then overlay active transactions
Why This Works
Timing Independence:
truncate()call time, before any transactions can completeClient Intent Preservation:
Tests Added
Added 11 comprehensive tests in
packages/db/tests/collection-truncate.test.ts:Core Scenarios
Edge Cases
Test Results
Files Changed
Core Logic
packages/db/src/collection/state.tsoptimisticSnapshotfield toPendingSyncedTransactioninterfacepackages/db/src/collection/sync.tstruncate()is calledTests
packages/db/tests/collection-truncate.test.ts(new file)packages/db/tests/collection-subscribe-changes.test.tsPerformance Impact
Minimal overhead:
Breaking Changes
None. This is a bug fix that corrects existing behavior.
Migration Guide
No migration needed. The fix is transparent to existing code.