0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

abort transition on detach - alternative fix for #1561

This commit is contained in:
Rich Harris 2018-06-29 15:04:45 -04:00
parent bec49a0ad0
commit f3e4f04a54
4 changed files with 46 additions and 5 deletions

View File

@ -712,6 +712,8 @@ export default class Element extends Node {
${name} = null;
});
`);
block.builders.destroy.addConditional('detach', `if (${name}) ${name}.abort();`);
} else {
const introName = intro && block.getUniqueName(`${this.var}_intro`);
const outroName = outro && block.getUniqueName(`${this.var}_outro`);
@ -726,8 +728,8 @@ export default class Element extends Node {
if (outro) {
block.builders.intro.addBlock(deindent`
if (${introName}) ${introName}.abort();
if (${outroName}) ${outroName}.abort();
if (${introName}) ${introName}.abort(1);
if (${outroName}) ${outroName}.abort(1);
`);
}
@ -748,7 +750,7 @@ export default class Element extends Node {
const fn = `%transitions-${outro.name}`;
block.builders.intro.addBlock(deindent`
if (${outroName}) ${outroName}.abort();
if (${outroName}) ${outroName}.abort(1);
`);
// TODO hide elements that have outro'd (unless they belong to a still-outroing
@ -757,6 +759,8 @@ export default class Element extends Node {
${outroName} = @wrapTransition(#component, ${this.var}, ${fn}, ${snippet}, false);
${outroName}.run(0, #outrocallback);
`);
block.builders.destroy.addConditional('detach', `if (${outroName}) ${outroName}.abort();`);
}
}
}

View File

@ -148,9 +148,9 @@ export function wrapTransition(component, node, fn, params, intro) {
this.running = !!this.pending;
},
abort() {
abort(reset) {
if (this.program) {
if (obj.tick) obj.tick(1, 0);
if (reset && obj.tick) obj.tick(1, 0);
if (obj.css) transitionManager.deleteRule(node, this.program.name);
this.program = this.pending = null;
this.running = false;

View File

@ -0,0 +1,19 @@
export default {
skipIntroByDefault: true,
data: {
visible: true
},
test(assert, component, target, window, raf) {
component.set({ visible: false });
const div = target.querySelector('div');
raf.tick(50);
assert.equal(div.foo, 0.5);
component.destroy();
raf.tick(100);
},
};

View File

@ -0,0 +1,18 @@
{#if visible}
<div transition:foo>destroy me</div>
{/if}
<script>
export default {
transitions: {
foo(node, params) {
return {
duration: 100,
tick: t => {
node.foo = t;
}
};
}
}
};
</script>