use dynamic_dependencies instead of dependencies where possible

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

@ -33,7 +33,7 @@ export default class Attribute extends Node {
this.isSynthetic = false; this.isSynthetic = false;
this.expression = new Expression(component, this, scope, info.expression); this.expression = new Expression(component, this, scope, info.expression);
this.dependencies = this.expression.dependencies; this.dependencies = this.expression.dynamic_dependencies;
this.chunks = null; this.chunks = null;
this.isDynamic = true; // TODO not necessarily this.isDynamic = true; // TODO not necessarily
@ -54,7 +54,7 @@ export default class Attribute extends Node {
const expression = new Expression(component, this, scope, node.expression); const expression = new Expression(component, this, scope, node.expression);
addToSet(this.dependencies, expression.dependencies); addToSet(this.dependencies, expression.dynamic_dependencies);
return expression; return expression;
}); });

@ -67,7 +67,7 @@ export default class AwaitBlockWrapper extends Wrapper {
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
block.addDependencies(this.node.expression.dependencies); block.addDependencies(this.node.expression.dynamic_dependencies);
let isDynamic = false; let isDynamic = false;
let hasIntros = false; let hasIntros = false;

@ -45,7 +45,7 @@ export default class DebugTagWrapper extends Wrapper {
const dependencies = new Set(); const dependencies = new Set();
this.node.expressions.forEach(expression => { this.node.expressions.forEach(expression => {
addToSet(dependencies, expression.dependencies); addToSet(dependencies, expression.dynamic_dependencies);
}); });
const condition = [...dependencies].map(d => `changed.${d}`).join(' || '); const condition = [...dependencies].map(d => `changed.${d}`).join(' || ');

@ -80,8 +80,8 @@ export default class EachBlockWrapper extends Wrapper {
super(renderer, block, parent, node); super(renderer, block, parent, node);
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
const { dependencies } = node.expression; const { dynamic_dependencies } = node.expression;
block.addDependencies(dependencies); block.addDependencies(dynamic_dependencies);
this.block = block.child({ this.block = block.child({
comment: createDebuggingComment(this.node, this.renderer.component), comment: createDebuggingComment(this.node, this.renderer.component),

@ -31,14 +31,14 @@ export default class BindingWrapper {
this.node = node; this.node = node;
this.parent = parent; this.parent = parent;
const { dependencies } = this.node.expression; const { dynamic_dependencies } = this.node.expression;
block.addDependencies(dependencies); block.addDependencies(dynamic_dependencies);
// TODO does this also apply to e.g. `<input type='checkbox' bind:group='foo'>`? // TODO does this also apply to e.g. `<input type='checkbox' bind:group='foo'>`?
if (parent.node.name === 'select') { if (parent.node.name === 'select') {
parent.selectBindingDependencies = dependencies; parent.selectBindingDependencies = dynamic_dependencies;
dependencies.forEach((prop: string) => { dynamic_dependencies.forEach((prop: string) => {
parent.renderer.component.indirectDependencies.set(prop, new Set()); parent.renderer.component.indirectDependencies.set(prop, new Set());
}); });
} }
@ -140,7 +140,7 @@ export default class BindingWrapper {
updateDom = null; updateDom = null;
} }
const dependencyArray = [...this.node.expression.dependencies] const dependencyArray = [...this.node.expression.dynamic_dependencies]
if (dependencyArray.length === 1) { if (dependencyArray.length === 1) {
updateConditions.push(`changed.${dependencyArray[0]}`) updateConditions.push(`changed.${dependencyArray[0]}`)

@ -4,6 +4,7 @@ import AttributeWrapper from './Attribute';
import Node from '../../../nodes/shared/Node'; import Node from '../../../nodes/shared/Node';
import ElementWrapper from '.'; import ElementWrapper from '.';
import { stringify } from '../../../../utils/stringify'; import { stringify } from '../../../../utils/stringify';
import addToSet from '../../../../utils/addToSet';
export interface StyleProp { export interface StyleProp {
key: string; key: string;
@ -32,12 +33,9 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
const { dependencies } = chunk;
const snippet = chunk.render(); const snippet = chunk.render();
dependencies.forEach(d => { addToSet(propDependencies, chunk.dynamic_dependencies);
propDependencies.add(d);
});
return chunk.getPrecedence() <= 13 ? `(${snippet})` : snippet; return chunk.getPrecedence() <= 13 ? `(${snippet})` : snippet;
} }

@ -168,13 +168,13 @@ export default class ElementWrapper extends Wrapper {
// add directive and handler dependencies // add directive and handler dependencies
[node.animation, node.outro, ...node.actions, ...node.classes].forEach(directive => { [node.animation, node.outro, ...node.actions, ...node.classes].forEach(directive => {
if (directive && directive.expression) { if (directive && directive.expression) {
block.addDependencies(directive.expression.dependencies); block.addDependencies(directive.expression.dynamic_dependencies);
} }
}); });
node.handlers.forEach(handler => { node.handlers.forEach(handler => {
if (handler.expression) { if (handler.expression) {
block.addDependencies(handler.expression.dependencies); block.addDependencies(handler.expression.dynamic_dependencies);
} }
}); });

@ -87,7 +87,7 @@ export default class IfBlockWrapper extends Wrapper {
this.branches.push(branch); this.branches.push(branch);
blocks.push(branch.block); blocks.push(branch.block);
block.addDependencies(node.expression.dependencies); block.addDependencies(node.expression.dynamic_dependencies);
if (branch.block.dependencies.size > 0) { if (branch.block.dependencies.size > 0) {
isDynamic = true; isDynamic = true;

@ -31,7 +31,7 @@ export default class InlineComponentWrapper extends Wrapper {
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
if (this.node.expression) { if (this.node.expression) {
block.addDependencies(this.node.expression.dependencies); block.addDependencies(this.node.expression.dynamic_dependencies);
} }
this.node.attributes.forEach(attr => { this.node.attributes.forEach(attr => {
@ -48,12 +48,12 @@ export default class InlineComponentWrapper extends Wrapper {
eachBlock.hasBinding = true; eachBlock.hasBinding = true;
} }
block.addDependencies(binding.expression.dependencies); block.addDependencies(binding.expression.dynamic_dependencies);
}); });
this.node.handlers.forEach(handler => { this.node.handlers.forEach(handler => {
if (handler.expression) { if (handler.expression) {
block.addDependencies(handler.expression.dependencies); block.addDependencies(handler.expression.dynamic_dependencies);
} }
}); });
@ -214,7 +214,7 @@ export default class InlineComponentWrapper extends Wrapper {
); );
updates.push(deindent` updates.push(deindent`
if (!${updating} && ${[...binding.expression.dependencies].map((dependency: string) => `changed.${dependency}`).join(' || ')}) { if (!${updating} && ${[...binding.expression.dynamic_dependencies].map((dependency: string) => `changed.${dependency}`).join(' || ')}) {
${name_changes}${quotePropIfNecessary(binding.name)} = ${snippet}; ${name_changes}${quotePropIfNecessary(binding.name)} = ${snippet};
} }
`); `);

@ -47,10 +47,9 @@ export default class TitleWrapper extends Wrapper {
if (chunk.type === 'Text') { if (chunk.type === 'Text') {
return stringify(chunk.data); return stringify(chunk.data);
} else { } else {
const { dependencies } = chunk.expression;
const snippet = chunk.expression.render(); const snippet = chunk.expression.render();
dependencies.forEach(d => { chunk.expression.dynamic_dependencies.forEach(d => {
allDependencies.add(d); allDependencies.add(d);
}); });

@ -11,14 +11,14 @@ export default class Tag extends Wrapper {
super(renderer, block, parent, node); super(renderer, block, parent, node);
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
block.addDependencies(node.expression.dependencies); block.addDependencies(node.expression.dynamic_dependencies);
} }
renameThisMethod( renameThisMethod(
block: Block, block: Block,
update: ((value: string) => string) update: ((value: string) => string)
) { ) {
const { dependencies } = this.node.expression; const dependencies = this.node.expression.dynamic_dependencies;
const snippet = this.node.expression.render(); const snippet = this.node.expression.render();
const value = this.node.shouldCache && block.getUniqueName(`${this.var}_value`); const value = this.node.shouldCache && block.getUniqueName(`${this.var}_value`);

@ -4,15 +4,15 @@ import { SvelteComponentDev, addLoc, append, createElement, createText, detachNo
const file = undefined; const file = undefined;
function create_fragment(component, ctx) { function create_fragment(component, ctx) {
var p, text0_value = Math.max(0, ctx.foo), text0, text1, text2_value = ctx.bar(), text2, current; var p, text0_value = Math.max(0, ctx.foo), text0, text1, text2, current;
return { return {
c: function create() { c: function create() {
p = createElement("p"); p = createElement("p");
text0 = createText(text0_value); text0 = createText(text0_value);
text1 = createText("\n\t"); text1 = createText("\n\t");
text2 = createText(text2_value); text2 = createText(ctx.bar);
addLoc(p, file, 8, 0, 77); addLoc(p, file, 7, 0, 67);
}, },
l: function claim(nodes) { l: function claim(nodes) {
@ -32,8 +32,8 @@ function create_fragment(component, ctx) {
setData(text0, text0_value); setData(text0, text0_value);
} }
if ((changed.bar) && text2_value !== (text2_value = ctx.bar())) { if (changed.bar) {
setData(text2, text2_value); setData(text2, ctx.bar);
} }
}, },
@ -52,18 +52,20 @@ function create_fragment(component, ctx) {
}; };
} }
function define($$self, $$props) { function define($$self, $$props, $$make_dirty) {
let { foo } = $$props; let { foo } = $$props;
function bar() { let bar;
return foo * 2;
}
$$self.$$.get = () => ({ foo, bar }); $$self.$$.get = () => ({ foo, bar });
$$self.$$.set = $$props => { $$self.$$.set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) foo = $$props.foo;
}; };
$$self.$$.update = ($$dirty = { foo: 1 }) => {
if ($$dirty.foo) { bar = foo * 2; $$make_dirty('bar'); }
};
} }
class SvelteComponent extends SvelteComponentDev { class SvelteComponent extends SvelteComponentDev {

@ -1,12 +1,11 @@
<script> <script>
export let foo; export let foo;
function bar() { let bar;
return foo * 2; $: bar = foo * 2;
}
</script> </script>
<p> <p>
{Math.max(0, foo)} {Math.max(0, foo)}
{bar()} {bar}
</p> </p>
Loading…
Cancel
Save