Skip to content

Commit fbec895

Browse files
committed
use helper for async bodies
1 parent af11ee5 commit fbec895

File tree

3 files changed

+36
-51
lines changed

3 files changed

+36
-51
lines changed

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

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,38 @@ export function client_component(analysis, options) {
359359
if (dev) push_args.push(b.id(analysis.name));
360360

361361
let component_block = b.block([
362+
store_init,
362363
...store_setup,
363364
...legacy_reactive_declarations,
364365
...group_binding_declarations,
365-
...state.instance_level_snippets,
366-
.../** @type {ESTree.Statement[]} */ (instance.body),
367-
analysis.runes || !analysis.needs_context
368-
? b.empty
369-
: b.stmt(b.call('$.init', analysis.immutable ? b.true : undefined))
366+
...state.instance_level_snippets
370367
]);
371368

369+
if (analysis.instance.has_await) {
370+
const body = b.block([
371+
.../** @type {ESTree.Statement[]} */ (instance.body),
372+
b.if(b.call('$.aborted'), b.return()),
373+
.../** @type {ESTree.Statement[]} */ (template.body)
374+
]);
375+
376+
component_block.body.push(b.stmt(b.call(`$.async_body`, b.arrow([], body, true))));
377+
} else {
378+
component_block.body.push(
379+
.../** @type {ESTree.Statement[]} */ (instance.body),
380+
.../** @type {ESTree.Statement[]} */ (template.body)
381+
);
382+
}
383+
372384
if (analysis.needs_mutation_validation) {
373385
component_block.body.unshift(
374386
b.var('$$ownership_validator', b.call('$.create_ownership_validator', b.id('$$props')))
375387
);
376388
}
377389

390+
if (!analysis.runes && analysis.needs_context) {
391+
component_block.body.push(b.stmt(b.call('$.init', analysis.immutable ? b.true : undefined)));
392+
}
393+
378394
const should_inject_context =
379395
dev ||
380396
analysis.needs_context ||
@@ -389,52 +405,6 @@ export function client_component(analysis, options) {
389405
analysis.uses_slots ||
390406
analysis.slot_names.size > 0;
391407

392-
if (analysis.instance.has_await) {
393-
const params = [b.id('$$anchor')];
394-
if (should_inject_props) {
395-
params.push(b.id('$$props'));
396-
}
397-
if (store_setup.length > 0) {
398-
params.push(b.id('$$stores'));
399-
}
400-
const body = b.function_declaration(
401-
b.id('$$body'),
402-
params,
403-
b.block([
404-
b.var('$$unsuspend', b.call('$.suspend')),
405-
b.var('$$active', b.id('$.active_effect')),
406-
b.try_catch(
407-
b.block([
408-
...component_block.body,
409-
b.if(b.call('$.aborted'), b.return()),
410-
.../** @type {ESTree.Statement[]} */ (template.body)
411-
]),
412-
b.block([
413-
b.if(
414-
b.unary('!', b.call('$.aborted', b.id('$$active'))),
415-
b.stmt(b.call('$.invoke_error_boundary', b.id('$$error'), b.id('$$active')))
416-
)
417-
])
418-
),
419-
b.stmt(b.call('$$unsuspend'))
420-
]),
421-
true
422-
);
423-
424-
state.hoisted.push(body);
425-
426-
component_block = b.block([
427-
b.var('fragment', b.call('$.comment')),
428-
b.var('node', b.call('$.first_child', b.id('fragment'))),
429-
store_init,
430-
b.stmt(b.call(body.id, b.id('node'), ...params.slice(1))),
431-
b.stmt(b.call('$.append', b.id('$$anchor'), b.id('fragment')))
432-
]);
433-
} else {
434-
component_block.body.unshift(store_init);
435-
component_block.body.push(.../** @type {ESTree.Statement[]} */ (template.body));
436-
}
437-
438408
// trick esrap into including comments
439409
component_block.loc = instance.loc;
440410

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { suspend } from './reactivity/batch.js';
2+
3+
/**
4+
* @param {() => Promise<void>} fn
5+
*/
6+
export async function async_body(fn) {
7+
const unsuspend = suspend();
8+
9+
try {
10+
await fn();
11+
} finally {
12+
unsuspend();
13+
}
14+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { createAttachmentKey as attachment } from '../../attachments/index.js';
22
export { FILENAME, HMR, NAMESPACE_SVG } from '../../constants.js';
3+
export { async_body } from './async_body.js';
34
export { push, pop, add_svelte_meta } from './context.js';
45
export { assign, assign_and, assign_or, assign_nullish } from './dev/assign.js';
56
export { cleanup_styles } from './dev/css.js';

0 commit comments

Comments
 (0)