Lint and format src/compiler/compile/render_dom/invalidate.js

pull/8569/head
Simon Holthausen 3 years ago
parent 15d7b2b21c
commit 92aef8e122

@ -9,72 +9,80 @@ import { x } from 'code-red';
* @param {boolean} main_execution_context * @param {boolean} main_execution_context
*/ */
export function invalidate(renderer, scope, node, names, main_execution_context = false) { export function invalidate(renderer, scope, node, names, main_execution_context = false) {
const { component } = renderer; const { component } = renderer;
const [head, ...tail] = /** @type {import('../../interfaces.js').Var[]} */ (Array.from(names) const [head, ...tail] = /** @type {import('../../interfaces.js').Var[]} */ (
.filter((name) => { Array.from(names)
const owner = scope.find_owner(name); .filter((name) => {
return !owner || owner === component.instance_scope; const owner = scope.find_owner(name);
}) return !owner || owner === component.instance_scope;
.map((name) => component.var_lookup.get(name)) })
.filter((variable) => { .map((name) => component.var_lookup.get(name))
return (variable && .filter((variable) => {
!variable.hoistable && return (
!variable.global && variable &&
!variable.module && !variable.hoistable &&
(variable.referenced || !variable.global &&
variable.subscribable || !variable.module &&
variable.is_reactive_dependency || (variable.referenced ||
variable.export_name || variable.subscribable ||
variable.name[0] === '$')); variable.is_reactive_dependency ||
})); variable.export_name ||
variable.name[0] === '$')
);
})
);
/** /**
* @param {import('../../interfaces.js').Var} variable * @param {import('../../interfaces.js').Var} variable
* @param {import('estree').Expression} [node] * @param {import('estree').Expression} [node]
*/ */
function get_invalidated(variable, node) { function get_invalidated(variable, node) {
if (main_execution_context && !variable.subscribable && variable.name[0] !== '$') { if (main_execution_context && !variable.subscribable && variable.name[0] !== '$') {
return node; return node;
} }
return renderer_invalidate(renderer, variable.name, undefined, main_execution_context); return renderer_invalidate(renderer, variable.name, undefined, main_execution_context);
} }
if (!head) { if (!head) {
return node; return node;
} }
component.has_reactive_assignments = true; component.has_reactive_assignments = true;
if (node.type === 'AssignmentExpression' && if (
node.operator === '=' && node.type === 'AssignmentExpression' &&
nodes_match(node.left, node.right) && node.operator === '=' &&
tail.length === 0) { nodes_match(node.left, node.right) &&
return get_invalidated(head, node); tail.length === 0
} ) {
const is_store_value = head.name[0] === '$' && head.name[1] !== '$'; return get_invalidated(head, node);
const extra_args = tail.map((variable) => get_invalidated(variable)).filter(Boolean); }
if (is_store_value) { const is_store_value = head.name[0] === '$' && head.name[1] !== '$';
return x `@set_store_value(${head.name.slice(1)}, ${node}, ${head.name}, ${extra_args})`; const extra_args = tail.map((variable) => get_invalidated(variable)).filter(Boolean);
} if (is_store_value) {
let invalidate; return x`@set_store_value(${head.name.slice(1)}, ${node}, ${head.name}, ${extra_args})`;
if (!main_execution_context) { }
const pass_value = extra_args.length > 0 || let invalidate;
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') || if (!main_execution_context) {
(node.type === 'UpdateExpression' && (!node.prefix || node.argument.type !== 'Identifier')); const pass_value =
if (pass_value) { extra_args.length > 0 ||
extra_args.unshift({ (node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
type: 'Identifier', (node.type === 'UpdateExpression' && (!node.prefix || node.argument.type !== 'Identifier'));
name: head.name if (pass_value) {
}); extra_args.unshift({
} type: 'Identifier',
invalidate = x `$$invalidate(${renderer.context_lookup.get(head.name).index}, ${node}, ${extra_args})`; name: head.name
} });
else { }
// skip `$$invalidate` if it is in the main execution context invalidate = x`$$invalidate(${
invalidate = extra_args.length ? [node, ...extra_args] : node; renderer.context_lookup.get(head.name).index
} }, ${node}, ${extra_args})`;
if (head.subscribable && head.reassigned) { } else {
const subscribe = `$$subscribe_${head.name}`; // skip `$$invalidate` if it is in the main execution context
invalidate = x `${subscribe}(${invalidate})`; invalidate = extra_args.length ? [node, ...extra_args] : node;
} }
return invalidate; if (head.subscribable && head.reassigned) {
const subscribe = `$$subscribe_${head.name}`;
invalidate = x`${subscribe}(${invalidate})`;
}
return invalidate;
} }
/** /**
@ -84,57 +92,53 @@ export function invalidate(renderer, scope, node, names, main_execution_context
* @param {boolean} main_execution_context * @param {boolean} main_execution_context
*/ */
export function renderer_invalidate(renderer, name, value, main_execution_context = false) { export function renderer_invalidate(renderer, name, value, main_execution_context = false) {
const variable = renderer.component.var_lookup.get(name); const variable = renderer.component.var_lookup.get(name);
if (variable && variable.subscribable && (variable.reassigned || variable.export_name)) { if (variable && variable.subscribable && (variable.reassigned || variable.export_name)) {
if (main_execution_context) { if (main_execution_context) {
return x `${`$$subscribe_${name}`}(${value || name})`; return x`${`$$subscribe_${name}`}(${value || name})`;
} } else {
else { const member = renderer.context_lookup.get(name);
const member = renderer.context_lookup.get(name); return x`${`$$subscribe_${name}`}($$invalidate(${member.index}, ${value || name}))`;
return x `${`$$subscribe_${name}`}($$invalidate(${member.index}, ${value || name}))`; }
} }
} if (name[0] === '$' && name[1] !== '$') {
if (name[0] === '$' && name[1] !== '$') { return x`${name.slice(1)}.set(${value || name})`;
return x `${name.slice(1)}.set(${value || name})`; }
} if (
if (variable && variable &&
(variable.module || (variable.module ||
(!variable.referenced && (!variable.referenced &&
!variable.is_reactive_dependency && !variable.is_reactive_dependency &&
!variable.export_name && !variable.export_name &&
!name.startsWith('$$')))) { !name.startsWith('$$')))
return value || name; ) {
} return value || name;
if (value) { }
if (main_execution_context) { if (value) {
return x `${value}`; if (main_execution_context) {
} return x`${value}`;
else { } else {
const member = renderer.context_lookup.get(name); const member = renderer.context_lookup.get(name);
return x `$$invalidate(${member.index}, ${value})`; return x`$$invalidate(${member.index}, ${value})`;
} }
} }
if (main_execution_context) if (main_execution_context) return;
return; // if this is a reactive declaration, invalidate dependencies recursively
// if this is a reactive declaration, invalidate dependencies recursively const deps = new Set([name]);
const deps = new Set([name]); deps.forEach((name) => {
deps.forEach((name) => { const reactive_declarations = renderer.component.reactive_declarations.filter((x) =>
const reactive_declarations = renderer.component.reactive_declarations.filter((x) => x.assignees.has(name)); x.assignees.has(name)
reactive_declarations.forEach((declaration) => { );
declaration.dependencies.forEach((name) => { reactive_declarations.forEach((declaration) => {
deps.add(name); declaration.dependencies.forEach((name) => {
}); deps.add(name);
}); });
}); });
// TODO ideally globals etc wouldn't be here in the first place });
const filtered = Array.from(deps).filter((n) => renderer.context_lookup.has(n)); // TODO ideally globals etc wouldn't be here in the first place
if (!filtered.length) const filtered = Array.from(deps).filter((n) => renderer.context_lookup.has(n));
return null; if (!filtered.length) return null;
return filtered return filtered
.map((n) => x `$$invalidate(${renderer.context_lookup.get(n).index}, ${n})`) .map((n) => x`$$invalidate(${renderer.context_lookup.get(n).index}, ${n})`)
.reduce((lhs, rhs) => x `${lhs}, ${rhs}`); .reduce((lhs, rhs) => x`${lhs}, ${rhs}`);
} }

Loading…
Cancel
Save