Skip to content

Commit 4512462

Browse files
trueadmRich-Harris
andauthored
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 <[email protected]> * Update packages/svelte/src/internal/client/dom/blocks/each.js Co-authored-by: Rich Harris <[email protected]> * Update packages/svelte/src/internal/client/dom/blocks/each.js Co-authored-by: Rich Harris <[email protected]> --------- Co-authored-by: Rich Harris <[email protected]>
1 parent aa55b70 commit 4512462

File tree

1 file changed

+31
-27
lines changed
  • packages/svelte/src/internal/client/dom/blocks

1 file changed

+31
-27
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import {
3232
resume_effect
3333
} from '../../reactivity/effects.js';
3434
import { source, mutable_source, set } from '../../reactivity/sources.js';
35-
import { is_array, is_frozen } from '../../../shared/utils.js';
36-
import { INERT, STATE_SYMBOL } from '../../constants.js';
35+
import { array_from, is_array } from '../../../shared/utils.js';
36+
import { INERT } from '../../constants.js';
3737
import { queue_micro_task } from '../task.js';
3838
import { current_effect } from '../../runtime.js';
3939

@@ -139,7 +139,7 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
139139
? collection
140140
: collection == null
141141
? []
142-
: Array.from(collection);
142+
: array_from(collection);
143143

144144
var length = array.length;
145145

@@ -242,14 +242,14 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
242242
var first = state.first;
243243
var current = first;
244244

245-
/** @type {Set<EachItem>} */
246-
var seen = new Set();
245+
/** @type {undefined | Set<EachItem>} */
246+
var seen;
247247

248248
/** @type {EachItem | null} */
249249
var prev = null;
250250

251-
/** @type {Set<EachItem>} */
252-
var to_animate = new Set();
251+
/** @type {undefined | Set<EachItem>} */
252+
var to_animate;
253253

254254
/** @type {EachItem[]} */
255255
var matched = [];
@@ -277,7 +277,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
277277

278278
if (item !== undefined) {
279279
item.a?.measure();
280-
to_animate.add(item);
280+
(to_animate ??= new Set()).add(item);
281281
}
282282
}
283283
}
@@ -319,12 +319,12 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
319319
resume_effect(item.e);
320320
if (is_animated) {
321321
item.a?.unfix();
322-
to_animate.delete(item);
322+
(to_animate ??= new Set()).delete(item);
323323
}
324324
}
325325

326326
if (item !== current) {
327-
if (seen.has(item)) {
327+
if (seen !== undefined && seen.has(item)) {
328328
if (matched.length < stashed.length) {
329329
// more efficient to move later items to the front
330330
var start = stashed[0];
@@ -372,7 +372,7 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
372372
stashed = [];
373373

374374
while (current !== null && current.k !== key) {
375-
seen.add(current);
375+
(seen ??= new Set()).add(current);
376376
stashed.push(current);
377377
current = current.next;
378378
}
@@ -389,32 +389,36 @@ function reconcile(array, state, anchor, render_fn, flags, get_key) {
389389
current = item.next;
390390
}
391391

392-
const to_destroy = Array.from(seen);
392+
if (current !== null || seen !== undefined) {
393+
var to_destroy = seen === undefined ? [] : array_from(seen);
393394

394-
while (current !== null) {
395-
to_destroy.push(current);
396-
current = current.next;
397-
}
398-
var destroy_length = to_destroy.length;
395+
while (current !== null) {
396+
to_destroy.push(current);
397+
current = current.next;
398+
}
399399

400-
if (destroy_length > 0) {
401-
var controlled_anchor = (flags & EACH_IS_CONTROLLED) !== 0 && length === 0 ? anchor : null;
400+
var destroy_length = to_destroy.length;
402401

403-
if (is_animated) {
404-
for (i = 0; i < destroy_length; i += 1) {
405-
to_destroy[i].a?.measure();
406-
}
402+
if (destroy_length > 0) {
403+
var controlled_anchor = (flags & EACH_IS_CONTROLLED) !== 0 && length === 0 ? anchor : null;
407404

408-
for (i = 0; i < destroy_length; i += 1) {
409-
to_destroy[i].a?.fix();
405+
if (is_animated) {
406+
for (i = 0; i < destroy_length; i += 1) {
407+
to_destroy[i].a?.measure();
408+
}
409+
410+
for (i = 0; i < destroy_length; i += 1) {
411+
to_destroy[i].a?.fix();
412+
}
410413
}
411-
}
412414

413-
pause_effects(state, to_destroy, controlled_anchor, items);
415+
pause_effects(state, to_destroy, controlled_anchor, items);
416+
}
414417
}
415418

416419
if (is_animated) {
417420
queue_micro_task(() => {
421+
if (to_animate === undefined) return;
418422
for (item of to_animate) {
419423
item.a?.apply();
420424
}

0 commit comments

Comments
 (0)