fix: preserve Firestore opaque values through the diff pipeline#15
Merged
Conversation
`applyDiffMutable` was substituting `serverTimestamp()` with `Timestamp.now()`, shipping client clock time to Firestore instead of the real server timestamp. The diff/clone pipeline also mishandled other opaque values (`DocumentReference`, `GeoPoint`, `Bytes`, `VectorValue`, `arrayUnion`/`arrayRemove`, `increment`), either walking their keys or stripping their prototypes. Introduce `isFirestoreOpaque` and treat every sentinel and value type as opaque across `computeDiff`, `applyDiffMutable`, `deepClone`, and `flattenDiff` — preserved by reference so writes reach Firestore in their original form. To keep optimistic UI working now that the sentinel survives in `localState`, document and collection subscriptions maintain a `displayOverrides` map of frozen `Timestamp.now()` values keyed by dotted path. `getMergedData` overlays the overrides so consumers see a renderable Timestamp while the write is in flight; entries are dropped when the sentinel leaves `localState` on sync ack or overwrite.
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.
Summary
applyDiffMutablewas substitutingserverTimestamp()withTimestamp.now()before the write reached Firestore — shipping client clock time instead of the real server timestamp. Every other Firestore opaque value (DocumentReference,GeoPoint,Bytes,VectorValue,arrayUnion/arrayRemove,increment,deleteField) was also at risk of being walked or stripped of its prototype by the diff/clone pipeline.isFirestoreOpaquepredicate and routescomputeDiff,applyDiffMutable,deepClone, andflattenDiffthrough it — opaque values are preserved by reference so they reach Firestore in their original form.displayOverridesmaps (document + collection) that capture a frozenTimestamp.now()for eachserverTimestamp()path inlocalState, so optimistic UI keeps rendering a real Timestamp while the write is in flight. Entries are reconciled on every notify and dropped when the sentinel leaveslocalState.Test plan
pnpm test— 132/132 passing (includes 51 new diff unit tests and 6 new integration tests pinning the C1 regression end-to-end)serverTimestamp()writes land with server time (not client time) and that the UI shows a renderable Timestamp during the in-flight windowDocumentReference,GeoPoint,Bytes,VectorValue,arrayUnion/arrayRemove,incrementround-trips