Skip to content

fix: ensure directives run in sequential order #12591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-turkeys-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure directives run in sequential order
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,8 @@ function serialize_event_handler(node, metadata, { state, visit }) {
function serialize_event(node, metadata, context) {
const state = context.state;

/** @type {Statement} */
let statement;
/** @type {Expression} */
let expression;

if (node.expression) {
let handler = serialize_event_handler(node, metadata, context);
Expand Down Expand Up @@ -1303,19 +1303,23 @@ function serialize_event(node, metadata, context) {
}

// Events need to run in order with bindings/actions
statement = b.stmt(b.call('$.event', ...args));
expression = b.call('$.event', ...args);
} else {
statement = b.stmt(
b.call(
'$.event',
b.literal(node.name),
state.node,
serialize_event_handler(node, metadata, context)
)
expression = b.call(
'$.event',
b.literal(node.name),
state.node,
serialize_event_handler(node, metadata, context)
);
}

const parent = /** @type {SvelteNode} */ (context.path.at(-1));
const has_action_directive =
parent.type === 'RegularElement' && parent.attributes.find((a) => a.type === 'UseDirective');
const statement = b.stmt(
has_action_directive ? b.call('$.effect', b.thunk(expression)) : expression
);

if (
parent.type === 'SvelteDocument' ||
parent.type === 'SvelteWindow' ||
Expand Down Expand Up @@ -3083,12 +3087,20 @@ export const template_visitors = {
}
}

const parent = /** @type {import('#compiler').SvelteNode} */ (context.path.at(-1));
const has_action_directive =
parent.type === 'RegularElement' && parent.attributes.find((a) => a.type === 'UseDirective');

// Bindings need to happen after attribute updates, therefore after the render effect, and in order with events/actions.
// bind:this is a special case as it's one-way and could influence the render effect.
if (node.name === 'this') {
state.init.push(b.stmt(call_expr));
state.init.push(
b.stmt(has_action_directive ? b.call('$.effect', b.thunk(call_expr)) : call_expr)
);
} else {
state.after_update.push(b.stmt(call_expr));
state.after_update.push(
b.stmt(has_action_directive ? b.call('$.effect', b.thunk(call_expr)) : call_expr)
);
}
},
Component(node, context) {
Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export {
legacy_pre_effect_reset,
render_effect,
template_effect,
effect,
user_effect,
user_pre_effect
} from './reactivity/effects.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,25 @@ export default test({
flushSync();
}

// Svelte 5 breaking change, use:action now fires
// in effect phase. So they will occur AFTER the others.
assert.deepEqual(value, [
'1',
'2',
'3',
'1',
'4',
'5',
'6',
'4',
'7',
'9',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'18',
'17'
'17',
'18'
]);

// Previously
// assert.deepEqual(value, [
// '1',
// '2',
// '3',
// '4',
// '5',
// '6',
// '7',
// '8',
// '9',
// '10',
// '11',
// '12',
// '13',
// '14',
// '15',
// '16',
// '17',
// '18',
// ]);
}
});
Loading