pull/12215/head
Rich Harris 4 months ago
parent c2f4787dfa
commit 5d078faaf3

@ -1863,11 +1863,9 @@ export const template_visitors = {
const is_reactive = const is_reactive =
callee.type !== 'Identifier' || context.state.scope.get(callee.name)?.kind !== 'normal'; callee.type !== 'Identifier' || context.state.scope.get(callee.name)?.kind !== 'normal';
/** @type {import('estree').Expression[]} */ const args = raw_args.map((arg) =>
const args = [context.state.node]; b.thunk(/** @type {import('estree').Expression} */ (context.visit(arg)))
for (const arg of raw_args) { );
args.push(b.thunk(/** @type {import('estree').Expression} */ (context.visit(arg))));
}
let snippet_function = /** @type {import('estree').Expression} */ (context.visit(callee)); let snippet_function = /** @type {import('estree').Expression} */ (context.visit(callee));
if (context.state.options.dev) { if (context.state.options.dev) {
@ -1875,12 +1873,15 @@ export const template_visitors = {
} }
if (is_reactive) { if (is_reactive) {
context.state.init.push(b.stmt(b.call('$.snippet', b.thunk(snippet_function), ...args))); context.state.init.push(
b.stmt(b.call('$.snippet', context.state.node, b.thunk(snippet_function), ...args))
);
} else { } else {
context.state.init.push( context.state.init.push(
b.stmt( b.stmt(
(node.expression.type === 'CallExpression' ? b.call : b.maybe_call)( (node.expression.type === 'CallExpression' ? b.call : b.maybe_call)(
snippet_function, snippet_function,
context.state.node,
...args ...args
) )
) )

@ -2,26 +2,25 @@ import { add_snippet_symbol } from '../../../shared/validate.js';
import { EFFECT_TRANSPARENT } from '../../constants.js'; import { EFFECT_TRANSPARENT } from '../../constants.js';
import { branch, block, destroy_effect } from '../../reactivity/effects.js'; import { branch, block, destroy_effect } from '../../reactivity/effects.js';
import { import {
current_component_context,
dev_current_component_function, dev_current_component_function,
set_dev_current_component_function set_dev_current_component_function
} from '../../runtime.js'; } from '../../runtime.js';
/** /**
* @template {(node: import('#client').TemplateNode, ...args: any[]) => import('#client').Dom} SnippetFn * @template {(node: import('#client').TemplateNode, ...args: any[]) => import('#client').Dom} SnippetFn
* @param {import('#client').TemplateNode} anchor
* @param {() => SnippetFn | null | undefined} get_snippet * @param {() => SnippetFn | null | undefined} get_snippet
* @param {import('#client').TemplateNode} node
* @param {(() => any)[]} args * @param {(() => any)[]} args
* @returns {void} * @returns {void}
*/ */
export function snippet(get_snippet, node, ...args) { export function snippet(anchor, get_snippet, ...args) {
/** @type {SnippetFn | null | undefined} */ /** @type {SnippetFn | null | undefined} */
var snippet; var snippet;
/** @type {import('#client').Effect | null} */ /** @type {import('#client').Effect | null} */
var snippet_effect; var snippet_effect;
block(null, EFFECT_TRANSPARENT, () => { block(anchor, EFFECT_TRANSPARENT, () => {
if (snippet === (snippet = get_snippet())) return; if (snippet === (snippet = get_snippet())) return;
if (snippet_effect) { if (snippet_effect) {
@ -30,7 +29,7 @@ export function snippet(get_snippet, node, ...args) {
} }
if (snippet) { if (snippet) {
snippet_effect = branch(() => /** @type {SnippetFn} */ (snippet)(node, ...args)); snippet_effect = branch(() => /** @type {SnippetFn} */ (snippet)(anchor, ...args));
} }
}); });
} }

@ -341,7 +341,7 @@ export function execute_effect_teardown(effect) {
export function destroy_effect(effect, remove_dom = true) { export function destroy_effect(effect, remove_dom = true) {
var removed = false; var removed = false;
if (remove_dom || (effect.f & HEAD_EFFECT) !== 0) { if ((remove_dom || (effect.f & HEAD_EFFECT) !== 0) && effect.nodes !== null) {
var start = get_first_node(effect); var start = get_first_node(effect);
var end = get_last_node(effect); var end = get_last_node(effect);

Loading…
Cancel
Save