-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[@xstate/solid] Fix context mutation with new values nested in arrays #5100
Conversation
…tion into store - Fix statelyai#5099 Signed-off-by: Austin Golding <austin.golding.dev@icloud.com>
🦋 Changeset detectedLatest commit: 9575c67 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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])); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ❤️
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>
Fix new array index value not being cloned before insertion into store - Fixes #5099
packages/xstate-solid/src/createImmutable.ts
: Refactored theupdateStore
function to deep clone new elements in the array, ensuring immutability.Additions to test cases:
packages/xstate-solid/test/useActor.test.tsx
: Added a new test case to verify that moving objects between arrays does not mutate the context.