Skip to content

Commit 8738be0

Browse files
committed
only animate blocks that persist
1 parent 88d1287 commit 8738be0

File tree

1 file changed

+24
-19
lines changed
  • packages/svelte/src/internal/client/dom/blocks

1 file changed

+24
-19
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,6 @@ function reconcile_tracked_array(array, each_block, anchor, render_fn, flags, ke
336336
/** @type {Array<import('#client').EachItemBlock>} */
337337
var to_destroy = [];
338338

339-
/** @type {Array<import('#client').EachItemBlock>} */
340-
var to_animate = [];
341-
342-
if (is_animated) {
343-
for (block of a_blocks) {
344-
// TODO can we avoid measuring blocks that will be destroyed?
345-
block.a?.measure();
346-
}
347-
}
348-
349339
// Step 1 — trim common suffix
350340
while (a > 0 && b > 0 && a_blocks[a - 1].k === keys[b - 1]) {
351341
block = b_blocks[--b] = a_blocks[--a];
@@ -400,6 +390,21 @@ function reconcile_tracked_array(array, each_block, anchor, render_fn, flags, ke
400390
map_set(indexes, keys[i], i);
401391
}
402392

393+
/** @type {Array<import('#client').EachItemBlock>} */
394+
var to_animate = [];
395+
if (is_animated) {
396+
// for all blocks that were in both the old and the new list,
397+
// measure them and store them in `to_animate` so we can
398+
// apply animations once the DOM has been updated
399+
for (i = 0; i < a_blocks.length; i += 1) {
400+
block = a_blocks[i];
401+
if (indexes.has(block.k)) {
402+
block.a?.measure();
403+
to_animate.push(block);
404+
}
405+
}
406+
}
407+
403408
// populate the `sources` array for each old block with
404409
// its new index, so that we can calculate moves
405410
for (i = start; i < a; i += 1) {
@@ -437,7 +442,7 @@ function reconcile_tracked_array(array, each_block, anchor, render_fn, flags, ke
437442
block = create_block(array[b], keys[b], b, render_fn, flags);
438443
} else {
439444
block = b_blocks[b];
440-
if (!is_animated && should_update) {
445+
if (should_update) {
441446
update_block(block, array[b], b, flags);
442447
}
443448
}
@@ -449,16 +454,16 @@ function reconcile_tracked_array(array, each_block, anchor, render_fn, flags, ke
449454

450455
last_block = b_blocks[b] = block;
451456
}
452-
}
453457

454-
if (to_animate.length > 0) {
455-
user_effect(() => {
456-
untrack(() => {
457-
for (block of to_animate) {
458-
block.a?.apply();
459-
}
458+
if (to_animate.length > 0) {
459+
user_effect(() => {
460+
untrack(() => {
461+
for (block of to_animate) {
462+
block.a?.apply();
463+
}
464+
});
460465
});
461-
});
466+
}
462467
}
463468

464469
var remaining = to_destroy.length;

0 commit comments

Comments
 (0)