Skip to content

Commit 92710f8

Browse files
Refactor mount code rendering for bindings
1 parent 39c0094 commit 92710f8

File tree

1 file changed

+22
-7
lines changed
  • src/compiler/compile/render_dom/wrappers/Element

1 file changed

+22
-7
lines changed

src/compiler/compile/render_dom/wrappers/Element/Binding.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export default class BindingWrapper {
8686
const { parent } = this;
8787

8888
const update_conditions: any[] = this.needs_lock ? [x`!${lock}`] : [];
89+
const mount_conditions: any[] = [];
8990

9091
const dependency_array = [...this.node.expression.dependencies];
9192

@@ -103,6 +104,7 @@ export default class BindingWrapper {
103104

104105
// model to view
105106
let update_dom = get_dom_updater(parent, this);
107+
let mount_dom = update_dom;
106108

107109
// special cases
108110
switch (this.node.name) {
@@ -122,13 +124,19 @@ export default class BindingWrapper {
122124

123125
case 'textContent':
124126
update_conditions.push(x`${this.snippet} !== ${parent.var}.textContent`);
127+
mount_conditions.push(x`${this.snippet} !== void 0`);
125128
break;
126129

127130
case 'innerHTML':
128131
update_conditions.push(x`${this.snippet} !== ${parent.var}.innerHTML`);
132+
mount_conditions.push(x`${this.snippet} !== void 0`);
129133
break;
130134

131135
case 'currentTime':
136+
update_conditions.push(x`!@_isNaN(${this.snippet})`);
137+
mount_dom = null;
138+
break;
139+
132140
case 'playbackRate':
133141
case 'volume':
134142
update_conditions.push(x`!@_isNaN(${this.snippet})`);
@@ -142,12 +150,14 @@ export default class BindingWrapper {
142150

143151
update_conditions.push(x`${last} !== (${last} = ${this.snippet})`);
144152
update_dom = b`${parent.var}[${last} ? "pause" : "play"]();`;
153+
mount_dom = null;
145154
break;
146155
}
147156

148157
case 'value':
149158
if (parent.node.get_static_attribute_value('type') === 'file') {
150159
update_dom = null;
160+
mount_dom = null;
151161
}
152162
}
153163

@@ -165,13 +175,18 @@ export default class BindingWrapper {
165175
}
166176
}
167177

168-
if (this.node.name === 'innerHTML' || this.node.name === 'textContent') {
169-
block.chunks.mount.push(b`
170-
if (${this.snippet} !== void 0) {
171-
${update_dom}
172-
}`);
173-
} else if (!/(currentTime|paused)/.test(this.node.name)) {
174-
block.chunks.mount.push(update_dom);
178+
if (mount_dom) {
179+
if (mount_conditions.length > 0) {
180+
const condition = mount_conditions.reduce((lhs, rhs) => x`${lhs} && ${rhs}`);
181+
182+
block.chunks.mount.push(b`
183+
if (${condition}) {
184+
${mount_dom}
185+
}
186+
`);
187+
} else {
188+
block.chunks.mount.push(mount_dom);
189+
}
175190
}
176191
}
177192
}

0 commit comments

Comments
 (0)