Skip to content

Commit c8f4868

Browse files
committed
fix timing issue
1 parent 7a152fa commit c8f4868

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface ComponentClientTransformState extends ClientTransformState {
2929
readonly hoisted: Array<Statement | ModuleDeclaration>;
3030
readonly events: Set<string>;
3131

32+
/** Stuff that happens before the render effect(s) */
33+
readonly before_init: Statement[];
3234
/** Stuff that happens before the render effect(s) */
3335
readonly init: Statement[];
3436
/** Stuff that happens inside the render effect */

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ function create_block(parent, name, nodes, context) {
10051005
/** @type {import('../types').ComponentClientTransformState} */
10061006
const state = {
10071007
...context.state,
1008+
before_init: [],
10081009
init: [],
10091010
update: [],
10101011
after_update: [],
@@ -1050,11 +1051,11 @@ function create_block(parent, name, nodes, context) {
10501051
args.push(b.false);
10511052
}
10521053

1053-
body.push(b.var(id, b.call('$.open', ...args)), ...state.init);
1054+
body.push(b.var(id, b.call('$.open', ...args)), ...state.before_init, ...state.init);
10541055
close = b.stmt(b.call('$.close', b.id('$$anchor'), id));
10551056
} else if (is_single_child_not_needing_template) {
10561057
context.visit(trimmed[0], state);
1057-
body.push(...state.init);
1058+
body.push(...state.before_init, ...state.init);
10581059
} else if (trimmed.length > 0) {
10591060
const id = b.id(context.state.scope.generate('fragment'));
10601061

@@ -1071,7 +1072,11 @@ function create_block(parent, name, nodes, context) {
10711072
state
10721073
});
10731074

1074-
body.push(b.var(id, b.call('$.space_frag', b.id('$$anchor'))), ...state.init);
1075+
body.push(
1076+
b.var(id, b.call('$.space_frag', b.id('$$anchor'))),
1077+
...state.before_init,
1078+
...state.init
1079+
);
10751080
close = b.stmt(b.call('$.close', b.id('$$anchor'), id));
10761081
} else {
10771082
/** @type {(is_text: boolean) => import('estree').Expression} */
@@ -1107,12 +1112,12 @@ function create_block(parent, name, nodes, context) {
11071112
body.push(b.var(id, b.call('$.open_frag', ...args)));
11081113
}
11091114

1110-
body.push(...state.init);
1115+
body.push(...state.before_init, ...state.init);
11111116

11121117
close = b.stmt(b.call('$.close_frag', b.id('$$anchor'), id));
11131118
}
11141119
} else {
1115-
body.push(...state.init);
1120+
body.push(...state.before_init, ...state.init);
11161121
}
11171122

11181123
if (state.update.length > 0) {
@@ -1256,6 +1261,9 @@ function serialize_event_handler(node, { state, visit }) {
12561261
function serialize_event(node, context) {
12571262
const state = context.state;
12581263

1264+
/** @type {import('estree').Statement} */
1265+
let statement;
1266+
12591267
if (node.expression) {
12601268
let handler = serialize_event_handler(node, context);
12611269
const event_name = node.name;
@@ -1323,14 +1331,19 @@ function serialize_event(node, context) {
13231331
}
13241332

13251333
// Events need to run in order with bindings/actions
1326-
state.after_update.push(b.stmt(b.call('$.event', ...args)));
1334+
statement = b.stmt(b.call('$.event', ...args));
13271335
} else {
1328-
state.after_update.push(
1329-
b.stmt(
1330-
b.call('$.event', b.literal(node.name), state.node, serialize_event_handler(node, context))
1331-
)
1336+
statement = b.stmt(
1337+
b.call('$.event', b.literal(node.name), state.node, serialize_event_handler(node, context))
13321338
);
13331339
}
1340+
1341+
const parent = /** @type {import('#compiler').SvelteNode} */ (context.path.at(-1));
1342+
if (parent.type === 'SvelteDocument' || parent.type === 'SvelteWindow') {
1343+
state.before_init.push(statement);
1344+
} else {
1345+
state.after_update.push(statement);
1346+
}
13341347
}
13351348

13361349
/**
@@ -2027,6 +2040,7 @@ export const template_visitors = {
20272040
state: {
20282041
...context.state,
20292042
node: element_id,
2043+
before_init: [],
20302044
init: [],
20312045
update: [],
20322046
after_update: []

0 commit comments

Comments
 (0)