From 375575c4a5950f1c40c3515187f28674893d616c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2024 17:01:26 -0500 Subject: [PATCH] move render code --- .../internal/client/dom/blocks/render-tag.js | 27 +++++++++++++++++++ packages/svelte/src/internal/client/render.js | 21 --------------- packages/svelte/src/internal/index.js | 1 + 3 files changed, 28 insertions(+), 21 deletions(-) create mode 100644 packages/svelte/src/internal/client/dom/blocks/render-tag.js diff --git a/packages/svelte/src/internal/client/dom/blocks/render-tag.js b/packages/svelte/src/internal/client/dom/blocks/render-tag.js new file mode 100644 index 0000000000..2602686133 --- /dev/null +++ b/packages/svelte/src/internal/client/dom/blocks/render-tag.js @@ -0,0 +1,27 @@ +import { noop } from '../../../common.js'; +import { destroy_effect, pause_effect, render_effect } from '../../reactivity/computations.js'; +import { untrack } from '../../runtime.js'; + +/** + * @param {() => Function} get_snippet + * @param {Node} node + * @param {(() => any)[]} args + * @returns {void} + */ +export function snippet_effect(get_snippet, node, ...args) { + /** @type {import('../../types.js').EffectSignal | null} */ + let effect; + + render_effect(() => { + // Only rerender when the snippet function itself changes, + // not when an eagerly-read prop inside the snippet function changes + const snippet = get_snippet(); + + if (effect) { + pause_effect(effect, noop); // TODO we want to just destroy immediately + destroy_effect(effect); + } + + effect = render_effect(() => untrack(() => snippet(node, ...args)), {}, true); + }); +} diff --git a/packages/svelte/src/internal/client/render.js b/packages/svelte/src/internal/client/render.js index 824e111e24..9e240b6451 100644 --- a/packages/svelte/src/internal/client/render.js +++ b/packages/svelte/src/internal/client/render.js @@ -2417,27 +2417,6 @@ export function sanitize_slots(props) { return sanitized; } -/** - * @param {() => Function} get_snippet - * @param {Node} node - * @param {(() => any)[]} args - * @returns {void} - */ -export function snippet_effect(get_snippet, node, ...args) { - const block = create_snippet_block(); - render_effect(() => { - // Only rerender when the snippet function itself changes, - // not when an eagerly-read prop inside the snippet function changes - const snippet = get_snippet(); - untrack(() => snippet(node, ...args)); - return () => { - if (block.d !== null) { - remove(block.d); - } - }; - }, block); -} - /** * @param {Node} target * @param {string} style_sheet_id diff --git a/packages/svelte/src/internal/index.js b/packages/svelte/src/internal/index.js index b37ddf4880..6a0afb1c8b 100644 --- a/packages/svelte/src/internal/index.js +++ b/packages/svelte/src/internal/index.js @@ -24,6 +24,7 @@ export * from './client/dev/ownership.js'; export { await_block as await } from './client/dom/blocks/await.js'; export { if_block as if } from './client/dom/blocks/if.js'; export { key_block as key } from './client/dom/blocks/key.js'; +export { snippet_effect } from './client/dom/blocks/render-tag.js'; export { component } from './client/dom/blocks/svelte-component.js'; export { element } from './client/dom/blocks/svelte-element.js'; export * from './client/dom/blocks/each.js';