drive-by fix

pull/10880/head
Rich Harris 2 years ago
parent c72b068fb3
commit 8eba7a354f

@ -4,24 +4,30 @@ import { untrack } from '../../runtime.js';
import { create_block } from './utils.js';
/**
* @param {() => Function | null | undefined} get_snippet
* @param {Node} node
* @template {(node: import('#client').TemplateNode, ...args: any[]) => import('#client').Dom} SnippetFn
* @param {() => SnippetFn | null | undefined} get_snippet
* @param {import('#client').TemplateNode} node
* @param {(() => any)[]} args
* @returns {void}
*/
export function snippet(get_snippet, node, ...args) {
const block = create_block();
/** @type {SnippetFn | null | undefined} */
var snippet_fn;
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 (snippet) {
untrack(() => snippet(node, ...args));
if (snippet_fn === (snippet_fn = get_snippet())) return;
if (snippet_fn) {
// Only rerender when the snippet function itself changes,
// not when an eagerly-read prop inside the snippet function changes
var dom = untrack(() => /** @type {SnippetFn} */ (snippet_fn)(node, ...args));
}
return () => {
if (block.d !== null) {
remove(block.d);
if (dom !== undefined) {
remove(dom);
}
};
}, block);

Loading…
Cancel
Save