Skip to content

Commit b5a02f2

Browse files
authored
Merge branch 'main' into test/map-array-keyed-lifecycle
2 parents a08b6e6 + 4e39813 commit b5a02f2

13 files changed

Lines changed: 195 additions & 29 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Stop a `createRoot` dispose function from retaining the root's `fn`. The dispose function shared `createRoot`'s scope, so holding it kept `fn` and everything `fn` closes over alive: every `mapArray`/`indexArray` row (and so every `<For>`/`<Index>` row) kept the list it was created from, including items later removed from it.

‎.changeset/fix-lazy-remount.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Fix `lazy()` rendering nothing, or the Suspense fallback, when an already-loaded component is mounted again. Disposing an instance still releases the shared resource while its import is in flight or has failed, so that load can be collected and retried, but a resolved module stays cached and the next mount renders synchronously.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Preserve the target array length when reconciling stores on the server.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Update DOM Expressions to 0.40.13. The universal renderer's spread now clears properties whose keys are removed, calling `setProperty(node, name, undefined, prev)`, and SVG `<radialGradient>` accepts `fr`.

‎package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
"babel-plugin-jsx-dom-expressions": "^0.40.11",
3838
"coveralls": "^3.1.1",
3939
"csstype": "^3.1.0",
40-
"dom-expressions": "0.40.12",
41-
"hyper-dom-expressions": "0.40.12",
40+
"dom-expressions": "0.40.13",
41+
"hyper-dom-expressions": "0.40.13",
4242
"jsdom": "^25.0.1",
43-
"lit-dom-expressions": "0.40.12",
43+
"lit-dom-expressions": "0.40.13",
4444
"ncp": "^2.0.0",
4545
"npm-run-all": "^4.1.5",
4646
"prettier": "^3.6.2",

‎packages/solid/src/reactive/signal.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export function createRoot<T>(fn: RootFunction<T>, detachedOwner?: typeof Owner)
169169
throw new Error("Dispose method must be an explicit argument to createRoot function");
170170
})
171171
: fn
172-
: () => fn(() => untrack(() => cleanNode(root)));
172+
: () => fn(createDisposer(root));
173173

174174
if (IS_DEV) DevHooks.afterCreateOwner && DevHooks.afterCreateOwner(root);
175175

@@ -184,6 +184,13 @@ export function createRoot<T>(fn: RootFunction<T>, detachedOwner?: typeof Owner)
184184
}
185185
}
186186

187+
// Created outside createRoot so the dispose function does not share its scope,
188+
// which holds `fn`: whoever keeps the dispose function would keep `fn` and
189+
// everything it closes over (e.g. the list a mapArray row was created from).
190+
function createDisposer(root: Owner) {
191+
return () => untrack(() => cleanNode(root));
192+
}
193+
187194
export type Accessor<T> = () => T;
188195

189196
export type Setter<in out T> = {

‎packages/solid/src/render/component.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,12 @@ export function lazy<T extends Component<any>>(
405405
} else if (!comp) {
406406
const [s] = createResource<T>(() => load().then(mod => mod.default));
407407
comp = s;
408-
onCleanup(() => (comp = undefined));
408+
// `load()` replaces `comp` with the resolved component. Only drop the shared
409+
// cache while it still points at this resource, so an in-flight or failed
410+
// import can be collected and retried without blanking a module that loaded.
411+
onCleanup(() => {
412+
if (comp === s) comp = undefined;
413+
});
409414
}
410415
let Comp: T | undefined;
411416
return createMemo(() =>

‎packages/solid/store/src/server.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function reconcile<T extends U, U extends object>(
153153
for (let i = 0, len = previousKeys.length; i < len; i++) {
154154
if (value[previousKeys[i]] === undefined) setProperty(state, previousKeys[i], undefined);
155155
}
156+
if (Array.isArray(state) && Array.isArray(value)) setProperty(state, "length", value.length);
156157
return state as T;
157158
};
158159
}

‎packages/solid/test/array.spec.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { describe, expect, test, vi } from "vitest";
2+
import { setFlagsFromString } from "v8";
3+
import { runInNewContext } from "vm";
24
import {
35
mapArray,
46
indexArray,
@@ -120,6 +122,32 @@ describe("Map operator", () => {
120122
for (const item of [...initial, ...added]) expect(item.cleanup).toHaveBeenCalledTimes(1);
121123
});
122124
});
125+
126+
test("a surviving row does not retain removed items", async () => {
127+
setFlagsFromString("--expose-gc");
128+
const gc: () => void = runInNewContext("gc");
129+
const kept = {};
130+
let removed: WeakRef<object>, set: (v: object[]) => void;
131+
const dispose = createRoot(dispose => {
132+
const [s, setS] = createSignal(createItems()),
133+
r = createMemo(mapArray(s, v => v));
134+
set = setS;
135+
r();
136+
return dispose;
137+
});
138+
function createItems() {
139+
const item = {};
140+
removed = new WeakRef(item);
141+
return [kept, item];
142+
}
143+
// The row for `kept` was created from the original list.
144+
set!([kept]);
145+
// WeakRef targets stay alive until the current job ends.
146+
await new Promise(r => setTimeout(r, 0));
147+
gc();
148+
expect(removed!.deref()).toBeUndefined();
149+
dispose();
150+
});
123151
});
124152

125153
describe("Index operator", () => {
@@ -145,4 +173,4 @@ describe("Index operator", () => {
145173
expect(r()).toEqual([6, 8, 10]);
146174
});
147175
});
148-
});
176+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, test } from "vitest";
2+
import { createStore, reconcile, unwrap } from "../../store/src/server.js";
3+
4+
describe("reconcile array length on the server", () => {
5+
const cases: [string, (number | undefined)[], (number | undefined)[]][] = [
6+
["shortens an array", [1, 2, 3], [4]],
7+
["clears an array", [1, 2], []],
8+
["grows an array", [1], [1, 2, 3]],
9+
["preserves trailing undefined entries", [1], [1, undefined]],
10+
["preserves sparse array length", [1], new Array(3)]
11+
];
12+
13+
test.each(cases)("%s at the root", (_, initial, next) => {
14+
const original = initial.slice();
15+
const [state, setState] = createStore(original);
16+
setState(reconcile(next));
17+
expect(state).toHaveLength(next.length);
18+
expect([...state]).toEqual([...next]);
19+
expect(unwrap(state)).toBe(original);
20+
});
21+
22+
test.each(cases)("%s on a nested path", (_, initial, next) => {
23+
const original = initial.slice();
24+
const [state, setState] = createStore({ items: original });
25+
setState("items", reconcile(next));
26+
expect(state.items).toHaveLength(next.length);
27+
expect([...state.items]).toEqual([...next]);
28+
expect(unwrap(state.items)).toBe(original);
29+
});
30+
});

0 commit comments

Comments
 (0)