docs: clarify that observable() mutates input by reference#652
Closed
sgup wants to merge 1 commit into
Closed
Conversation
observable() stores plain object/array inputs by reference and mutates them in place as fields are updated. This surprises people coming from Zustand/Redux/MobX and produces silent footguns when a shared `initialState` constant is reused as a "reset target" — the constant becomes structurally equal to current state, so set() is a no-op. Add JSDoc to observable() with a minimal repro and the recommended factory-function pattern. Refs LegendApp#647 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Author
|
Closing in favor of legendapp/legend-docs#59, which adds the same guidance to the docs site (the canonical source). Happy to revive this as a JSDoc on top of the docs change if you'd find that useful. |
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.
Adds a JSDoc note on
observable()explaining that plain object/array inputs are stored by reference and mutated in place as fields are updated.This is intentional (avoids cloning on every update) but surprises people coming from Zustand/Redux/MobX, and produces silent footguns when a shared
initialStateconstant is reused as a "reset target" — the constant becomes structurally equal to current state, soset()ends up a no-op via the deep-equals check.The JSDoc includes a minimal repro and the recommended factory pattern.
Refs #647 — when I dug into that issue, the original sync-bug framing turned out to be wrong; the real cause is this mutation behavior. I then audited my own codebase and found 8 stores with silently-broken reset functions from the same pattern.
No code/behavior changes — JSDoc only.
Disclosure: written together with Claude (Anthropic). Verified locally.