fix: ensure batch for async work in effects

pull/18800/head
svelte-triage-bot 2 days ago
parent 5895c637b0
commit b3f704759c

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: prevent async component batch crash

@ -388,7 +388,7 @@ export function wait(blockers) {
export function increment_pending() { export function increment_pending() {
var effect = /** @type {Effect} */ (active_effect); var effect = /** @type {Effect} */ (active_effect);
var boundary = effect.b; // undefined if called outside the render tree, e.g. a standalone $effect.root var boundary = effect.b; // undefined if called outside the render tree, e.g. a standalone $effect.root
var batch = /** @type {Batch} */ (current_batch); var batch = Batch.ensure();
var blocking = !!boundary?.is_rendered(); var blocking = !!boundary?.is_rendered();
boundary?.update_pending_count(1, batch); boundary?.update_pending_count(1, batch);

@ -0,0 +1,5 @@
<script>
const value = $derived(await Promise.resolve('child'));
</script>
<p>{value}</p>

@ -0,0 +1,10 @@
import { tick } from 'svelte';
import { test } from '../../test';
export default test({
async test({ assert, target }) {
target.querySelector('button')?.click();
await tick();
assert.htmlEqual(target.innerHTML, '<button>run</button><p>child</p><span></span>');
}
});

@ -0,0 +1,13 @@
<script>
import Child from './Child.svelte';
let anchor;
let run = $state(false);
$effect(() => {
if (run) Child(anchor, {});
});
</script>
<button onclick={() => (run = true)}>run</button>
<span bind:this={anchor}></span>
Loading…
Cancel
Save