Skip to content

Commit 03d19ec

Browse files
committed
load dynamic code lazily
load dynamic code lazily load dynamic code lazily
1 parent 0760b5c commit 03d19ec

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

packages/svelte/src/internal/client/block.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,13 @@ export function create_each_block(flags, anchor) {
175175
*/
176176
export function create_each_item_block(item, index, key) {
177177
return {
178+
// animate transition
179+
a: null,
178180
// dom
179181
d: null,
180182
// effect
181183
e: null,
184+
// index
182185
i: index,
183186
// key
184187
k: key,

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ function destroy_active_transition_blocks(active_transitions) {
686686
* @param {import('./types.js').Block} block
687687
* @returns {Text | Element | Comment}
688688
*/
689-
function get_first_element(block) {
689+
export function get_first_element(block) {
690690
const current = block.d;
691691

692692
if (is_array(current)) {
@@ -717,25 +717,9 @@ function update_each_item_block(block, item, index, type) {
717717
const transitions = block.s;
718718
const index_is_reactive = (type & EACH_INDEX_REACTIVE) !== 0;
719719
// Handle each item animations
720-
if (transitions !== null && (type & EACH_KEYED) !== 0) {
721-
let prev_index = block.i;
722-
if (index_is_reactive) {
723-
prev_index = /** @type {import('./types.js').Signal<number>} */ (prev_index).v;
724-
}
725-
const items = block.p.v;
726-
if (prev_index !== index && /** @type {number} */ (index) < items.length) {
727-
const from_dom = /** @type {Element} */ (get_first_element(block));
728-
const from = from_dom.getBoundingClientRect();
729-
// Cancel any existing key transitions
730-
for (const transition of transitions) {
731-
if (transition.r === 'key') {
732-
transition.c();
733-
}
734-
}
735-
schedule_task(() => {
736-
trigger_transitions(transitions, 'key', from);
737-
});
738-
}
720+
const each_animation = block.a;
721+
if (transitions !== null && (type & EACH_KEYED) !== 0 && each_animation !== null) {
722+
each_animation(block, transitions, index, index_is_reactive);
739723
}
740724
if (index_is_reactive) {
741725
set_signal_value(/** @type {import('./types.js').Signal<number>} */ (block.i), index);

packages/svelte/src/internal/client/transitions.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
KEY_BLOCK,
1010
ROOT_BLOCK
1111
} from './block.js';
12-
import { destroy_each_item_block } from './each.js';
12+
import { destroy_each_item_block, get_first_element } from './each.js';
1313
import { append_child } from './operations.js';
1414
import { empty } from './render.js';
1515
import {
@@ -21,6 +21,7 @@ import {
2121
managed_effect,
2222
managed_pre_effect,
2323
mark_subtree_inert,
24+
schedule_task,
2425
untrack
2526
} from './runtime.js';
2627
import { raf } from './timing.js';
@@ -432,6 +433,7 @@ export function bind_transition(dom, get_transition_fn, props_fn, direction, glo
432433
if (transition_block.t === EACH_ITEM_BLOCK) {
433434
// Lazily apply the each block transition
434435
transition_block.r = each_item_transition;
436+
transition_block.a = each_item_animate;
435437
transition_block = transition_block.p;
436438
} else if (transition_block.t === AWAIT_BLOCK && transition_block.n /* pending */) {
437439
can_show_intro_on_mount = false;
@@ -646,3 +648,31 @@ function each_item_transition(transition) {
646648
});
647649
transitions.add(transition);
648650
}
651+
652+
/**
653+
*
654+
* @param {import('./types.js').EachItemBlock} block
655+
* @param {Set<import('./types.js').Transition>} transitions
656+
* @param {number} index
657+
* @param {boolean} index_is_reactive
658+
*/
659+
function each_item_animate(block, transitions, index, index_is_reactive) {
660+
let prev_index = block.i;
661+
if (index_is_reactive) {
662+
prev_index = /** @type {import('./types.js').Signal<number>} */ (prev_index).v;
663+
}
664+
const items = block.p.v;
665+
if (prev_index !== index && /** @type {number} */ (index) < items.length) {
666+
const from_dom = /** @type {Element} */ (get_first_element(block));
667+
const from = from_dom.getBoundingClientRect();
668+
// Cancel any existing key transitions
669+
for (const transition of transitions) {
670+
if (transition.r === 'key') {
671+
transition.c();
672+
}
673+
}
674+
schedule_task(() => {
675+
trigger_transitions(transitions, 'key', from);
676+
});
677+
}
678+
}

packages/svelte/src/internal/client/types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ export type EachBlock = {
285285
};
286286

287287
export type EachItemBlock = {
288+
/** transition */
289+
a:
290+
| null
291+
| ((
292+
block: EachItemBlock,
293+
transitions: Set<Transition>,
294+
index: number,
295+
index_is_reactive: boolean
296+
) => void);
288297
/** dom */
289298
d: null | TemplateNode | Array<TemplateNode>;
290299
/** effect */

0 commit comments

Comments
 (0)