Skip to content

docs: clarify that observable() mutates input by reference#652

Closed
sgup wants to merge 1 commit into
LegendApp:mainfrom
sgup:docs/observable-mutates-input
Closed

docs: clarify that observable() mutates input by reference#652
sgup wants to merge 1 commit into
LegendApp:mainfrom
sgup:docs/observable-mutates-input

Conversation

@sgup
Copy link
Copy Markdown

@sgup sgup commented Apr 27, 2026

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 initialState constant is reused as a "reset target" — the constant becomes structurally equal to current state, so set() 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.

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>
@sgup
Copy link
Copy Markdown
Author

sgup commented Apr 27, 2026

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.

@sgup sgup closed this Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant