Skip to content

Commit

Permalink
Merge pull request #3142 from sveltejs/gh-1496
Browse files Browse the repository at this point in the history
prevent await block mounting inside removed if block
  • Loading branch information
Rich-Harris committed Jul 1, 2019
2 parents 4b62fa2 + b7ba0d6 commit 8ffe03a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/compiler/compile/render_dom/wrappers/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default class AwaitBlockWrapper extends Wrapper {
const info_props = [
'ctx',
'current: null',
'token: null',
this.pending.block.name && `pending: ${this.pending.block.name}`,
this.then.block.name && `then: ${this.then.block.name}`,
this.catch.block.name && `catch: ${this.catch.block.name}`,
Expand Down Expand Up @@ -223,6 +224,7 @@ export default class AwaitBlockWrapper extends Wrapper {

block.builders.destroy.add_block(deindent`
${info}.block.d(${parent_node ? '' : 'detaching'});
${info}.token = null;
${info} = null;
`);

Expand Down
15 changes: 15 additions & 0 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ let compile = null;

const sveltePath = process.cwd().split('\\').join('/');

let unhandled_rejection = false;
process.on('unhandledRejection', err => {
unhandled_rejection = err;
});

describe("runtime", () => {
before(() => {
svelte = loadSvelte(false);
Expand Down Expand Up @@ -60,6 +65,8 @@ describe("runtime", () => {
throw new Error('skipping test, already failed');
}

unhandled_rejection = null;

compile = (config.preserveIdentifiers ? svelte : svelte$).compile;

const cwd = path.resolve(`test/runtime/samples/${dir}`);
Expand Down Expand Up @@ -165,10 +172,18 @@ describe("runtime", () => {
raf
})).then(() => {
component.$destroy();

if (unhandled_rejection) {
throw unhandled_rejection;
}
});
} else {
component.$destroy();
assert.htmlEqual(target.innerHTML, "");

if (unhandled_rejection) {
throw unhandled_rejection;
}
}
})
.catch(err => {
Expand Down
22 changes: 22 additions & 0 deletions test/runtime/samples/await-in-removed-if/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
let fulfil;

const promise = new Promise(f => {
fulfil = f;
});

export default {
props: {
promise
},

html: ``,

async test({ assert, component, target }) {
component.condition = false;

fulfil();
await new Promise(f => setTimeout(f, 0));

assert.htmlEqual(target.innerHTML, ``);
}
};
8 changes: 8 additions & 0 deletions test/runtime/samples/await-in-removed-if/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
export let promise;
export let condition = true;
</script>

{#if condition}
{#await promise then _}hello{/await}
{/if}

0 comments on commit 8ffe03a

Please sign in to comment.