Skip to content

Commit fdb6140

Browse files
committed
perf(size): flatten shares its thunk walk
flatten and flattenArray share isThunk/unwrap/skipped: the same checks in the same order. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 073fe73 commit fdb6140

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

‎packages/signals/src/flatten.ts‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ import { NotReadyError } from "./core/index.js";
55
// through — not a boundary feature. Moved verbatim so the boundary carve
66
// measures boundaries alone.
77

8+
// A zero-argument function is a thunk the walk calls through.
9+
const isThunk = (value: any): boolean => typeof value === "function" && !value.length;
10+
11+
function unwrap(value: any): any {
12+
do value = value();
13+
while (isThunk(value));
14+
return value;
15+
}
16+
17+
// `skipNonRendered`: null, undefined, booleans and "" render nothing.
18+
const skipped = (value: any, options?: { skipNonRendered?: boolean }): boolean | undefined =>
19+
options?.skipNonRendered && (value == null || value === true || value === false || value === "");
20+
821
/**
922
* Resolves nested accessors and arrays in a children tree to their rendered
1023
* values. See the `children()` helper in solid-js for the memoized form.
@@ -13,17 +26,11 @@ export function flatten(
1326
children: any,
1427
options?: { skipNonRendered?: boolean; doNotUnwrap?: boolean }
1528
): any {
16-
if (typeof children === "function" && !children.length) {
29+
if (isThunk(children)) {
1730
if (options?.doNotUnwrap) return children;
18-
do {
19-
children = children();
20-
} while (typeof children === "function" && !children.length);
31+
children = unwrap(children);
2132
}
22-
if (
23-
options?.skipNonRendered &&
24-
(children == null || children === true || children === false || children === "")
25-
)
26-
return;
33+
if (skipped(children, options)) return;
2734

2835
if (Array.isArray(children)) {
2936
let results: any[] = [];
@@ -49,27 +56,20 @@ function flattenArray(
4956
for (let i = 0; i < children.length; i++) {
5057
try {
5158
let child = children[i];
52-
if (typeof child === "function" && !child.length) {
59+
if (isThunk(child)) {
5360
if (options?.doNotUnwrap) {
5461
results.push(child);
5562
needsUnwrap = true;
5663
continue;
5764
}
58-
do {
59-
child = child();
60-
} while (typeof child === "function" && !child.length);
65+
child = unwrap(child);
6166
}
6267
if (Array.isArray(child)) {
6368
// OR, don't overwrite: an accessor already pushed under doNotUnwrap
6469
// still needs the resolving wrapper even when a later sibling
6570
// fragment contains no functions (#3133).
6671
needsUnwrap = flattenArray(child, results, options) || needsUnwrap;
67-
} else if (
68-
options?.skipNonRendered &&
69-
(child == null || child === true || child === false || child === "")
70-
) {
71-
// skip
72-
} else results.push(child);
72+
} else if (!skipped(child, options)) results.push(child);
7373
} catch (e) {
7474
if (!(e instanceof NotReadyError)) throw e;
7575
notReady = e;

0 commit comments

Comments
 (0)