Skip to content

fix: only destroy snippets when they have changed #11267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-mayflies-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: only destroy snippets when they have changed
16 changes: 7 additions & 9 deletions packages/svelte/src/internal/client/dom/blocks/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ export function if_block(
elseif = false
) {
/** @type {import('#client').Effect | null} */
let consequent_effect = null;
var consequent_effect = null;

/** @type {import('#client').Effect | null} */
let alternate_effect = null;
var alternate_effect = null;

/** @type {boolean | null} */
let condition = null;
var condition = null;

const effect = block(() => {
var flags = elseif ? EFFECT_TRANSPARENT : 0;

block(() => {
if (condition === (condition = !!get_condition())) return;

/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
Expand Down Expand Up @@ -76,9 +78,5 @@ export function if_block(
// continue in hydration mode
set_hydrating(true);
}
});

if (elseif) {
effect.f |= EFFECT_TRANSPARENT;
}
}, flags);
}
18 changes: 12 additions & 6 deletions packages/svelte/src/internal/client/dom/blocks/snippet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EFFECT_TRANSPARENT } from '../../constants.js';
import { branch, render_effect } from '../../reactivity/effects.js';
import { branch, block, destroy_effect } from '../../reactivity/effects.js';

/**
* @template {(node: import('#client').TemplateNode, ...args: any[]) => import('#client').Dom} SnippetFn
Expand All @@ -12,13 +12,19 @@ export function snippet(get_snippet, node, ...args) {
/** @type {SnippetFn | null | undefined} */
var snippet;

var effect = render_effect(() => {
/** @type {import('#client').Effect | null} */
var snippet_effect;

block(() => {
if (snippet === (snippet = get_snippet())) return;

if (snippet) {
branch(() => /** @type {SnippetFn} */ (snippet)(node, ...args));
if (snippet_effect) {
destroy_effect(snippet_effect);
snippet_effect = null;
}
});

effect.f |= EFFECT_TRANSPARENT;
if (snippet) {
snippet_effect = branch(() => /** @type {SnippetFn} */ (snippet)(node, ...args));
}
}, EFFECT_TRANSPARENT);
}
26 changes: 17 additions & 9 deletions packages/svelte/src/internal/client/dom/elements/transitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { should_intro } from '../../render.js';
import { is_function } from '../../utils.js';
import { current_each_item } from '../blocks/each.js';
import { TRANSITION_GLOBAL, TRANSITION_IN, TRANSITION_OUT } from '../../../../constants.js';
import { BLOCK_EFFECT, EFFECT_RAN } from '../../constants.js';
import { BLOCK_EFFECT, EFFECT_RAN, EFFECT_TRANSPARENT } from '../../constants.js';

/**
* @template T
Expand Down Expand Up @@ -241,18 +241,26 @@ export function transition(flags, element, get_fn, get_params) {

(e.transitions ??= []).push(transition);

// if this is a local transition, we only want to run it if the parent (block) effect's
// parent (branch) effect is where the state change happened. we can determine that by
// looking at whether the branch effect is currently initializing
// if this is a local transition, we only want to run it if the parent (branch) effect's
// parent (block) effect is where the state change happened. we can determine that by
// looking at whether the block effect is currently initializing
if (is_intro && should_intro) {
var parent = /** @type {import('#client').Effect} */ (e.parent);
let run = is_global;

// e.g snippets are implemented as render effects — keep going until we find the parent block
while ((parent.f & BLOCK_EFFECT) === 0 && parent.parent) {
parent = parent.parent;
if (!run) {
var block = /** @type {import('#client').Effect | null} */ (e.parent);

// skip over transparent blocks (e.g. snippets, else-if blocks)
while (block && (block.f & EFFECT_TRANSPARENT) !== 0) {
while ((block = block.parent)) {
if ((block.f & BLOCK_EFFECT) !== 0) break;
}
}

run = !block || (block.f & EFFECT_RAN) !== 0;
}

if (is_global || (parent.f & EFFECT_RAN) !== 0) {
if (run) {
effect(() => {
untrack(() => transition.in());
});
Expand Down
9 changes: 6 additions & 3 deletions packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,12 @@ export function render_effect(fn) {
return create_effect(RENDER_EFFECT, fn, true);
}

/** @param {(() => void)} fn */
export function block(fn) {
return create_effect(RENDER_EFFECT | BLOCK_EFFECT, fn, true);
/**
* @param {(() => void)} fn
* @param {number} flags
*/
export function block(fn, flags = 0) {
return create_effect(RENDER_EFFECT | BLOCK_EFFECT | flags, fn, true);
}

/** @param {(() => void)} fn */
Expand Down
Loading