Skip to content

Commit

Permalink
Merge pull request solidjs#1699 from mdynnl/fix-stores
Browse files Browse the repository at this point in the history
stores: array items tracking, removal, fixes solidjs#1695
  • Loading branch information
ryansolid authored Apr 30, 2023
2 parents f04bdc9 + 8e7a483 commit 775edd3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/solid/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ export function setProperty(
node: DataNode | undefined;
if ((node = getDataNode(nodes, property, prev))) node.$(() => value);

if (Array.isArray(state) && state.length !== len)
if (Array.isArray(state) && state.length !== len) {
for (let i = state.length; i < len; i++) (node = nodes[i]) && node.$();
(node = getDataNode(nodes, "length", len)) && node.$(state.length);
}
(node = nodes._) && node.$();
}

Expand Down
21 changes: 21 additions & 0 deletions packages/solid/store/test/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,27 @@ describe("Tracking State changes", () => {
setState("user", "firstName", "Jake");
});

test("Track array item on removal", () => {
const [state, setState] = createStore([1]);
createRoot(() => {
let executionCount = 0;

expect.assertions(2);
createEffect(() => {
if (executionCount === 0) {
expect(state[0]).toBe(1);
} else if (executionCount === 1) {
expect(state[0]).toBe(undefined);
} else {
// should never get here
expect(executionCount).toBe(-1);
}
executionCount++;
});
});
setState([]);
});

test("Tracking Top-Level Array iteration", () => {
const [state, setState] = createStore(["hi"]);
let executionCount = 0;
Expand Down

0 comments on commit 775edd3

Please sign in to comment.