Skip to content

Commit 1c2ea75

Browse files
committed
make it work in dev
1 parent 9ab08aa commit 1c2ea75

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

packages/svelte/src/compiler/phases/3-transform/client/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export interface ParallelizedChunk {
9797
/** index in instance body */
9898
position: number;
9999
bindings: Binding[];
100+
type: 'promise_all' | 'all';
100101
}
101102

102103
export type Context = import('zimmerframe').Context<AST.SvelteNode, ClientTransformState>;

packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,17 @@ export function Program(node, context) {
152152
chunk.position + offset,
153153
0,
154154
b.declaration(chunk.kind, [
155-
b.declarator(declarator.id, b.call(b.await(b.call('$.save', declarator.init))))
155+
b.declarator(
156+
declarator.id,
157+
chunk.type === 'promise_all'
158+
? b.await(declarator.init)
159+
: b.call(b.await(b.call('$.save', declarator.init)))
160+
)
156161
])
157162
);
158163
} else {
159164
const pattern = b.array_pattern(chunk.declarators.map(({ id }) => id));
160-
const init = b.call('$.all', ...chunk.declarators.map(({ init }) => init));
165+
const init = b.call(`$.${chunk.type}`, ...chunk.declarators.map(({ init }) => init));
161166
body.splice(
162167
chunk.position + offset,
163168
0,

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,22 @@ export function VariableDeclaration(node, context) {
7171
];
7272
if (
7373
context.state.current_parallelized_chunk &&
74-
context.state.current_parallelized_chunk.kind === node.kind
74+
context.state.current_parallelized_chunk.kind === node.kind &&
75+
context.state.current_parallelized_chunk.type === 'all'
7576
) {
7677
context.state.current_parallelized_chunk.declarators.push(...declarators);
7778
context.state.current_parallelized_chunk.bindings.push(...bindings);
7879
context.state.current_parallelized_chunk.position = /** @type {Program} */ (
7980
context.path.at(-1)
8081
).body.indexOf(node);
8182
} else {
83+
/** @type {ParallelizedChunk} */
8284
const chunk = {
8385
kind: node.kind,
8486
declarators,
8587
position: /** @type {Program} */ (context.path.at(-1)).body.indexOf(node),
86-
bindings
88+
bindings,
89+
type: 'all'
8790
};
8891
context.state.current_parallelized_chunk = chunk;
8992
context.state.parallelized_chunks.push(chunk);
@@ -257,7 +260,6 @@ export function VariableDeclaration(node, context) {
257260
is_async &&
258261
context.state.analysis.instance &&
259262
context.state.scope === context.state.analysis.instance.scope &&
260-
!dev &&
261263
// TODO make it work without this
262264
declarator.id.type === 'Identifier'
263265
) {
@@ -266,6 +268,7 @@ export function VariableDeclaration(node, context) {
266268
...context.state.scope.get_bindings(declarator)
267269
]);
268270
}
271+
const type = parallelize ? (dev ? 'promise_all' : 'all') : null;
269272

270273
/** @type {VariableDeclarator[]} */
271274
const derived_declarators = [];
@@ -289,7 +292,13 @@ export function VariableDeclaration(node, context) {
289292
);
290293

291294
if (!parallelize) call = b.call(b.await(b.call('$.save', call)));
292-
if (dev) call = b.call('$.tag', call, b.literal(declarator.id.name));
295+
if (dev) {
296+
call = b.call(
297+
'$.tag' + (parallelize ? '_async' : ''),
298+
call,
299+
b.literal(declarator.id.name)
300+
);
301+
}
293302
bindings.push(/** @type {Binding} */ (context.state.scope.get(declarator.id.name)));
294303
derived_declarators.push(b.declarator(declarator.id, call));
295304
} else {
@@ -373,19 +382,22 @@ export function VariableDeclaration(node, context) {
373382
}));
374383
if (
375384
context.state.current_parallelized_chunk &&
376-
context.state.current_parallelized_chunk.kind === node.kind
385+
context.state.current_parallelized_chunk.kind === node.kind &&
386+
context.state.current_parallelized_chunk.type === type
377387
) {
378388
context.state.current_parallelized_chunk.declarators.push(...declarators);
379389
context.state.current_parallelized_chunk.bindings.push(...bindings);
380390
context.state.current_parallelized_chunk.position = /** @type {Program} */ (
381391
context.path.at(-1)
382392
).body.indexOf(node);
383393
} else {
394+
/** @type {ParallelizedChunk} */
384395
const chunk = {
385396
kind: node.kind,
386397
declarators,
387398
position: /** @type {Program} */ (context.path.at(-1)).body.indexOf(node),
388-
bindings
399+
bindings,
400+
type: /** @type {ParallelizedChunk['type']} */ (type)
389401
};
390402
context.state.current_parallelized_chunk = chunk;
391403
context.state.parallelized_chunks.push(chunk);

packages/svelte/src/internal/client/dev/tracing.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ export function tag(source, label) {
184184
return source;
185185
}
186186

187+
/**
188+
* @template T
189+
* @param {Promise<Value<T>>} promise
190+
* @param {string} label
191+
* @returns {Promise<Value<T>>}
192+
*/
193+
export async function tag_async(promise, label) {
194+
const source = await promise;
195+
return tag(source, label);
196+
}
197+
187198
/**
188199
* @param {unknown} value
189200
* @param {string} label

packages/svelte/src/internal/client/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export { add_locations } from './dev/elements.js';
77
export { hmr } from './dev/hmr.js';
88
export { create_ownership_validator } from './dev/ownership.js';
99
export { check_target, legacy_api } from './dev/legacy.js';
10-
export { trace, tag, tag_proxy } from './dev/tracing.js';
10+
export { trace, tag, tag_async, tag_proxy } from './dev/tracing.js';
1111
export { inspect } from './dev/inspect.js';
1212
export { async } from './dom/blocks/async.js';
1313
export { validate_snippet_args } from './dev/validation.js';
@@ -102,6 +102,7 @@ export {
102102
all,
103103
async_body,
104104
for_await_track_reactivity_loss,
105+
promise_all,
105106
save,
106107
track_reactivity_loss
107108
} from './reactivity/async.js';

packages/svelte/src/internal/client/reactivity/async.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,5 @@ export function all(...promises) {
202202
)
203203
);
204204
}
205+
206+
export const promise_all = Promise.all;

0 commit comments

Comments
 (0)