Skip to content
Source
Sign inSign up
TanStack/db

collection: option to sync while persisting. #1048

Closedthruflo(GitHub profile, opens in a new tab)wants to merge 4 commits intomain fromthruflo/sync-while-persistingopened

Showing 5 changed files with 119 additions and 7 deletions (loading 2).

x
0 of 5 viewed, 5 left
25 additions and 0 deletions.changeset/poor-worlds-glow.mdAdded
View file
@@ -0,0 +1,25 @@
+---
+'@tanstack/db': patch
+---
+​
+Added `onSyncWhilePersisting` callback to collection sync config for fine-grained control over whether synced data should be applied while optimistic transactions are persisting.
+​
+The callback receives context about pending sync operations and persisting transactions, allowing developers to make selective decisions:
+​
+```typescript
+sync: {
+ sync: ({ begin, write, commit }) => { /* ... */ },
+ // Allow sync only when there are no conflicting keys
+ onSyncWhilePersisting: ({ conflictingKeys }) => conflictingKeys.size === 0,
+}
+```
+​
+Context provided to the callback:
+​
+- `pendingSyncKeys` - Keys of items in pending sync operations
+- `persistingKeys` - Keys being modified by persisting optimistic transactions
+- `conflictingKeys` - Keys that appear in both (potential conflicts)
+- `persistingTransactionCount` - Number of persisting transactions
+- `isTruncate` - Whether this includes a truncate operation
+​
+If no callback is provided, sync is deferred while any optimistic transaction is persisting (the safe default). Truncate operations always proceed regardless of the callback.
47 additions and 7 deletionspackages/db/src/collection/state.tsModified
View file