fix: allow `$derived(await ...)` in disconnected effect roots (#18273)

If `$effect.root` is created outside the component tree, the boundary
property is null

Fixes #18188
pull/18087/merge
Simon H 2 months ago committed by GitHub
parent f023e8d7c8
commit fe9ab936b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: allow `$derived(await ...)` in disconnected effect roots

@ -187,7 +187,10 @@ export function async_derived(fn, label, location) {
var decrement_pending = increment_pending();
}
if (/** @type {Boundary} */ (parent.b).is_rendered()) {
if (
// boundary can be null if the async derived is inside an $effect.root not connected to the component render tree
parent.b?.is_rendered()
) {
batch.async_deriveds.get(effect)?.reject(OBSOLETE);
} else {
// While the boundary is still showing pending, a new run supersedes all older in-flight runs

@ -0,0 +1,9 @@
import { test } from '../../test';
export default test({
// Test that an async derived inside an $effect.root not connected to the component tree still works
async test({ assert, logs }) {
await new Promise((resolve) => setTimeout(resolve, 10));
assert.deepEqual(logs, [1]);
}
});

@ -0,0 +1,11 @@
<script>
setTimeout(() => {
$effect.root(() => {
async function fn() {
const value = $derived(await 1);
return { get value() { return value } };
}
fn().then(r => console.log(r.value));
})
})
</script>
Loading…
Cancel
Save