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-trailing-removal-notify.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/signals": patch
---

reconcile: notify ownKeys subscribers on pure keyed trailing removal
4 changes: 2 additions & 2 deletions packages/solid-signals/src/store/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function applyStateFast(next: any, target: any, keyFn: (item: NonNullable<any>)
applyState(next[j], wrapped, keyFn);
}

changed && notifySelf(target);
(changed || prevLength !== next.length) && notifySelf(target);
prevLength !== next.length &&
arrayNodes?.length &&
setSignal(arrayNodes.length, next.length);
Expand Down Expand Up @@ -244,7 +244,7 @@ function applyStateSlow(next: any, target: any, keyFn: (item: NonNullable<any>)
}

const nextLength = next.length;
changed && notifySelf(target);
(changed || prevLength !== nextLength) && notifySelf(target);
prevLength !== nextLength && nodes?.length && setSignal(nodes.length, nextLength);
return;
}
Expand Down
23 changes: 22 additions & 1 deletion packages/solid-signals/tests/store/reconcile.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { createStore, reconcile, snapshot } from "../../src/index.js";
import { createStore, reconcile, snapshot, $TRACK, createMemo, createRoot, createEffect, createRenderEffect, flush } from "../../src/index.js";

describe("setState with reconcile", () => {
test("Reconcile a simple object", () => {
Expand Down Expand Up @@ -158,6 +158,27 @@ describe("setState with reconcile", () => {
setStore(reconcile({ value: { q: "aa" } }, "id"));
expect(store.value).toEqual({ q: "aa" });
});
test("Reconcile keyed trailing removal notifies $TRACK subscribers", () => {
let effectRunCount = 0;
const [state, setState] = createStore({ arr: [{ id: 1 }, { id: 2 }, { id: 3 }] });
createRoot(() => {
createRenderEffect(() => {
effectRunCount++;
// accessing $TRACK subscribes to ownKeys notifications on arr
(state.arr as any)[$TRACK];
return undefined;
}, () => undefined);
});
// flush to run the effect initially
flush();
const runsBefore = effectRunCount;
setState(s => {
reconcile([{ id: 1 }, { id: 2 }], "id")(s.arr);
});
// flush to propagate invalidation and re-run the effect
flush();
expect(effectRunCount).toBeGreaterThan(runsBefore);
});
});
// type tests

Expand Down
Loading