Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition in svelte:element with transition #7948 #7949

Merged
merged 10 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,16 @@ export default class ElementWrapper extends Wrapper {
const has_transitions = !!(this.node.intro || this.node.outro);
const not_equal = this.renderer.component.component_options.immutable ? x`@not_equal` : x`@safe_not_equal`;

const tag_will_be_removed = block.get_unique_name('tag_will_be_removed');
if (has_transitions) {
block.add_variable(tag_will_be_removed, x`false`);
}

block.chunks.update.push(b`
if (${tag}) {
if (!${previous_tag}) {
${this.var} = ${this.child_dynamic_element_block.name}(#ctx);
${previous_tag} = ${tag};
${this.var}.c();
${has_transitions && b`@transition_in(${this.var})`}
${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor});
Expand All @@ -327,27 +333,39 @@ export default class ElementWrapper extends Wrapper {
${this.renderer.options.dev && b`@validate_dynamic_element(${tag});`}
${this.renderer.options.dev && this.node.children.length > 0 && b`@validate_void_dynamic_element(${tag});`}
${this.var} = ${this.child_dynamic_element_block.name}(#ctx);
${previous_tag} = ${tag};
${this.var}.c();
${has_transitions && b`if (${tag_will_be_removed}) {
${tag_will_be_removed} = false;
@transition_in(${this.var})
}`}
${this.var}.m(${this.get_update_mount_node(anchor)}, ${anchor});
} else {
${has_transitions && b`if (${tag_will_be_removed}) {
${tag_will_be_removed} = false;
@transition_in(${this.var})
}`}
${this.var}.p(#ctx, #dirty);
}
} else if (${previous_tag}) {
${has_transitions
? b`
${tag_will_be_removed} = true;
@group_outros();
@transition_out(${this.var}, 1, 1, () => {
${this.var} = null;
${previous_tag} = ${tag};
${tag_will_be_removed} = false;
});
@check_outros();
`
: b`
${this.var}.d(1);
${this.var} = null;
${previous_tag} = ${tag};
`
}
}
${previous_tag} = ${tag};
`);

if (this.child_dynamic_element_block.has_intros) {
Expand Down
7 changes: 4 additions & 3 deletions test/js/samples/svelte-element-event-handlers/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ function create_fragment(ctx) {
if (a) {
if (!previous_tag) {
svelte_element = create_dynamic_element(ctx);
previous_tag = a;
svelte_element.c();
svelte_element.m(svelte_element_anchor.parentNode, svelte_element_anchor);
} else if (safe_not_equal(previous_tag, a)) {
svelte_element.d(1);
svelte_element = create_dynamic_element(ctx);
previous_tag = a;
svelte_element.c();
svelte_element.m(svelte_element_anchor.parentNode, svelte_element_anchor);
} else {
Expand All @@ -125,9 +127,8 @@ function create_fragment(ctx) {
} else if (previous_tag) {
svelte_element.d(1);
svelte_element = null;
previous_tag = a;
}

previous_tag = a;
},
i: noop,
o: noop,
Expand Down Expand Up @@ -168,4 +169,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
7 changes: 4 additions & 3 deletions test/js/samples/svelte-element-svg/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ function create_fragment(ctx) {
if (/*tag*/ ctx[0].svg) {
if (!previous_tag) {
svelte_element = create_dynamic_element(ctx);
previous_tag = /*tag*/ ctx[0].svg;
svelte_element.c();
svelte_element.m(svelte_element_anchor.parentNode, svelte_element_anchor);
} else if (safe_not_equal(previous_tag, /*tag*/ ctx[0].svg)) {
svelte_element.d(1);
svelte_element = create_dynamic_element(ctx);
previous_tag = /*tag*/ ctx[0].svg;
svelte_element.c();
svelte_element.m(svelte_element_anchor.parentNode, svelte_element_anchor);
} else {
Expand All @@ -85,9 +87,8 @@ function create_fragment(ctx) {
} else if (previous_tag) {
svelte_element.d(1);
svelte_element = null;
previous_tag = /*tag*/ ctx[0].svg;
}

previous_tag = /*tag*/ ctx[0].svg;
},
i: noop,
o: noop,
Expand All @@ -110,4 +111,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
19 changes: 19 additions & 0 deletions test/runtime/samples/component-transition/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
async test({ assert, target, raf }) {
const button = target.querySelector('#button');
const container = target.querySelector('#container');

// Multiple click on button
await button.dispatchEvent(new window.MouseEvent('click'));
await button.dispatchEvent(new window.MouseEvent('click'));
await button.dispatchEvent(new window.MouseEvent('click'));
await button.dispatchEvent(new window.MouseEvent('click'));
await button.dispatchEvent(new window.MouseEvent('click'));
await button.dispatchEvent(new window.MouseEvent('click'));
await button.dispatchEvent(new window.MouseEvent('click'));

assert.equal(container.children.length, 1);
raf.tick(501);
assert.equal(container.children.length, 0);
}
};
13 changes: 13 additions & 0 deletions test/runtime/samples/component-transition/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { slide } from 'svelte/transition';
let tag = 'div';
function toggle() {
tag = (tag) ? null : 'div';
}
</script>

<button id="button" on:click={toggle}>toggle</button> TAG={tag}

<div id="container">
<svelte:element this={tag} transition:slide={{duration:500}}>CONTENT</svelte:element>
</div>