chore: tweak each block logic (#12884)

* chore: tweak each block logic to avoid Array.from

* more tweaks

* lint

* Update packages/svelte/src/internal/client/dom/blocks/each.js

Co-authored-by: Rich Harris <rich.harris@vercel.com>

* Update packages/svelte/src/internal/client/dom/blocks/each.js

Co-authored-by: Rich Harris <rich.harris@vercel.com>

* Update packages/svelte/src/internal/client/dom/blocks/each.js

Co-authored-by: Rich Harris <rich.harris@vercel.com>

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
pull/12889/head
Dominic Gannaway 3 months ago committed by GitHub
parent aa55b709e7
commit 4512462b66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -32,8 +32,8 @@ import {
resume_effect
} from '../../reactivity/effects.js';
import { source, mutable_source, set } from '../../reactivity/sources.js';
import { is_array, is_frozen } from '../../../shared/utils.js';
import { INERT, STATE_SYMBOL } from '../../constants.js';
import { array_from, is_array } from '../../../shared/utils.js';
import { INERT } from '../../constants.js';
import { queue_micro_task } from '../task.js';
import { current_effect } from '../../runtime.js';
@ -139,7 +139,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
? collection
: collection == null
? []
: Array.from(collection);
: array_from(collection);
var length = array.length;
@ -242,14 +242,14 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
var first = state.first;
var current = first;
/** @type {Set<EachItem>} */
var seen = new Set();
/** @type {undefined | Set<EachItem>} */
var seen;
/** @type {EachItem | null} */
var prev = null;
/** @type {Set<EachItem>} */
var to_animate = new Set();
/** @type {undefined | Set<EachItem>} */
var to_animate;
/** @type {EachItem[]} */
var matched = [];
@ -277,7 +277,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
if (item !== undefined) {
item.a?.measure();
to_animate.add(item);
(to_animate ??= new Set()).add(item);
}
}
}
@ -319,12 +319,12 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
resume_effect(item.e);
if (is_animated) {
item.a?.unfix();
to_animate.delete(item);
(to_animate ??= new Set()).delete(item);
}
}
if (item !== current) {
if (seen.has(item)) {
if (seen !== undefined && seen.has(item)) {
if (matched.length < stashed.length) {
// more efficient to move later items to the front
var start = stashed[0];
@ -372,7 +372,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
stashed = [];
while (current !== null && current.k !== key) {
seen.add(current);
(seen ??= new Set()).add(current);
stashed.push(current);
current = current.next;
}
@ -389,32 +389,36 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
current = item.next;
}
const to_destroy = Array.from(seen);
if (current !== null || seen !== undefined) {
var to_destroy = seen === undefined ? [] : array_from(seen);
while (current !== null) {
to_destroy.push(current);
current = current.next;
}
var destroy_length = to_destroy.length;
while (current !== null) {
to_destroy.push(current);
current = current.next;
}
if (destroy_length > 0) {
var controlled_anchor = (flags & EACH_IS_CONTROLLED) !== 0 && length === 0 ? anchor : null;
var destroy_length = to_destroy.length;
if (is_animated) {
for (i = 0; i < destroy_length; i += 1) {
to_destroy[i].a?.measure();
}
if (destroy_length > 0) {
var controlled_anchor = (flags & EACH_IS_CONTROLLED) !== 0 && length === 0 ? anchor : null;
for (i = 0; i < destroy_length; i += 1) {
to_destroy[i].a?.fix();
if (is_animated) {
for (i = 0; i < destroy_length; i += 1) {
to_destroy[i].a?.measure();
}
for (i = 0; i < destroy_length; i += 1) {
to_destroy[i].a?.fix();
}
}
}
pause_effects(state, to_destroy, controlled_anchor, items);
pause_effects(state, to_destroy, controlled_anchor, items);
}
}
if (is_animated) {
queue_micro_task(() => {
if (to_animate === undefined) return;
for (item of to_animate) {
item.a?.apply();
}

Loading…
Cancel
Save