Skip to content

Commit e1f3b56

Browse files
authored
Merge pull request #2722 from brenelz/fix-store-primitive-array-map
Fix stale mappings for primitive store arrays
2 parents 52255dc + 23c9563 commit e1f3b56

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

.changeset/thin-badgers-move.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
update primitive store array mappings

packages/solid-signals/src/store/reconcile.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ function applyStateFast(next: any, target: any, keyFn: (item: NonNullable<any>)
137137
} else if (next.length) {
138138
for (let i = 0, len = next.length; i < len; i++) {
139139
const item = previous[i];
140-
isWrappable(item)
141-
? applyState(next[i], wrap(item, target), keyFn)
142-
: target[STORE_NODE][i] && setSignal(target[STORE_NODE][i], next[i]);
140+
if (isWrappable(item)) applyState(next[i], wrap(item, target), keyFn);
141+
else {
142+
if (item !== next[i]) changed = true;
143+
target[STORE_NODE][i] && setSignal(target[STORE_NODE][i], next[i]);
144+
}
143145
}
144146
}
145147

@@ -279,9 +281,11 @@ function applyStateSlow(next: any, target: any, keyFn: (item: NonNullable<any>)
279281
} else if (next.length) {
280282
for (let i = 0, len = next.length; i < len; i++) {
281283
const item = getOverrideValue(previous, override, i as any, optOverride);
282-
isWrappable(item)
283-
? applyState(next[i], wrap(item, target), keyFn)
284-
: target[STORE_NODE][i] && setSignal(target[STORE_NODE][i], next[i]);
284+
if (isWrappable(item)) applyState(next[i], wrap(item, target), keyFn);
285+
else {
286+
if (item !== next[i]) changed = true;
287+
target[STORE_NODE][i] && setSignal(target[STORE_NODE][i], next[i]);
288+
}
285289
}
286290
}
287291

packages/solid-signals/tests/maparray-store-nonkeyed.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
import { describe, expect, test } from "vitest";
2-
import { createRoot, createStore, mapArray, flush } from "../src/index.js";
2+
import { createRoot, createSignal, createStore, mapArray, flush } from "../src/index.js";
3+
4+
describe("mapArray backed by derived store arrays", () => {
5+
test("updates when same-length primitive array items are replaced", () => {
6+
let setIssue!: (issue: number) => void;
7+
let mapped!: () => string[];
8+
9+
const dispose = createRoot(dispose => {
10+
const [issue, set] = createSignal(0);
11+
const [comments] = createStore(
12+
() => [`issue ${issue()} comment 0`, `issue ${issue()} comment 1`],
13+
[]
14+
);
15+
setIssue = set;
16+
mapped = mapArray(
17+
() => comments,
18+
comment => comment
19+
);
20+
return dispose;
21+
});
22+
23+
expect(mapped()).toEqual(["issue 0 comment 0", "issue 0 comment 1"]);
24+
25+
setIssue(1);
26+
flush();
27+
28+
expect(mapped()).toEqual(["issue 1 comment 0", "issue 1 comment 1"]);
29+
dispose();
30+
});
31+
});
332

433
describe("mapArray keyed:false backed by a store (#2687)", () => {
534
// Regression: mapArray's internal owner is a Root, so untracked store-proxy

0 commit comments

Comments
 (0)