Refactor mount code rendering for bindings

pull/3650/head
Mattias Buelens 6 years ago
parent 39c009499e
commit 92710f8280

@ -86,6 +86,7 @@ export default class BindingWrapper {
const { parent } = this; const { parent } = this;
const update_conditions: any[] = this.needs_lock ? [x`!${lock}`] : []; const update_conditions: any[] = this.needs_lock ? [x`!${lock}`] : [];
const mount_conditions: any[] = [];
const dependency_array = [...this.node.expression.dependencies]; const dependency_array = [...this.node.expression.dependencies];
@ -103,6 +104,7 @@ export default class BindingWrapper {
// model to view // model to view
let update_dom = get_dom_updater(parent, this); let update_dom = get_dom_updater(parent, this);
let mount_dom = update_dom;
// special cases // special cases
switch (this.node.name) { switch (this.node.name) {
@ -122,13 +124,19 @@ export default class BindingWrapper {
case 'textContent': case 'textContent':
update_conditions.push(x`${this.snippet} !== ${parent.var}.textContent`); update_conditions.push(x`${this.snippet} !== ${parent.var}.textContent`);
mount_conditions.push(x`${this.snippet} !== void 0`);
break; break;
case 'innerHTML': case 'innerHTML':
update_conditions.push(x`${this.snippet} !== ${parent.var}.innerHTML`); update_conditions.push(x`${this.snippet} !== ${parent.var}.innerHTML`);
mount_conditions.push(x`${this.snippet} !== void 0`);
break; break;
case 'currentTime': case 'currentTime':
update_conditions.push(x`!@_isNaN(${this.snippet})`);
mount_dom = null;
break;
case 'playbackRate': case 'playbackRate':
case 'volume': case 'volume':
update_conditions.push(x`!@_isNaN(${this.snippet})`); update_conditions.push(x`!@_isNaN(${this.snippet})`);
@ -142,12 +150,14 @@ export default class BindingWrapper {
update_conditions.push(x`${last} !== (${last} = ${this.snippet})`); update_conditions.push(x`${last} !== (${last} = ${this.snippet})`);
update_dom = b`${parent.var}[${last} ? "pause" : "play"]();`; update_dom = b`${parent.var}[${last} ? "pause" : "play"]();`;
mount_dom = null;
break; break;
} }
case 'value': case 'value':
if (parent.node.get_static_attribute_value('type') === 'file') { if (parent.node.get_static_attribute_value('type') === 'file') {
update_dom = null; update_dom = null;
mount_dom = null;
} }
} }
@ -165,13 +175,18 @@ export default class BindingWrapper {
} }
} }
if (this.node.name === 'innerHTML' || this.node.name === 'textContent') { if (mount_dom) {
block.chunks.mount.push(b` if (mount_conditions.length > 0) {
if (${this.snippet} !== void 0) { const condition = mount_conditions.reduce((lhs, rhs) => x`${lhs} && ${rhs}`);
${update_dom}
}`); block.chunks.mount.push(b`
} else if (!/(currentTime|paused)/.test(this.node.name)) { if (${condition}) {
block.chunks.mount.push(update_dom); ${mount_dom}
}
`);
} else {
block.chunks.mount.push(mount_dom);
}
} }
} }
} }

Loading…
Cancel
Save