fix timing issue

pull/10939/head
Rich Harris 2 years ago
parent 7a152fa834
commit c8f4868803

@ -29,6 +29,8 @@ export interface ComponentClientTransformState extends ClientTransformState {
readonly hoisted: Array<Statement | ModuleDeclaration>; readonly hoisted: Array<Statement | ModuleDeclaration>;
readonly events: Set<string>; readonly events: Set<string>;
/** Stuff that happens before the render effect(s) */
readonly before_init: Statement[];
/** Stuff that happens before the render effect(s) */ /** Stuff that happens before the render effect(s) */
readonly init: Statement[]; readonly init: Statement[];
/** Stuff that happens inside the render effect */ /** Stuff that happens inside the render effect */

@ -1005,6 +1005,7 @@ function create_block(parent, name, nodes, context) {
/** @type {import('../types').ComponentClientTransformState} */ /** @type {import('../types').ComponentClientTransformState} */
const state = { const state = {
...context.state, ...context.state,
before_init: [],
init: [], init: [],
update: [], update: [],
after_update: [], after_update: [],
@ -1050,11 +1051,11 @@ function create_block(parent, name, nodes, context) {
args.push(b.false); args.push(b.false);
} }
body.push(b.var(id, b.call('$.open', ...args)), ...state.init); body.push(b.var(id, b.call('$.open', ...args)), ...state.before_init, ...state.init);
close = b.stmt(b.call('$.close', b.id('$$anchor'), id)); close = b.stmt(b.call('$.close', b.id('$$anchor'), id));
} else if (is_single_child_not_needing_template) { } else if (is_single_child_not_needing_template) {
context.visit(trimmed[0], state); context.visit(trimmed[0], state);
body.push(...state.init); body.push(...state.before_init, ...state.init);
} else if (trimmed.length > 0) { } else if (trimmed.length > 0) {
const id = b.id(context.state.scope.generate('fragment')); const id = b.id(context.state.scope.generate('fragment'));
@ -1071,7 +1072,11 @@ function create_block(parent, name, nodes, context) {
state state
}); });
body.push(b.var(id, b.call('$.space_frag', b.id('$$anchor'))), ...state.init); body.push(
b.var(id, b.call('$.space_frag', b.id('$$anchor'))),
...state.before_init,
...state.init
);
close = b.stmt(b.call('$.close', b.id('$$anchor'), id)); close = b.stmt(b.call('$.close', b.id('$$anchor'), id));
} else { } else {
/** @type {(is_text: boolean) => import('estree').Expression} */ /** @type {(is_text: boolean) => import('estree').Expression} */
@ -1107,12 +1112,12 @@ function create_block(parent, name, nodes, context) {
body.push(b.var(id, b.call('$.open_frag', ...args))); body.push(b.var(id, b.call('$.open_frag', ...args)));
} }
body.push(...state.init); body.push(...state.before_init, ...state.init);
close = b.stmt(b.call('$.close_frag', b.id('$$anchor'), id)); close = b.stmt(b.call('$.close_frag', b.id('$$anchor'), id));
} }
} else { } else {
body.push(...state.init); body.push(...state.before_init, ...state.init);
} }
if (state.update.length > 0) { if (state.update.length > 0) {
@ -1256,6 +1261,9 @@ function serialize_event_handler(node, { state, visit }) {
function serialize_event(node, context) { function serialize_event(node, context) {
const state = context.state; const state = context.state;
/** @type {import('estree').Statement} */
let statement;
if (node.expression) { if (node.expression) {
let handler = serialize_event_handler(node, context); let handler = serialize_event_handler(node, context);
const event_name = node.name; const event_name = node.name;
@ -1323,14 +1331,19 @@ function serialize_event(node, context) {
} }
// Events need to run in order with bindings/actions // Events need to run in order with bindings/actions
state.after_update.push(b.stmt(b.call('$.event', ...args))); statement = b.stmt(b.call('$.event', ...args));
} else { } else {
state.after_update.push( statement = b.stmt(
b.stmt(
b.call('$.event', b.literal(node.name), state.node, serialize_event_handler(node, context)) b.call('$.event', b.literal(node.name), state.node, serialize_event_handler(node, context))
)
); );
} }
const parent = /** @type {import('#compiler').SvelteNode} */ (context.path.at(-1));
if (parent.type === 'SvelteDocument' || parent.type === 'SvelteWindow') {
state.before_init.push(statement);
} else {
state.after_update.push(statement);
}
} }
/** /**
@ -2027,6 +2040,7 @@ export const template_visitors = {
state: { state: {
...context.state, ...context.state,
node: element_id, node: element_id,
before_init: [],
init: [], init: [],
update: [], update: [],
after_update: [] after_update: []

Loading…
Cancel
Save