Repository navigation
[HSF-448] feat!: keep unchanged state identity across edits and snapshots - #51
Merged
Merged
Conversation
Every edit deep-cloned the whole collection, and every snapshot rebuilt every document, so one field change gave every document a new object. Reference-based memoization in consumers (React.memo, useMemo) could never skip work. - applyDiff copies only the objects on the changed path. - Snapshots reuse each document and nested object that equals the current view (replaceEqualDeep), including our own confirmed write. - The rebase, id injection, remote deletes, add, remove, and display overrides copy on write instead of mutating shared state. - Outside production, notify() deep-freezes syncState and localState, so an in-place mutation throws instead of corrupting the diff baseline. BREAKING CHANGE: state is immutable. Non-production builds throw a TypeError when code mutates data read from Firestate. applyDiff results share unchanged subtrees with their input. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
add() now deep-clones its data like set() does, and applyDiff copies arrays from a diff (reusing the current array when equal), so objects a caller passes to add() or update() are never frozen by notify(). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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
Every edit deep-cloned the whole collection, and every snapshot rebuilt every document. One field change gave every document a new object, so
React.memoanduseMemoin consumers could never skip work.applyDiffcopies only the objects on the changed path. Siblings and unchanged nested objects keep their identity.replaceEqualDeep). This covers the confirmation of our own write and server write-backs during a pending edit.add,remove, andserverTimestamp()display overrides copy on write.notify()deep-freezessyncStateandlocalState. An in-place mutation throws aTypeErrorinstead of silently corrupting the diff baseline.README.md,docs/, andAGENTS.mddescribe the immutability contract.0.2.0.Breaking change
State is immutable. Non-production builds throw a
TypeErrorwhen code mutates data read from Firestate.applyDiffresults share unchanged subtrees with their input.Tests
src/__tests__/structural-sharing.test.ts: edits, one-document snapshots, write confirmations, write-back during a pending edit, add and remove, undo, and the freeze.src/utils/diff.test.tsfor the diff helpers.Release
After merge, publish a GitHub release for
v0.2.0to trigger the npm publish workflow.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.