-
Notifications
You must be signed in to change notification settings - Fork 102
preserve optimistic mutations during truncate operations #659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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: |
Size Change: +59 B (+0.08%) Total Size: 76.5 kB
ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 1.47 kB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
commitPendingTransactions
logic was rebuilding optimistic state from "active" transactions onlypending
→completed
during 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.ts
optimisticSnapshot
field toPendingSyncedTransaction
interfacepackages/db/src/collection/sync.ts
truncate()
is calledTests
packages/db/tests/collection-truncate.test.ts
(new file)packages/db/tests/collection-subscribe-changes.test.ts
Performance 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.