Split $.each into $.each_keyed/$.each_indexed

pull/9422/head
Dominic Gannaway 3 years ago
parent 6d7caf3fd3
commit 638483951a

@ -4,7 +4,6 @@ Please note that [the Svelte codebase is currently being rewritten for Svelte 5]
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`. 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`.
### Before submitting the PR, please make sure you do the following ### Before submitting the PR, please make sure you do the following
- [ ] 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 - [ ] 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

@ -2227,19 +2227,34 @@ export const template_visitors = {
declarations.push(b.let(node.index, index)); declarations.push(b.let(node.index, index));
} }
context.state.after_update.push( if ((each_type & EACH_KEYED) !== 0) {
b.stmt( context.state.after_update.push(
b.call( b.stmt(
'$.each', b.call(
context.state.node, '$.each_keyed',
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection), context.state.node,
b.literal(each_type), each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
key_function, b.literal(each_type),
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))), key_function,
else_block b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
else_block
)
) )
) );
); } else {
context.state.after_update.push(
b.stmt(
b.call(
'$.each_indexed',
context.state.node,
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
b.literal(each_type),
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children))),
else_block
)
)
);
}
}, },
IfBlock(node, context) { IfBlock(node, context) {
context.state.template.push('<!>'); context.state.template.push('<!>');

@ -260,9 +260,9 @@ export function reconcile_indexed_array(
* @param {Element | Comment | Text} dom * @param {Element | Comment | Text} dom
* @param {boolean} is_controlled * @param {boolean} is_controlled
* @param {(anchor: null, item: V, index: number | import('./types.js').Signal<number>) => void} render_fn * @param {(anchor: null, item: V, index: number | import('./types.js').Signal<number>) => void} render_fn
* @param {Array<string> | null} keys
* @param {number} flags * @param {number} flags
* @param {boolean} apply_transitions * @param {boolean} apply_transitions
* @param {Array<string> | null} keys
* @returns {void} * @returns {void}
*/ */
export function reconcile_tracked_array( export function reconcile_tracked_array(
@ -271,9 +271,9 @@ export function reconcile_tracked_array(
dom, dom,
is_controlled, is_controlled,
render_fn, render_fn,
keys,
flags, flags,
apply_transitions apply_transitions,
keys
) { ) {
var a_blocks = each_block.items; var a_blocks = each_block.items;
const is_computed_key = keys !== null; const is_computed_key = keys !== null;

@ -2263,9 +2263,10 @@ export function each_item_block(item, key, index, render_fn, flags) {
* @param {null | ((item: V) => string)} key_fn * @param {null | ((item: V) => string)} key_fn
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn * @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
* @param {null | ((anchor: Node) => void)} fallback_fn * @param {null | ((anchor: Node) => void)} fallback_fn
* @param {typeof reconcile_indexed_array | reconcile_tracked_array} reconcile_fn
* @returns {void} * @returns {void}
*/ */
export function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn) { function each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, reconcile_fn) {
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0; const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
const block = create_each_block(flags, anchor_node); const block = create_each_block(flags, anchor_node);
@ -2385,20 +2386,7 @@ export function each(anchor_node, collection, flags, key_fn, render_fn, fallback
const flags = block.flags; const flags = block.flags;
const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0; const is_controlled = (flags & EACH_IS_CONTROLLED) !== 0;
const anchor_node = block.anchor; const anchor_node = block.anchor;
if ((flags & EACH_KEYED) !== 0) { reconcile_fn(array, block, anchor_node, is_controlled, render_fn, flags, true, keys);
reconcile_tracked_array(
array,
block,
anchor_node,
is_controlled,
render_fn,
keys,
flags,
true
);
} else {
reconcile_indexed_array(array, block, anchor_node, is_controlled, render_fn, flags, true);
}
}, },
block, block,
true true
@ -2426,6 +2414,33 @@ export function each(anchor_node, collection, flags, key_fn, render_fn, fallback
block.effect = each; block.effect = each;
} }
/**
* @template V
* @param {Element | Comment} anchor_node
* @param {() => V[]} collection
* @param {number} flags
* @param {null | ((item: V) => string)} key_fn
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
* @param {null | ((anchor: Node) => void)} fallback_fn
* @returns {void}
*/
export function each_keyed(anchor_node, collection, flags, key_fn, render_fn, fallback_fn) {
each(anchor_node, collection, flags, key_fn, render_fn, fallback_fn, reconcile_tracked_array);
}
/**
* @template V
* @param {Element | Comment} anchor_node
* @param {() => V[]} collection
* @param {number} flags
* @param {(anchor: null, item: V, index: import('./types.js').MaybeSignal<number>) => void} render_fn
* @param {null | ((anchor: Node) => void)} fallback_fn
* @returns {void}
*/
export function each_indexed(anchor_node, collection, flags, render_fn, fallback_fn) {
each(anchor_node, collection, flags, null, render_fn, fallback_fn, reconcile_indexed_array);
}
/** /**
* @param {Element | Text | Comment} anchor * @param {Element | Text | Comment} anchor
* @param {boolean} is_html * @param {boolean} is_html

Loading…
Cancel
Save