use function composition

pull/15538/head
Rich Harris 4 months ago
parent 408955dae6
commit 7d1551b3dd

@ -12,16 +12,11 @@ import * as b from '../../../../utils/builders.js';
* @returns * @returns
*/ */
function get_template_function(namespace, state) { function get_template_function(namespace, state) {
const contains_script_tag = state.template.contains_script_tag;
return ( return (
namespace === 'svg' namespace === 'svg'
? contains_script_tag ? '$.ns_template'
? '$.svg_template_with_script'
: '$.ns_template'
: namespace === 'mathml' : namespace === 'mathml'
? '$.mathml_template' ? '$.mathml_template'
: contains_script_tag
? '$.template_with_script'
: '$.template' : '$.template'
).concat(state.options.templatingMode === 'functional' ? '_fn' : ''); ).concat(state.options.templatingMode === 'functional' ? '_fn' : '');
} }
@ -67,6 +62,10 @@ export function transform_template(state, namespace, flags) {
flags ? b.literal(flags) : undefined flags ? b.literal(flags) : undefined
); );
if (state.template.contains_script_tag) {
call = b.call(`$.with_script`, call);
}
if (dev) { if (dev) {
return b.call( return b.call(
'$.add_locations', '$.add_locations',

@ -165,24 +165,10 @@ export function template_fn(structure, flags) {
} }
/** /**
* @param {string} content * @param {() => Element | DocumentFragment} fn
* @param {number} flags
* @returns {() => Node | Node[]}
*/ */
/*#__NO_SIDE_EFFECTS__*/ export function with_script(fn) {
export function template_with_script(content, flags) { return () => run_scripts(fn());
var fn = template(content, flags);
return () => run_scripts(/** @type {Element | DocumentFragment} */ (fn()));
}
/**
* @param {Array<TemplateStructure>} structure
* @param {number} flags
* @returns {() => Node | Node[]}
*/ /*#__NO_SIDE_EFFECTS__*/
export function template_with_script_fn(structure, flags) {
var templated_fn = template_fn(structure, flags);
return () => run_scripts(/** @type {Element | DocumentFragment} */ (templated_fn()));
} }
/** /**
@ -290,28 +276,6 @@ export function ns_template_fn(structure, flags, ns = 'svg') {
}; };
} }
/**
* @param {string} content
* @param {number} flags
* @returns {() => Node | Node[]}
*/
/*#__NO_SIDE_EFFECTS__*/
export function svg_template_with_script(content, flags) {
var fn = ns_template(content, flags);
return () => run_scripts(/** @type {Element | DocumentFragment} */ (fn()));
}
/**
* @param {Array<TemplateStructure>} structure
* @param {number} flags
* @returns {() => Node | Node[]}
*/
/*#__NO_SIDE_EFFECTS__*/
export function svg_template_with_script_fn(structure, flags) {
var templated_fn = ns_template_fn(structure, flags);
return () => run_scripts(/** @type {Element | DocumentFragment} */ (templated_fn()));
}
/** /**
* @param {string} content * @param {string} content
* @param {number} flags * @param {number} flags

@ -90,16 +90,13 @@ export {
comment, comment,
ns_template, ns_template,
ns_template_fn, ns_template_fn,
svg_template_with_script,
svg_template_with_script_fn,
mathml_template, mathml_template,
mathml_template_fn, mathml_template_fn,
template, template,
template_fn, template_fn,
template_with_script,
template_with_script_fn,
text, text,
props_id props_id,
with_script
} from './dom/template.js'; } from './dom/template.js';
export { user_derived as derived, derived_safe_equal } from './reactivity/deriveds.js'; export { user_derived as derived, derived_safe_equal } from './reactivity/deriveds.js';
export { export {

Loading…
Cancel
Save