Skip to content

Commit

Permalink
@xstate/solid Update new array index set to use placeholder value if …
Browse files Browse the repository at this point in the history
…wrappable to prevent mutating original object/array without needing to deep clone

Signed-off-by: Austin Golding <[email protected]>
  • Loading branch information
GoldingAustin committed Oct 17, 2024
1 parent 3ff295d commit 9575c67
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .changeset/strong-keys-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@xstate/solid': patch
---

Deep clone values in new array indexes to prevent mutation of original machine context
When setting new array indexes, if the value is an object/array, use placeholder empty value to prevent mutation of original machine context
10 changes: 9 additions & 1 deletion packages/xstate-solid/src/createImmutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ const resolvePath = (path: any[], obj = {}): unknown => {
return current;
};

const getWrappablePlaceholder = (value: any) => {
if (!isWrappable(value)) return value;
if (Array.isArray(value)) {
return [];
}
return {};
};

const updateStore = <Path extends unknown[]>(
nextStore: Store<any>,
prevStore: Store<any>,
Expand Down Expand Up @@ -56,7 +64,7 @@ const updateStore = <Path extends unknown[]>(
// Update new or now undefined indexes
if (newIndices !== 0) {
for (let newEnd = smallestSize; newEnd <= largestSize - 1; newEnd++) {
set(...path, newEnd, deepClone(next[newEnd]));
set(...path, newEnd, getWrappablePlaceholder(next[newEnd]));
}
}

Expand Down

0 comments on commit 9575c67

Please sign in to comment.