Skip to content

Commit b975914

Browse files
committed
failing test + fix
1 parent 14a4f6c commit b975914

File tree

9 files changed

+70
-26
lines changed

9 files changed

+70
-26
lines changed

packages/svelte/src/internal/client/async_body.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/svelte/src/internal/client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export { createAttachmentKey as attachment } from '../../attachments/index.js';
22
export { FILENAME, HMR, NAMESPACE_SVG } from '../../constants.js';
3-
export { async_body } from './async_body.js';
43
export { push, pop, add_svelte_meta } from './context.js';
54
export { assign, assign_and, assign_or, assign_nullish } from './dev/assign.js';
65
export { cleanup_styles } from './dev/css.js';
@@ -100,6 +99,7 @@ export {
10099
with_script
101100
} from './dom/template.js';
102101
export {
102+
async_body,
103103
for_await_track_reactivity_loss,
104104
save,
105105
track_reactivity_loss

packages/svelte/src/internal/client/reactivity/async.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import {
1111
set_active_effect,
1212
set_active_reaction
1313
} from '../runtime.js';
14-
import { current_batch } from './batch.js';
14+
import { current_batch, suspend } from './batch.js';
1515
import {
1616
async_derived,
1717
current_async_effect,
1818
derived,
1919
derived_safe_equal,
2020
set_from_async_derived
2121
} from './deriveds.js';
22+
import { aborted } from './effects.js';
2223

2324
/**
2425
*
@@ -170,3 +171,21 @@ export function unset_context() {
170171
set_component_context(null);
171172
if (DEV) set_from_async_derived(null);
172173
}
174+
175+
/**
176+
* @param {() => Promise<void>} fn
177+
*/
178+
export async function async_body(fn) {
179+
const unsuspend = suspend();
180+
const active = /** @type {Effect} */ (active_effect);
181+
182+
try {
183+
await fn();
184+
} catch (error) {
185+
if (!aborted(active)) {
186+
invoke_error_boundary(error, active);
187+
}
188+
} finally {
189+
unsuspend();
190+
}
191+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import { route } from "./main.svelte";
3+
4+
await new Promise(async (_, reject) => {
5+
await Promise.resolve();
6+
route.current = 'other'
7+
route.reject = reject;
8+
});
9+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: `<button>reject</button> <p>pending</p>`,
6+
7+
async test({ assert, target }) {
8+
const [reject] = target.querySelectorAll('button');
9+
10+
await tick();
11+
reject.click();
12+
await tick();
13+
assert.htmlEqual(target.innerHTML, '<button>reject</button> <p>route: other</p>');
14+
}
15+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script module>
2+
import Child from './Child.svelte';
3+
export let route = $state({ current: 'home' });
4+
</script>
5+
6+
<button onclick={() => route.reject()}>reject</button>
7+
8+
<svelte:boundary>
9+
{#if route.current === 'home'}
10+
<Child />
11+
{:else}
12+
<p>route: {route.current}</p>
13+
{/if}
14+
15+
{#snippet pending()}
16+
<p>pending</p>
17+
{/snippet}
18+
</svelte:boundary>

packages/svelte/tests/runtime-runes/samples/async-top-level-error-nested/Child.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import { route } from "./main.svelte";
33
44
await new Promise(async (_, reject) => {
5-
await Promise.resolve();
6-
route.current = 'other'
75
route.reject = reject;
86
});
97
</script>

packages/svelte/tests/runtime-runes/samples/async-top-level-error-nested/_config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ export default test({
77
async test({ assert, target }) {
88
const [reject] = target.querySelectorAll('button');
99

10-
await tick();
1110
reject.click();
1211
await tick();
13-
assert.htmlEqual(target.innerHTML, '<button>reject</button> <p>route: other</p>');
12+
assert.htmlEqual(target.innerHTML, '<button>reject</button> <p>failed</p>');
1413
}
1514
});
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<script module>
22
import Child from './Child.svelte';
3-
export let route = $state({ current: 'home' });
3+
export let route = $state({});
44
</script>
55

66
<button onclick={() => route.reject()}>reject</button>
77

88
<svelte:boundary>
9-
{#if route.current === 'home'}
10-
<Child />
11-
{:else}
12-
<p>route: {route.current}</p>
13-
{/if}
9+
<Child />
1410

1511
{#snippet pending()}
1612
<p>pending</p>
1713
{/snippet}
14+
15+
{#snippet failed()}
16+
<p>failed</p>
17+
{/snippet}
1818
</svelte:boundary>

0 commit comments

Comments
 (0)