Skip to content

Commit

Permalink
fix not passing child_ctx for catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Aug 12, 2020
1 parent a1cb70d commit 037c755
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/compiler/compile/render_dom/wrappers/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ export default class AwaitBlockWrapper extends Wrapper {

const dependencies = this.node.expression.dynamic_dependencies();

let update_child_context;
if (this.then.value && this.catch.value) {
update_child_context = b`#child_ctx[${this.then.value_index}] = #child_ctx[${this.catch.value_index}] = ${info}.resolved;`;
} else if (this.then.value) {
update_child_context = b`#child_ctx[${this.then.value_index}] = ${info}.resolved;`;
} else if (this.catch.value) {
update_child_context = b`#child_ctx[${this.catch.value_index}] = ${info}.resolved;`;
}

if (dependencies.length > 0) {
const condition = x`
${block.renderer.dirty(dependencies)} &&
Expand All @@ -247,7 +256,7 @@ export default class AwaitBlockWrapper extends Wrapper {
} else {
const #child_ctx = #ctx.slice();
${this.then.value && b`#child_ctx[${this.then.value_index}] = ${info}.resolved;`}
${update_child_context}
${info}.block.p(#child_ctx, #dirty);
}
`);
Expand All @@ -261,7 +270,7 @@ export default class AwaitBlockWrapper extends Wrapper {
block.chunks.update.push(b`
{
const #child_ctx = #ctx.slice();
${this.then.value && b`#child_ctx[${this.then.value_index}] = ${info}.resolved;`}
${update_child_context}
${info}.block.p(#child_ctx, #dirty);
}
`);
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/await-with-update-2/Component.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let count;
export let value;
</script>

<div>count: {count}</div>
<div>value: {value}</div>
64 changes: 64 additions & 0 deletions test/runtime/samples/await-with-update-2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export default {
props: {
thePromise: new Promise((_) => {}),
count: 0,
},

html: `
<div><p>loading...</p></div>
`,

async test({ assert, component, target }) {
await (component.thePromise = Promise.resolve({ value: "success", Component: component.Component }));

assert.htmlEqual(
target.innerHTML,
`
<div>Resolved:
<div>count: 0</div>
<div>value: success</div>
</div>
`
);

component.count = 5;

assert.htmlEqual(
target.innerHTML,
`
<div>Resolved:
<div>count: 5</div>
<div>value: success</div>
</div>
`
);

try {
await (component.thePromise = Promise.reject({ value: "failure", Component: component.Component }));
} catch {
// ignore
}

assert.htmlEqual(
target.innerHTML,
`
<div>Rejected:
<div>count: 5</div>
<div>value: failure</div>
</div>
`
);

component.count = 10;

assert.htmlEqual(
target.innerHTML,
`
<div>Rejected:
<div>count: 10</div>
<div>value: failure</div>
</div>
`
);
},
};
16 changes: 16 additions & 0 deletions test/runtime/samples/await-with-update-2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import Component from './Component.svelte';
export let thePromise;
export let count;
export { Component }
</script>

<div>
{#await thePromise}
<p>loading...</p>
{:then { value: theValue, Component }}
Resolved: <svelte:component this={Component} {count} value={theValue} />
{:catch { value: theError, Component } }
Rejected: <svelte:component this={Component} {count} value={theError} />
{/await}
</div>
5 changes: 5 additions & 0 deletions test/runtime/samples/await-with-update/Component.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let count;
</script>

<div>count: {count}</div>
60 changes: 60 additions & 0 deletions test/runtime/samples/await-with-update/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export default {
props: {
thePromise: new Promise((_) => {}),
count: 0,
},

html: `
<div><p>loading...</p></div>
`,

async test({ assert, component, target }) {
await (component.thePromise = Promise.resolve(component.Component));

assert.htmlEqual(
target.innerHTML,
`
<div>Resolved:
<div>count: 0</div>
</div>
`
);

component.count = 5;

assert.htmlEqual(
target.innerHTML,
`
<div>Resolved:
<div>count: 5</div>
</div>
`
);

try {
await (component.thePromise = Promise.reject(component.Component));
} catch {
// ignore
}

assert.htmlEqual(
target.innerHTML,
`
<div>Rejected:
<div>count: 5</div>
</div>
`
);

component.count = 10;

assert.htmlEqual(
target.innerHTML,
`
<div>Rejected:
<div>count: 10</div>
</div>
`
);
},
};
16 changes: 16 additions & 0 deletions test/runtime/samples/await-with-update/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import Component from './Component.svelte';
export let thePromise;
export let count;
export { Component }
</script>

<div>
{#await thePromise}
<p>loading...</p>
{:then theValue}
Resolved: <svelte:component this={theValue} {count} />
{:catch theError}
Rejected: <svelte:component this={theError} {count} />
{/await}
</div>

0 comments on commit 037c755

Please sign in to comment.