Add synced state introspection APIs for conflict detection #1245
+549
−10
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.
🎯 Changes
This PR adds two new public APIs to the
CollectionImplclass to enable introspection of synced (server-side) state, independent of optimistic updates:getSyncedValue(key)- Returns the authoritative server-side value for a given key, ignoring any pending optimistic mutations. This allows applications to detect server-side changes and implement conflict resolution strategies.getSyncedMetadata(key)- Returns sync metadata (e.g., revision numbers, ETags) associated with a synced entity, useful for version-based conflict detection.These APIs enable robust conflict detection in paced mutations by allowing the
mutationFnto compare the original mutation state against the current server state before persisting changes. The PR includes comprehensive test coverage demonstrating:Additionally, the paced mutations implementation is hardened to gracefully handle external transaction rollbacks (e.g., from cascade conflicts or manual rollback). When a debounce fires after a transaction has been externally cancelled, the strategy callback now safely returns early instead of throwing an error, and the next
mutate()call creates a fresh transaction.✅ Checklist
pnpm test:pr.🚀 Release Impact
https://claude.ai/code/session_01FPgcCTxP2SC8NTeWx9xhJV