Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@xstate/solid] Fix context mutation with new values nested in arrays #5100

Conversation

GoldingAustin
Copy link
Contributor

@GoldingAustin GoldingAustin commented Oct 10, 2024

Fix new array index value not being cloned before insertion into store - Fixes #5099

Additions to test cases:

…tion into store - Fix statelyai#5099

Signed-off-by: Austin Golding <austin.golding.dev@icloud.com>
Copy link

changeset-bot bot commented Oct 10, 2024

🦋 Changeset detected

Latest commit: 9575c67

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@xstate/solid Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…tion into store - Fix statelyai#5099

Signed-off-by: Austin Golding <austin.golding.dev@icloud.com>
// Update new or now undefined indexes
if (newIndices !== 0) {
for (let newEnd = smallestSize; newEnd <= largestSize - 1; newEnd++) {
set(...path, newEnd, deepClone(next[newEnd]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought we might only have to deep clone everything once upfront - do we deep clone objects anywhere else while performing other types of updates right now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps my question is - wow is this handled when a new property is being added to an object?

Copy link
Contributor Author

@GoldingAustin GoldingAustin Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my initial testing, only inserting into an array seems to require a deep clone. With objects, we're able to diff down to primitives, but with a new index in an array, it doesn't update properly unless we insert the full value. Maybe there's a way we can modify array updates to work the same way objects are updated, but that'll take more research/testing. Moving nested objects around and into new properties works as expected.

I will dig deeper into this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Austin. I appreciate it ❤️

Copy link
Contributor Author

@GoldingAustin GoldingAustin Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After additional reading/testing, I found that we only needed to place an empty array/object in the new array index; the diff loop properly updates all changes after that. Objects automatically populate with the path provided to set, but arrays require the nested object/array to be set before properly updating the path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects automatically populate with the path provided to set, but arrays require the nested object/array to be set before properly updating the path

Why? What is the difference between arrays and objects in this regard? 🤔

…wrappable to prevent mutating original object/array without needing to deep clone

Signed-off-by: Austin Golding <austin.golding.dev@icloud.com>
@Andarist Andarist merged commit 519188a into statelyai:main Oct 17, 2024
1 check passed
@github-actions github-actions bot mentioned this pull request Oct 17, 2024
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.

Bug: useActor from @xstate/solid mutates context
2 participants