Repository navigation
fix: keep outer drafts out of current() in nested producers - #1405
Open
YouKnowChatta wants to merge 1 commit into
Open
YouKnowChatta wants to merge 1 commit into
YouKnowChatta wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
current()inside a nested producer can return a snapshot that still contains drafts of the outer producer. Before 11.2.14 this happened for children the inner recipe had read; since 11.2.14 it also happens for children it never touched. The outer producer revokes those drafts when it finishes, and reading the snapshot then throws:If the inner recipe reads
user.tags.xbefore it callscurrent(), the sameJSON.stringifycall throws on 10.0.3, 10.1.1, 11.0.0, 11.1.0 and 11.2.13 as well.The nested producer is the pattern that the "Always use the result of nested producers" section of the pitfalls page recommends. The
currentpage says its result is not a Proxy and can be safely leaked from a produce function.#1394 made
current()reuse the children that a draft still shares with its base, because those children hold no drafts. Inside a nested producer that does not always hold. When the base there is a draft of the outer producer, or a plain object that holds one, the reused children are outer drafts. Array and Map drafts behave the same, and so does a producer whose base is a draft fromcreateDraft.The older failure has a separate cause. For an unmodified draft,
current()returnsstate.base_. A child that the inner recipe reads becomes an inner draft whose base is the outer draft of that child, so the snapshot gets the outer draft.With this change, an unmodified draft whose base is a draft returns the snapshot of that base.
current()reuses base children only when the scope has no parent and the base is not a draft. Finalize uses the samescope.parent_check to skip freezing in nested producers. In the other casescurrent()copies the children, as it did in 11.2.13. The change also fixes three nested producers and an outer draft kept in a Set of a nested producer, which fail on 11.2.13 too.Top-level producers on plain data keep the #1394 behavior. On a top-level draft that holds 10,000 untouched objects, 50
current()calls take the same time with this change as on main, within run-to-run noise. Inside a nested producer they are faster than on main.This change does not fix a top-level
produceon plain data that holds a live draft fromcreateDraftor from anotherImmerinstance. 11.2.13 handles that case, and 11.2.14 and this change leave the draft in the snapshot.Six tests in
__tests__/current.jscover object, array and Map drafts passed to a nested producer, with and without the inner recipe reading a child first. They also cover three nested producers, a nested producer whose plain base holds an outer draft in an object or in a Set, and a producer on acreateDraftdraft. All six fail on main (b80ac34) and pass with the fix.Validation (Node 24.23.0, Yarn 1.22.22, Linux):
yarn test: 4,295 source tests and 3,744 production-build tests passed, with 8 existing skips in each suite. Build and Flow passed.yarn coverage,yarn test:perf, Prettier on both changed files, andgit diff --checkpassed.