Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-reconcile-array-object-type-mismatch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/signals": patch
---

reconcile: force wholesale replacement when nested value type changes between array and object
2 changes: 2 additions & 0 deletions packages/solid-signals/src/store/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function applyStateFast(next: any, target: any, keyFn: (item: NonNullable<any>)
!previousValue ||
!isWrappable(previousValue) ||
!isWrappable(nextValue) ||
Array.isArray(previousValue) !== Array.isArray(nextValue) ||
(keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue))
) {
tracked && setSignal(tracked, void 0);
Expand Down Expand Up @@ -314,6 +315,7 @@ function applyStateSlow(next: any, target: any, keyFn: (item: NonNullable<any>)
!previousValue ||
!isWrappable(previousValue) ||
!isWrappable(nextValue) ||
Array.isArray(previousValue) !== Array.isArray(nextValue) ||
(keyFn(previousValue) != null && keyFn(previousValue) !== keyFn(nextValue))
) {
tracked && setSignal(tracked, void 0);
Expand Down
18 changes: 18 additions & 0 deletions packages/solid-signals/tests/store/reconcile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test } from "vitest";
import { createMemo, createRoot, flush } from "../../src/index.js";
import { createStore, reconcile, snapshot } from "../../src/index.js";

describe("setState with reconcile", () => {
Expand Down Expand Up @@ -158,6 +159,23 @@ describe("setState with reconcile", () => {
setStore(reconcile({ value: { q: "aa" } }, "id"));
expect(store.value).toEqual({ q: "aa" });
});

test("Reconcile overwrite tracked array with object updates the signal node", () => {
const [store, setStore] = createStore<{ value: any }>({ value: [1, 2] });
let derived: any;

// Establish a tracking subscription on store.value so a signal node is created for it
createRoot(() => {
derived = createMemo(() => store.value);
});
expect(Array.isArray(derived())).toBe(true);

setStore(reconcile({ value: { a: 1 } }, "id"));
flush();

expect(Array.isArray(derived())).toBe(false);
expect((derived() as any).a).toBe(1);
});
});
// type tests

Expand Down
Loading