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

Fix race condition for if block

This commit is contained in:
Yury Zhuravlev 2018-07-06 19:02:12 +09:00
parent a043c4414a
commit 7cab338e32
4 changed files with 44 additions and 4 deletions

View File

@ -169,9 +169,11 @@ export default class Block {
toString() {
const { dev } = this.compiler.options;
const properties = new CodeBuilder();
if (this.hasIntroMethod || this.hasOutroMethod) {
this.addVariable('#current');
properties.addBlock(`cr(){ return #current; },`);
if (!this.builders.mount.isEmpty()) {
this.builders.mount.addLine(`#current = true;`);
@ -186,8 +188,6 @@ export default class Block {
this.builders.mount.addLine(`${this.autofocus}.focus();`);
}
const properties = new CodeBuilder();
let localKey;
if (this.key) {
localKey = this.getUniqueName('key');

View File

@ -301,7 +301,7 @@ export default class IfBlock extends Node {
const updateMountNode = this.getUpdateMountNode(anchor);
const destroyOldBlock = deindent`
@groupOutros();
if(${name}.cr()) @groupOutros();
${name}.o(function() {
${if_blocks}[${previous_block_index}].d(1);
${if_blocks}[${previous_block_index}] = null;
@ -423,7 +423,7 @@ export default class IfBlock extends Node {
// as that will typically result in glitching
const exit = branch.hasOutroMethod
? deindent`
@groupOutros();
if(${name}.cr()) @groupOutros();
${name}.o(function() {
${name}.d(1);
${name} = null;

View File

@ -0,0 +1,22 @@
export default {
test ( assert, component, target, window, raf ) {
component.set({ visible: true });
const div = target.querySelector('div');
component.set({ visible: false });
assert.equal(window.getComputedStyle(div).opacity, 1);
raf.tick(200);
assert.equal(window.getComputedStyle(div).opacity, 0.5);
component.set({ blabla: false });
raf.tick(400);
assert.equal(window.getComputedStyle(div).opacity, 0);
raf.tick(600);
assert.equal(component.refs.div, undefined);
assert.equal(target.querySelector('div'), undefined);
component.destroy();
}
};

View File

@ -0,0 +1,18 @@
{#if visible}
<div out:fade style="opacity: 1;" ref:div>yes</div>
{/if}
<script>
export default {
transitions: {
fade: function ( node, params ) {
return {
duration: 400,
tick: t => {
node.style.opacity = t;
}
};
}
}
};
</script>