diff --git a/packages/svelte/src/internal/client/dom/blocks/async.js b/packages/svelte/src/internal/client/dom/blocks/async.js index 13116c50fe..db6a7fda79 100644 --- a/packages/svelte/src/internal/client/dom/blocks/async.js +++ b/packages/svelte/src/internal/client/dom/blocks/async.js @@ -18,13 +18,27 @@ export function async(node, expressions, fn) { var restore = capture(); - Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => { - restore(); - fn(node, ...result); + let boundary = effect.b; + + while (boundary !== null && !boundary.has_pending_snippet()) { + boundary = boundary.parent; + } + + if (boundary === null) { + throw new Error('TODO cannot create async derived outside a boundary with a pending snippet'); + } + + boundary.increment(); - // TODO is this necessary? + Promise.all(expressions.map((fn) => async_derived(fn))).then((result) => { batch.run(() => { + restore(); + fn(node, ...result); + + // TODO is this necessary? schedule_effect(effect); }); + + boundary.decrement(); }); } diff --git a/packages/svelte/src/internal/client/dom/blocks/boundary.js b/packages/svelte/src/internal/client/dom/blocks/boundary.js index 95db4dfefc..a98a354bd0 100644 --- a/packages/svelte/src/internal/client/dom/blocks/boundary.js +++ b/packages/svelte/src/internal/client/dom/blocks/boundary.js @@ -119,10 +119,13 @@ export class Boundary { return branch(() => this.#children(this.#anchor)); }); - if (this.#pending_count === 0) { + if (this.#pending_count > 0) { + this.#show_pending_snippet(); + } else { pause_effect(/** @type {Effect} */ (this.#pending_effect), () => { this.#pending_effect = null; }); + this.ran = true; } }); } else { @@ -130,14 +133,14 @@ export class Boundary { if (this.#pending_count > 0) { this.#show_pending_snippet(); + } else { + this.ran = true; } } reset_is_throwing_error(); }, flags); - this.ran = true; - if (hydrating) { this.#anchor = hydrate_node; } @@ -189,6 +192,8 @@ export class Boundary { } commit() { + this.ran = true; + if (this.#pending_effect) { pause_effect(this.#pending_effect, () => { this.#pending_effect = null; @@ -242,10 +247,10 @@ export class Boundary { } }); - this.ran = true; - if (this.#pending_count > 0) { this.#show_pending_snippet(); + } else { + this.ran = true; } };