You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/src/compile/render-dom/wrappers/shared/add_actions.ts

47 lines
1.2 KiB

import Block from '../../Block';
import Action from '../../../nodes/Action';
import Component from '../../../Component';
export default function add_actions(
component: Component,
block: Block,
target: string,
actions: Action[]
) {
actions.forEach(action => {
const { expression } = action;
let snippet, dependencies;
if (expression) {
snippet = expression.render(block);
dependencies = expression.dynamic_dependencies();
}
const name = block.get_unique_name(
`${action.name.replace(/[^a-zA-Z0-9_$]/g, '_')}_action`
);
block.add_variable(name);
const fn = component.qualify(action.name);
block.builders.mount.add_line(
`${name} = ${fn}.call(null, ${target}${snippet ? `, ${snippet}` : ''}) || {};`
);
if (dependencies && dependencies.length > 0) {
let conditional = `typeof ${name}.update === 'function' && `;
const deps = dependencies.map(dependency => `changed.${dependency}`).join(' || ');
conditional += dependencies.length > 1 ? `(${deps})` : deps;
block.builders.update.add_conditional(
conditional,
`${name}.update.call(null, ${snippet});`
);
}
block.builders.destroy.add_line(
`if (${name} && typeof ${name}.destroy === 'function') ${name}.destroy();`
);
});
}