Skip to content

fix: keep snapshot identity when a node is instantiated lazily - #2310

Open
lavrton wants to merge 2 commits into
mobxjs:masterfrom
lavrton:fix/snapshot-identity-after-instantiation
Open

lavrton wants to merge 2 commits into
mobxjs:masterfrom
lavrton:fix/snapshot-identity-after-instantiation

Conversation

@lavrton

@lavrton lavrton commented Oct 21, 2026 •

Copy link
Copy Markdown

What does this PR do and why?

Keeps a node's snapshot object the same across lazy instantiation.

Before a node is read, its snapshot computed returns the cached initial snapshot, and parents embed that object in their own snapshots. createObservableInstance then calls trackAndCompute(), which replaces it with a fresh, structurally identical object without notifying the parents. From then on a snapshot taken before the read never matches by identity, so applySnapshot re-applies every child of it (ArrayType.applySnapshot, ModelType.applySnapshot and areSame skip a child only when given the very object it holds). An undo manager that records getSnapshot(root) after a load pays O(all children) every time it undoes to that state: ~560 ms for 10,000 elements in Polotno, ~1 ms after this change.

The computed now treats the recompute during INITIALIZING as unchanged, so MobX keeps the previous object. Instantiation changes no data; afterCreate runs after that window and propagates as before.

Steps to validate locally

  • bun test __tests__/core/snapshot-identity.test.ts — both tests fail on master.
  • NODE_ENV=development bun test and MST_TESTING=1 NODE_ENV=production bun test are green.

@lavrton lavrton closed this Oct 21, 2026
@lavrton lavrton reopened this Oct 21, 2026
@lavrton
lavrton marked this pull request as ready for review October 21, 2026 18:56

This branch has not been deployed

No deployments
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