From 1c2ea7541606e7fbea86cbdc31cb3ce836f83c30 Mon Sep 17 00:00:00 2001 From: ComputerGuy <63362464+Ocean-OS@users.noreply.github.com> Date: Fri, 29 Aug 2025 18:26:48 -0700 Subject: [PATCH] make it work in dev --- .../phases/3-transform/client/types.d.ts | 1 + .../3-transform/client/visitors/Program.js | 9 +++++-- .../client/visitors/VariableDeclaration.js | 24 ++++++++++++++----- .../svelte/src/internal/client/dev/tracing.js | 11 +++++++++ packages/svelte/src/internal/client/index.js | 3 ++- .../src/internal/client/reactivity/async.js | 2 ++ 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts b/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts index 9a569f3a9f..e67b14e624 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts +++ b/packages/svelte/src/compiler/phases/3-transform/client/types.d.ts @@ -97,6 +97,7 @@ export interface ParallelizedChunk { /** index in instance body */ position: number; bindings: Binding[]; + type: 'promise_all' | 'all'; } export type Context = import('zimmerframe').Context; diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js index 72bb7828e0..12a8f71c6c 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js @@ -152,12 +152,17 @@ export function Program(node, context) { chunk.position + offset, 0, b.declaration(chunk.kind, [ - b.declarator(declarator.id, b.call(b.await(b.call('$.save', declarator.init)))) + b.declarator( + declarator.id, + chunk.type === 'promise_all' + ? b.await(declarator.init) + : b.call(b.await(b.call('$.save', declarator.init))) + ) ]) ); } else { const pattern = b.array_pattern(chunk.declarators.map(({ id }) => id)); - const init = b.call('$.all', ...chunk.declarators.map(({ init }) => init)); + const init = b.call(`$.${chunk.type}`, ...chunk.declarators.map(({ init }) => init)); body.splice( chunk.position + offset, 0, diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js index db7e909164..49f9e721f8 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js @@ -71,7 +71,8 @@ export function VariableDeclaration(node, context) { ]; if ( context.state.current_parallelized_chunk && - context.state.current_parallelized_chunk.kind === node.kind + context.state.current_parallelized_chunk.kind === node.kind && + context.state.current_parallelized_chunk.type === 'all' ) { context.state.current_parallelized_chunk.declarators.push(...declarators); context.state.current_parallelized_chunk.bindings.push(...bindings); @@ -79,11 +80,13 @@ export function VariableDeclaration(node, context) { context.path.at(-1) ).body.indexOf(node); } else { + /** @type {ParallelizedChunk} */ const chunk = { kind: node.kind, declarators, position: /** @type {Program} */ (context.path.at(-1)).body.indexOf(node), - bindings + bindings, + type: 'all' }; context.state.current_parallelized_chunk = chunk; context.state.parallelized_chunks.push(chunk); @@ -257,7 +260,6 @@ export function VariableDeclaration(node, context) { is_async && context.state.analysis.instance && context.state.scope === context.state.analysis.instance.scope && - !dev && // TODO make it work without this declarator.id.type === 'Identifier' ) { @@ -266,6 +268,7 @@ export function VariableDeclaration(node, context) { ...context.state.scope.get_bindings(declarator) ]); } + const type = parallelize ? (dev ? 'promise_all' : 'all') : null; /** @type {VariableDeclarator[]} */ const derived_declarators = []; @@ -289,7 +292,13 @@ export function VariableDeclaration(node, context) { ); if (!parallelize) call = b.call(b.await(b.call('$.save', call))); - if (dev) call = b.call('$.tag', call, b.literal(declarator.id.name)); + if (dev) { + call = b.call( + '$.tag' + (parallelize ? '_async' : ''), + call, + b.literal(declarator.id.name) + ); + } bindings.push(/** @type {Binding} */ (context.state.scope.get(declarator.id.name))); derived_declarators.push(b.declarator(declarator.id, call)); } else { @@ -373,7 +382,8 @@ export function VariableDeclaration(node, context) { })); if ( context.state.current_parallelized_chunk && - context.state.current_parallelized_chunk.kind === node.kind + context.state.current_parallelized_chunk.kind === node.kind && + context.state.current_parallelized_chunk.type === type ) { context.state.current_parallelized_chunk.declarators.push(...declarators); context.state.current_parallelized_chunk.bindings.push(...bindings); @@ -381,11 +391,13 @@ export function VariableDeclaration(node, context) { context.path.at(-1) ).body.indexOf(node); } else { + /** @type {ParallelizedChunk} */ const chunk = { kind: node.kind, declarators, position: /** @type {Program} */ (context.path.at(-1)).body.indexOf(node), - bindings + bindings, + type: /** @type {ParallelizedChunk['type']} */ (type) }; context.state.current_parallelized_chunk = chunk; context.state.parallelized_chunks.push(chunk); diff --git a/packages/svelte/src/internal/client/dev/tracing.js b/packages/svelte/src/internal/client/dev/tracing.js index 673a710fac..f66e0a8106 100644 --- a/packages/svelte/src/internal/client/dev/tracing.js +++ b/packages/svelte/src/internal/client/dev/tracing.js @@ -184,6 +184,17 @@ export function tag(source, label) { return source; } +/** + * @template T + * @param {Promise>} promise + * @param {string} label + * @returns {Promise>} + */ +export async function tag_async(promise, label) { + const source = await promise; + return tag(source, label); +} + /** * @param {unknown} value * @param {string} label diff --git a/packages/svelte/src/internal/client/index.js b/packages/svelte/src/internal/client/index.js index c88114de01..9b45e264eb 100644 --- a/packages/svelte/src/internal/client/index.js +++ b/packages/svelte/src/internal/client/index.js @@ -7,7 +7,7 @@ export { add_locations } from './dev/elements.js'; export { hmr } from './dev/hmr.js'; export { create_ownership_validator } from './dev/ownership.js'; export { check_target, legacy_api } from './dev/legacy.js'; -export { trace, tag, tag_proxy } from './dev/tracing.js'; +export { trace, tag, tag_async, tag_proxy } from './dev/tracing.js'; export { inspect } from './dev/inspect.js'; export { async } from './dom/blocks/async.js'; export { validate_snippet_args } from './dev/validation.js'; @@ -102,6 +102,7 @@ export { all, async_body, for_await_track_reactivity_loss, + promise_all, save, track_reactivity_loss } from './reactivity/async.js'; diff --git a/packages/svelte/src/internal/client/reactivity/async.js b/packages/svelte/src/internal/client/reactivity/async.js index 181c33e9b9..c0e081b017 100644 --- a/packages/svelte/src/internal/client/reactivity/async.js +++ b/packages/svelte/src/internal/client/reactivity/async.js @@ -202,3 +202,5 @@ export function all(...promises) { ) ); } + +export const promise_all = Promise.all;