set context in await blocks - fixes #2443

pull/3442/head
Rich Harris 5 years ago
parent 860040a7fa
commit 0f65b6cc9d

@ -1,6 +1,7 @@
import { assign, is_promise } from './utils';
import { check_outros, group_outros, transition_in, transition_out } from './transitions';
import { flush } from './scheduler';
import { get_current_component, set_current_component } from './lifecycle';
export function handle_promise(promise, info) {
const token = info.token = {};
@ -40,10 +41,15 @@ export function handle_promise(promise, info) {
}
if (is_promise(promise)) {
const current_component = get_current_component();
promise.then(value => {
set_current_component(current_component);
update(info.then, 1, info.value, value);
set_current_component(null);
}, error => {
set_current_component(current_component);
update(info.catch, 2, info.error, error);
set_current_component(null);
});
// if we previously had a then/catch block, destroy it

@ -6,7 +6,7 @@ export function set_current_component(component) {
current_component = component;
}
function get_current_component() {
export function get_current_component() {
if (!current_component) throw new Error(`Function called outside component initialization`);
return current_component;
}

@ -0,0 +1,6 @@
<script>
import { getContext } from 'svelte';
const num = getContext('test');
</script>
<p>Context value: {num}</p>

@ -0,0 +1,13 @@
export default {
html: `
<p>...waiting</p>
`,
async test({ assert, component, target }) {
await component.promise;
assert.htmlEqual(target.innerHTML, `
<p>Context value: 123</p>
`);
}
};

@ -0,0 +1,14 @@
<script>
import { setContext } from 'svelte';
import Child from './Child.svelte';
setContext('test', 123);
export let promise = Promise.resolve();
</script>
{#await promise}
<p>...waiting</p>
{:then}
<Child />
{/await}
Loading…
Cancel
Save