diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js index 470e8035f5..fcbb59ba74 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.js @@ -25,13 +25,7 @@ export function IfBlock(node, context) { statements.push(b.var(alternate_id, b.arrow([b.id('$$anchor')], alternate))); } - // TODO helperise - const blockers = new Set(); - for (const d of node.metadata.expression.dependencies) { - if (d.blocker) blockers.add(d.blocker); - } - - const is_async = blockers.size > 0 || node.metadata.expression.has_await; + const is_async = node.metadata.expression.is_async(); const expression = build_expression(context, node.test, node.metadata.expression); const test = is_async ? b.call('$.get', b.id('$$condition')) : expression; @@ -84,7 +78,7 @@ export function IfBlock(node, context) { b.call( '$.async', context.state.node, - b.array([...blockers]), + node.metadata.expression.blockers(), b.array([b.thunk(expression, node.metadata.expression.has_await)]), b.arrow([context.state.node, b.id('$$condition')], b.block(statements)) ) diff --git a/packages/svelte/src/compiler/phases/nodes.js b/packages/svelte/src/compiler/phases/nodes.js index 5e88c0acb2..f275ddacf9 100644 --- a/packages/svelte/src/compiler/phases/nodes.js +++ b/packages/svelte/src/compiler/phases/nodes.js @@ -1,5 +1,6 @@ /** @import { Expression, PrivateIdentifier } from 'estree' */ /** @import { AST, Binding } from '#compiler' */ +import * as b from '#compiler/builders'; /** * All nodes that can appear elsewhere than the top level, have attributes and can contain children @@ -91,6 +92,29 @@ export class ExpressionMetadata { * @type {Set} */ references = new Set(); + + /** @type {null | Set} */ + #blockers = null; + + #get_blockers() { + if (!this.#blockers) { + this.#blockers = new Set(); + + for (const d of this.dependencies) { + if (d.blocker) this.#blockers.add(d.blocker); + } + } + + return this.#blockers; + } + + blockers() { + return b.array([...this.#get_blockers()]); + } + + is_async() { + return this.has_await || this.#get_blockers().size > 0; + } } export function new ExpressionMetadata() {