Skip to content

Commit 8798f3b

Browse files
trueadmdummdidumm
andauthored
chore: split $.each into $.each_keyed/$.each_indexed (#9422)
* Split $.each into $.each_keyed/$.each_indexed * Add changeset * Update .changeset/quiet-camels-mate.md * Fix typo --------- Co-authored-by: Simon H <[email protected]>
1 parent 5a6afe5 commit 8798f3b

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

.changeset/quiet-camels-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: improve keyblock treeshaking

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: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import {
6868
hydrate_block_anchor,
6969
set_current_hydration_fragment
7070
} from './hydration.js';
71-
import { array_from, define_property, get_descriptor, get_descriptors, is_array } from './utils.js';
71+
import { array_from, define_property, get_descriptor, is_array } from './utils.js';
7272
import { is_promise } from '../common.js';
7373
import { bind_transition } from './transitions.js';
7474

@@ -2262,9 +2262,10 @@ export function each_item_block(item, key, index, render_fn, flags) {
22622262
* @param {null | ((item: V) => string)} key_fn
22632263
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
22642264
* @param {null | ((anchor: Node) => void)} fallback_fn
2265+
* @param {typeof reconcile_indexed_array | reconcile_tracked_array} reconcile_fn
22652266
* @returns {void}
22662267
*/
2267-
export function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn) {
2268+
function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, reconcile_fn) {
22682269
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
22692270
const block = create_each_block(flags, anchor_node);
22702271

@@ -2384,20 +2385,7 @@ export function each(anchor_node, collection, flags, key_fn, render_fn, fallback
23842385
const flags = block.flags;
23852386
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
23862387
const anchor_node = block.anchor;
2387-
if ((flags & EACH_KEYED) !== 0) {
2388-
reconcile_tracked_array(
2389-
array,
2390-
block,
2391-
anchor_node,
2392-
is_controlled,
2393-
render_fn,
2394-
keys,
2395-
flags,
2396-
true
2397-
);
2398-
} else {
2399-
reconcile_indexed_array(array, block, anchor_node, is_controlled, render_fn, flags, true);
2400-
}
2388+
reconcile_fn(array, block, anchor_node, is_controlled, render_fn, flags, true, keys);
24012389
},
24022390
block,
24032391
true
@@ -2419,12 +2407,39 @@ export function each(anchor_node, collection, flags, key_fn, render_fn, fallback
24192407
fallback = fallback.prev;
24202408
}
24212409
// Clear the array
2422-
reconcile_indexed_array([], block, anchor_node, is_controlled, render_fn, flags, false);
2410+
reconcile_fn([], block, anchor_node, is_controlled, render_fn, flags, false, keys);
24232411
destroy_signal(/** @type {import('./types.js').EffectSignal} */ (render));
24242412
});
24252413
block.effect = each;
24262414
}
24272415

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

0 commit comments

Comments
 (0)