avoid unnecessary if (changed.foo....) condiitons

pull/1839/head
Rich Harris 7 years ago
parent f288259c34
commit 5290988ed1

@ -65,8 +65,9 @@ export default class Expression {
node: any; node: any;
snippet: string; snippet: string;
references: Set<string>; references: Set<string>;
dependencies: Set<string>; dependencies: Set<string> = new Set();
contextual_dependencies: Set<string>; contextual_dependencies: Set<string> = new Set();
dynamic_dependencies: Set<string> = new Set();
template_scope: TemplateScope; template_scope: TemplateScope;
scope: Scope; scope: Scope;
@ -99,17 +100,31 @@ export default class Expression {
this.owner = owner; this.owner = owner;
this.is_synthetic = owner.isSynthetic; this.is_synthetic = owner.isSynthetic;
const expression_dependencies = new Set(); const { dependencies, contextual_dependencies, dynamic_dependencies } = this;
const expression_contextual_dependencies = new Set();
let dependencies = expression_dependencies;
let contextual_dependencies = expression_contextual_dependencies;
let { map, scope } = createScopes(info); let { map, scope } = createScopes(info);
this.scope = scope; this.scope = scope;
this.scope_map = map; this.scope_map = map;
const expression = this; const expression = this;
let function_expression;
function add_dependency(name) {
dependencies.add(name);
if (!function_expression) {
// dynamic_dependencies is used to create `if (changed.foo || ...)`
// conditions — it doesn't apply if the dependency is inside a
// function, and it only applies if the dependency is writable
if (component.instance_script) {
if (component.writable_declarations.has(name)) {
dynamic_dependencies.add(name);
}
} else {
dynamic_dependencies.add(name);
}
}
}
// discover dependencies, but don't change the code yet // discover dependencies, but don't change the code yet
walk(info, { walk(info, {
@ -121,6 +136,10 @@ export default class Expression {
scope = map.get(node); scope = map.get(node);
} }
if (!function_expression && /FunctionExpression/.test(node.type)) {
function_expression = node;
}
if (isReference(node, parent)) { if (isReference(node, parent)) {
const { name } = flattenReference(node); const { name } = flattenReference(node);
@ -132,21 +151,26 @@ export default class Expression {
contextual_dependencies.add(name); contextual_dependencies.add(name);
template_scope.dependenciesForName.get(name).forEach(dependency => { template_scope.dependenciesForName.get(name).forEach(add_dependency);
dependencies.add(dependency);
});
} else { } else {
dependencies.add(name); add_dependency(name);
component.template_references.add(name); component.template_references.add(name);
} }
this.skip(); this.skip();
} }
},
leave(node) {
if (map.has(node)) {
scope = scope.parent;
}
if (node === function_expression) {
function_expression = null;
}
} }
}); });
this.dependencies = dependencies;
this.contextual_dependencies = contextual_dependencies;
} }
getPrecedence() { getPrecedence() {

@ -14,7 +14,7 @@ export default function addActions(
let snippet, dependencies; let snippet, dependencies;
if (expression) { if (expression) {
snippet = expression.render(); snippet = expression.render();
dependencies = expression.dependencies; dependencies = expression.dynamic_dependencies;
expression.declarations.forEach(declaration => { expression.declarations.forEach(declaration => {
block.builders.init.addBlock(declaration); block.builders.init.addBlock(declaration);

@ -1,5 +1,5 @@
/* generated by Svelte vX.Y.Z-alpha1 */ /* generated by Svelte vX.Y.Z-alpha1 */
import { SvelteComponent as SvelteComponent_1, addListener, createElement, detachNode, flush, init, insert, noop, removeListener, run, safe_not_equal } from "svelte/internal.js"; import { SvelteComponent as SvelteComponent_1, createElement, detachNode, flush, init, insert, noop, run, safe_not_equal } from "svelte/internal.js";
function create_fragment(component, ctx) { function create_fragment(component, ctx) {
var button, foo_action, current; var button, foo_action, current;

Loading…
Cancel
Save