Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update inputs when the value changed #1703

Merged
merged 1 commit into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/compile/nodes/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class Binding extends Node {
dimensions.test(this.name)
);

let updateCondition: string;
let updateConditions: string[] = [];

const { name } = getObject(this.value.node);
const { snippet } = this.value;
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class Binding extends Node {
}

if (this.name === 'currentTime' || this.name === 'volume') {
updateCondition = `!isNaN(${snippet})`;
updateConditions.push(`!isNaN(${snippet})`);

if (this.name === 'currentTime') initialUpdate = null;
}
Expand All @@ -118,7 +118,7 @@ export default class Binding extends Node {
const last = block.getUniqueName(`${node.var}_is_paused`);
block.addVariable(last, 'true');

updateCondition = `${last} !== (${last} = ${snippet})`;
updateConditions.push(`${last} !== (${last} = ${snippet})`);
updateDom = `${node.var}[${last} ? "pause" : "play"]();`;
initialUpdate = null;
}
Expand All @@ -129,14 +129,24 @@ export default class Binding extends Node {
updateDom = null;
}

const dependencyArray = [...this.value.dependencies]

if (dependencyArray.length === 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not positive that it's needed, but using quotePropIfNecessary here seems like a good idea.

Copy link
Contributor

@jacwright jacwright Aug 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't quoting change.${prop} in most areas of the app because, for the most part, you cannot reference something that needs quotes in expressions in the template. The exception is when shorthands are used, such as:

<Comp :my-shorthand-binding-var-has-dashes/>
<div class:my-class-name-shorthand-has-dashes></div>

So do shorthand bindings come through here? If so, I agree, we need to quotePropIfNecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These dependency prop strings are the same ones used like ctx.${prop} a bit further down, so I felt comfortable not looking for a quote function.

updateConditions.push(`changed.${dependencyArray[0]}`)
} else if (dependencyArray.length > 1) {
updateConditions.push(
`(${dependencyArray.map(prop => `changed.${prop}`).join(' || ')})`
)
}

return {
name: this.name,
object: name,
handler,
updateDom,
initialUpdate,
needsLock: !isReadOnly && needsLock,
updateCondition,
updateCondition: updateConditions.length ? updateConditions.join(' && ') : undefined,
isReadOnlyMediaAttribute: this.isReadOnlyMediaAttribute()
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/input-files/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
if (!input_updating) input.files = ctx.files;
if (!input_updating && changed.files) input.files = ctx.files;
},

d(detach) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/input-files/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
if (!input_updating) input.files = ctx.files;
if (!input_updating && changed.files) input.files = ctx.files;
},

d(detach) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/input-range/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
input.value = ctx.value;
if (changed.value) input.value = ctx.value;
},

d(detach) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/input-range/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
input.value = ctx.value;
if (changed.value) input.value = ctx.value;
},

d(detach) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
input.checked = ctx.foo;
if (changed.foo) input.checked = ctx.foo;
},

d(detach) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/input-without-blowback-guard/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
input.checked = ctx.foo;
if (changed.foo) input.checked = ctx.foo;
},

d(detach) {
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/media-bindings/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
if (!audio_updating && !isNaN(ctx.currentTime )) audio.currentTime = ctx.currentTime ;
if (!audio_updating && audio_is_paused !== (audio_is_paused = ctx.paused )) audio[audio_is_paused ? "pause" : "play"]();
if (!audio_updating && !isNaN(ctx.volume)) audio.volume = ctx.volume;
if (!audio_updating && !isNaN(ctx.currentTime ) && changed.currentTime) audio.currentTime = ctx.currentTime ;
if (!audio_updating && audio_is_paused !== (audio_is_paused = ctx.paused ) && changed.paused) audio[audio_is_paused ? "pause" : "play"]();
if (!audio_updating && !isNaN(ctx.volume) && changed.volume) audio.volume = ctx.volume;
},

d(detach) {
Expand Down
6 changes: 3 additions & 3 deletions test/js/samples/media-bindings/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ function create_main_fragment(component, ctx) {
},

p(changed, ctx) {
if (!audio_updating && !isNaN(ctx.currentTime )) audio.currentTime = ctx.currentTime ;
if (!audio_updating && audio_is_paused !== (audio_is_paused = ctx.paused )) audio[audio_is_paused ? "pause" : "play"]();
if (!audio_updating && !isNaN(ctx.volume)) audio.volume = ctx.volume;
if (!audio_updating && !isNaN(ctx.currentTime ) && changed.currentTime) audio.currentTime = ctx.currentTime ;
if (!audio_updating && audio_is_paused !== (audio_is_paused = ctx.paused ) && changed.paused) audio[audio_is_paused ? "pause" : "play"]();
if (!audio_updating && !isNaN(ctx.volume) && changed.volume) audio.volume = ctx.volume;
},

d(detach) {
Expand Down