mirror of https://github.com/sveltejs/svelte
parent
a3e7f83790
commit
c58b47a0d1
@ -1,55 +1,55 @@
|
|||||||
import { b, x } from 'code-red';
|
import { b, x } from 'code-red';
|
||||||
import Block from '../../Block';
|
import is_contextual from '../../../nodes/shared/is_contextual.js';
|
||||||
import Action from '../../../nodes/Action';
|
|
||||||
import { Expression, Node } from 'estree';
|
/**
|
||||||
import is_contextual from '../../../nodes/shared/is_contextual';
|
* @param {import('../../Block.js').default} block
|
||||||
|
* @param {string | import('estree').Expression} target
|
||||||
export default function add_actions(block: Block, target: string | Expression, actions: Action[]) {
|
* @param {import('../../../nodes/Action.js').default[]} actions
|
||||||
|
*/
|
||||||
|
export default function add_actions(block, target, actions) {
|
||||||
actions.forEach((action) => add_action(block, target, action));
|
actions.forEach((action) => add_action(block, target, action));
|
||||||
}
|
}
|
||||||
|
|
||||||
const regex_invalid_variable_identifier_characters = /[^a-zA-Z0-9_$]/g;
|
const regex_invalid_variable_identifier_characters = /[^a-zA-Z0-9_$]/g;
|
||||||
|
|
||||||
export function add_action(block: Block, target: string | Expression, action: Action) {
|
/**
|
||||||
|
* @param {import('../../Block.js').default} block
|
||||||
|
* @param {string | import('estree').Expression} target
|
||||||
|
* @param {import('../../../nodes/Action.js').default} action
|
||||||
|
*/
|
||||||
|
export function add_action(block, target, action) {
|
||||||
const { expression, template_scope } = action;
|
const { expression, template_scope } = action;
|
||||||
let snippet: Node | undefined;
|
|
||||||
let dependencies: string[] | undefined;
|
|
||||||
|
|
||||||
|
/** @type {import('estree').Node | undefined} */
|
||||||
|
let snippet;
|
||||||
|
|
||||||
|
/** @type {string[] | undefined} */
|
||||||
|
let dependencies;
|
||||||
if (expression) {
|
if (expression) {
|
||||||
snippet = expression.manipulate(block);
|
snippet = expression.manipulate(block);
|
||||||
dependencies = expression.dynamic_dependencies();
|
dependencies = expression.dynamic_dependencies();
|
||||||
}
|
}
|
||||||
|
const id = block.get_unique_name(`${action.name.replace(regex_invalid_variable_identifier_characters, '_')}_action`);
|
||||||
const id = block.get_unique_name(
|
|
||||||
`${action.name.replace(regex_invalid_variable_identifier_characters, '_')}_action`
|
|
||||||
);
|
|
||||||
|
|
||||||
block.add_variable(id);
|
block.add_variable(id);
|
||||||
|
|
||||||
const [obj, ...properties] = action.name.split('.');
|
const [obj, ...properties] = action.name.split('.');
|
||||||
|
|
||||||
const fn = is_contextual(action.component, template_scope, obj)
|
const fn = is_contextual(action.component, template_scope, obj)
|
||||||
? block.renderer.reference(obj)
|
? block.renderer.reference(obj)
|
||||||
: obj;
|
: obj;
|
||||||
|
|
||||||
if (properties.length) {
|
if (properties.length) {
|
||||||
const member_expression = properties.reduce((lhs, rhs) => x`${lhs}.${rhs}`, fn);
|
const member_expression = properties.reduce((lhs, rhs) => x `${lhs}.${rhs}`, fn);
|
||||||
block.event_listeners.push(
|
block.event_listeners.push(x `@action_destroyer(${id} = ${member_expression}(${target}, ${snippet}))`);
|
||||||
x`@action_destroyer(${id} = ${member_expression}(${target}, ${snippet}))`
|
}
|
||||||
);
|
else {
|
||||||
} else {
|
block.event_listeners.push(x `@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`);
|
||||||
block.event_listeners.push(
|
|
||||||
x`@action_destroyer(${id} = ${fn}.call(null, ${target}, ${snippet}))`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dependencies && dependencies.length > 0) {
|
if (dependencies && dependencies.length > 0) {
|
||||||
let condition = x`${id} && @is_function(${id}.update)`;
|
let condition = x `${id} && @is_function(${id}.update)`;
|
||||||
|
|
||||||
if (dependencies.length > 0) {
|
if (dependencies.length > 0) {
|
||||||
condition = x`${condition} && ${block.renderer.dirty(dependencies)}`;
|
condition = x `${condition} && ${block.renderer.dirty(dependencies)}`;
|
||||||
}
|
}
|
||||||
|
block.chunks.update.push(b `if (${condition}) ${id}.update.call(null, ${snippet});`);
|
||||||
block.chunks.update.push(b`if (${condition}) ${id}.update.call(null, ${snippet});`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in new issue