Skip to content

Commit 6384839

Browse files
committed
Split $.each into $.each_keyed/$.each_indexed
1 parent 6d7caf3 commit 6384839

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Please note that [the Svelte codebase is currently being rewritten for Svelte 5]
44

55
If your PR concerns Svelte 4 (including updates to [svelte.dev.docs](https://svelte.dev/docs)), please ensure the base branch is `svelte-4` and not `main`.
66

7-
87
### Before submitting the PR, please make sure you do the following
98

109
- [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,19 +2227,34 @@ export const template_visitors = {
22272227
declarations.push(b.let(node.index, index));
22282228
}
22292229

2230-
context.state.after_update.push(
2231-
b.stmt(
2232-
b.call(
2233-
'$.each',
2234-
context.state.node,
2235-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2236-
b.literal(each_type),
2237-
key_function,
2238-
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2239-
else_block
2230+
if ((each_type & EACH_KEYED) !== 0) {
2231+
context.state.after_update.push(
2232+
b.stmt(
2233+
b.call(
2234+
'$.each_keyed',
2235+
context.state.node,
2236+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2237+
b.literal(each_type),
2238+
key_function,
2239+
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2240+
else_block
2241+
)
22402242
)
2241-
)
2242-
);
2243+
);
2244+
} else {
2245+
context.state.after_update.push(
2246+
b.stmt(
2247+
b.call(
2248+
'$.each_indexed',
2249+
context.state.node,
2250+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
2251+
b.literal(each_type),
2252+
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
2253+
else_block
2254+
)
2255+
)
2256+
);
2257+
}
22432258
},
22442259
IfBlock(node, context) {
22452260
context.state.template.push('<!>');

packages/svelte/src/internal/client/reconciler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ export function reconcile_indexed_array(
260260
* @param {Element | Comment | Text} dom
261261
* @param {boolean} is_controlled
262262
* @param {(anchor: null, item: V, index: number | import('./types.js').Signal<number>) => void} render_fn
263-
* @param {Array<string> | null} keys
264263
* @param {number} flags
265264
* @param {boolean} apply_transitions
265+
* @param {Array<string> | null} keys
266266
* @returns {void}
267267
*/
268268
export function reconcile_tracked_array(
@@ -271,9 +271,9 @@ export function reconcile_tracked_array(
271271
dom,
272272
is_controlled,
273273
render_fn,
274-
keys,
275274
flags,
276-
apply_transitions
275+
apply_transitions,
276+
keys
277277
) {
278278
var a_blocks = each_block.items;
279279
const is_computed_key = keys !== null;

packages/svelte/src/internal/client/render.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,9 +2263,10 @@ export function each_item_block(item, key, index, render_fn, flags) {
22632263
* @param {null | ((item: V) => string)} key_fn
22642264
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
22652265
* @param {null | ((anchor: Node) => void)} fallback_fn
2266+
* @param {typeof reconcile_indexed_array | reconcile_tracked_array} reconcile_fn
22662267
* @returns {void}
22672268
*/
2268-
export function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn) {
2269+
function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, reconcile_fn) {
22692270
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
22702271
const block = create_each_block(flags, anchor_node);
22712272

@@ -2385,20 +2386,7 @@ export function each(anchor_node, collection, flags, key_fn, render_fn, fallback
23852386
const flags = block.flags;
23862387
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
23872388
const anchor_node = block.anchor;
2388-
if ((flags & EACH_KEYED) !== 0) {
2389-
reconcile_tracked_array(
2390-
array,
2391-
block,
2392-
anchor_node,
2393-
is_controlled,
2394-
render_fn,
2395-
keys,
2396-
flags,
2397-
true
2398-
);
2399-
} else {
2400-
reconcile_indexed_array(array, block, anchor_node, is_controlled, render_fn, flags, true);
2401-
}
2389+
reconcile_fn(array, block, anchor_node, is_controlled, render_fn, flags, true, keys);
24022390
},
24032391
block,
24042392
true
@@ -2426,6 +2414,33 @@ export function each(anchor_node, collection, flags, key_fn, render_fn, fallback
24262414
block.effect = each;
24272415
}
24282416

2417+
/**
2418+
* @template V
2419+
* @param {Element | Comment} anchor_node
2420+
* @param {() => V[]} collection
2421+
* @param {number} flags
2422+
* @param {null | ((item: V) => string)} key_fn
2423+
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
2424+
* @param {null | ((anchor: Node) => void)} fallback_fn
2425+
* @returns {void}
2426+
*/
2427+
export function each_keyed(anchor_node, collection, flags, key_fn, render_fn, fallback_fn) {
2428+
each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, reconcile_tracked_array);
2429+
}
2430+
2431+
/**
2432+
* @template V
2433+
* @param {Element | Comment} anchor_node
2434+
* @param {() => V[]} collection
2435+
* @param {number} flags
2436+
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
2437+
* @param {null | ((anchor: Node) => void)} fallback_fn
2438+
* @returns {void}
2439+
*/
2440+
export function each_indexed(anchor_node, collection, flags, render_fn, fallback_fn) {
2441+
each(anchor_node, collection, flags, null, render_fn, fallback_fn, reconcile_indexed_array);
2442+
}
2443+
24292444
/**
24302445
* @param {Element | Text | Comment} anchor
24312446
* @param {boolean} is_html

0 commit comments

Comments
 (0)