Skip to content

Commit b76f074

Browse files
authoredJun 8, 2018
Merge pull request #1529 from sveltejs/gh-1527
avoid unnecessary remounts
2 parents f1cfa55 + 3b928b3 commit b76f074

File tree

7 files changed

+66
-39
lines changed

7 files changed

+66
-39
lines changed
 

‎src/compile/dom/Block.ts

+21-35
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ export default class Block {
142142
}
143143

144144
addVariable(name: string, init?: string) {
145+
if (name[0] === '#') {
146+
name = this.alias(name.slice(1));
147+
}
148+
145149
if (this.variables.has(name) && this.variables.get(name) !== init) {
146150
throw new Error(
147151
`Variable '${name}' already initialised with a different value`
@@ -166,18 +170,16 @@ export default class Block {
166170
toString() {
167171
const { dev } = this.compiler.options;
168172

169-
let introing;
170-
const hasIntros = !this.builders.intro.isEmpty();
171-
if (hasIntros) {
172-
introing = this.getUniqueName('introing');
173-
this.addVariable(introing);
174-
}
173+
if (this.hasIntroMethod || this.hasOutroMethod) {
174+
this.addVariable('#current');
175175

176-
let outroing;
177-
const hasOutros = !this.builders.outro.isEmpty();
178-
if (hasOutros) {
179-
outroing = this.alias('outroing');
180-
this.addVariable(outroing);
176+
if (!this.builders.mount.isEmpty()) {
177+
this.builders.mount.addLine(`#current = true;`);
178+
}
179+
180+
if (!this.builders.outro.isEmpty()) {
181+
this.builders.outro.addLine(`#current = false;`);
182+
}
181183
}
182184

183185
if (this.autofocus) {
@@ -275,46 +277,30 @@ export default class Block {
275277
}
276278

277279
if (this.hasIntroMethod || this.hasOutroMethod) {
278-
if (hasIntros) {
280+
if (this.builders.mount.isEmpty()) {
281+
properties.addBlock(`i: @noop,`);
282+
} else {
279283
properties.addBlock(deindent`
280284
${dev ? 'i: function intro' : 'i'}(#target, anchor) {
281-
if (${introing}) return;
282-
${introing} = true;
283-
${hasOutros && `${outroing} = false;`}
284-
285+
if (#current) return;
285286
${this.builders.intro}
286-
287287
this.m(#target, anchor);
288288
},
289289
`);
290-
} else {
291-
if (this.builders.mount.isEmpty()) {
292-
properties.addBlock(`i: @noop,`);
293-
} else {
294-
properties.addBlock(deindent`
295-
${dev ? 'i: function intro' : 'i'}(#target, anchor) {
296-
this.m(#target, anchor);
297-
},
298-
`);
299-
}
300290
}
301291

302-
if (hasOutros) {
292+
if (this.builders.outro.isEmpty()) {
293+
properties.addBlock(`o: @run,`);
294+
} else {
303295
properties.addBlock(deindent`
304296
${dev ? 'o: function outro' : 'o'}(#outrocallback) {
305-
if (${outroing}) return;
306-
${outroing} = true;
307-
${hasIntros && `${introing} = false;`}
297+
if (!#current) return;
308298
309299
${this.outros > 1 && `#outrocallback = @callAfter(#outrocallback, ${this.outros});`}
310300
311301
${this.builders.outro}
312302
},
313303
`);
314-
} else {
315-
properties.addBlock(deindent`
316-
o: @run,
317-
`);
318304
}
319305
}
320306

‎src/compile/nodes/Attribute.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default class Attribute extends Node {
232232
if (this.dependencies.size || isSelectValueAttribute) {
233233
const dependencies = Array.from(this.dependencies);
234234
const changedCheck = (
235-
( block.hasOutros ? `#outroing || ` : '' ) +
235+
(block.hasOutros ? `!#current || ` : '') +
236236
dependencies.map(dependency => `changed.${dependency}`).join(' || ')
237237
);
238238

@@ -308,7 +308,7 @@ export default class Attribute extends Node {
308308
if (propDependencies.size) {
309309
const dependencies = Array.from(propDependencies);
310310
const condition = (
311-
(block.hasOutros ? `#outroing || ` : '') +
311+
(block.hasOutros ? `!#current || ` : '') +
312312
dependencies.map(dependency => `changed.${dependency}`).join(' || ')
313313
);
314314

‎src/compile/nodes/Title.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default class Title extends Node {
8181
if (allDependencies.size) {
8282
const dependencies = Array.from(allDependencies);
8383
const changedCheck = (
84-
( block.hasOutros ? `#outroing || ` : '' ) +
84+
( block.hasOutros ? `!#current || ` : '' ) +
8585
dependencies.map(dependency => `changed.${dependency}`).join(' || ')
8686
);
8787

‎src/compile/nodes/shared/Tag.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default class Tag extends Node {
3535

3636
if (dependencies.size) {
3737
const changedCheck = (
38-
(block.hasOutros ? `#outroing || ` : '') +
38+
(block.hasOutros ? `!#current || ` : '') +
3939
[...dependencies].map((dependency: string) => `changed.${dependency}`).join(' || ')
4040
);
4141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span><slot></slot></span>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default {
2+
data: {
3+
x: true,
4+
value: 'one'
5+
},
6+
7+
html: `
8+
<div>
9+
<input>
10+
<span>x</span>
11+
</div>
12+
`,
13+
14+
nestedTransitions: true,
15+
16+
test(assert, component, target, window, raf) {
17+
const div = target.querySelector('div');
18+
const { appendChild, insertBefore } = div;
19+
20+
div.appendChild = div.insertBefore = () => {
21+
throw new Error('DOM was mutated');
22+
};
23+
24+
component.set({ value: 'two' });
25+
},
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div>
2+
{#if x}
3+
<input on:input="set({ value: this.value })">
4+
<Span>x</Span>
5+
{/if}
6+
</div>
7+
8+
<script>
9+
export default {
10+
components: {
11+
Span: './Span.html'
12+
}
13+
};
14+
</script>

0 commit comments

Comments
 (0)