svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Svelte 5: Actions and on: directives don't respect the order they are listed

Open Conduitry opened this issue 1 year ago • 3 comments
trafficstars

Describe the bug

In Svelte 4, putting a use: action followed by an on: event handler on an element would first attach the action and then attach the event handler. In Svelte 5, it looks like event handlers are attached before the actions, regardless of the order they are specified on the element.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACnWQQW7DIBBFr4LY2JaidE-cSF101xvUXRAYp6h4xoIhUmX57gWHJN10A_z5_DcMixydhyjVxyJRTyCVfJ1nuZP8MxcRr-AZso6UgimVPprgZj4NOPCY0LAjFHrbWiQLnViKNXARe23t2xWQ311kQAhtY7wz381OtJ04noQhjORh7-lSLTEGmipQQMmKL43WQ2i67lDQa17-Nq92--j8HzTAJXkdnrw7rn95DoX9OTFnbIqg6jMI1YY5LjW6buPfLpb47ZSL-acmsm50YKXikGD9XH8B9L2B8mQBAAA=

Logs

No response

System Info

REPL

Severity

annoyance

Conduitry avatar May 08 '24 20:05 Conduitry

In the generated code, I do see these in order

	$.action(button, ($$node) => action($$node));
	$.event("click", button, handler, false);

so I'm assuming that $.action isn't synchronously calling the action's function.

Conduitry avatar May 08 '24 20:05 Conduitry

Conversely, when I'm using onclick, it looks like the action's event handler always gets called first: https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACnWQwW7DIAyGX8XikkSqunuWVtpht73BsgMDp0MjdgSmUhXl3QtJtu6yC_Db_j_bzGpwHqNq32dFekTVqpdpUgclt6mIeEUvmHXkFEyJdNEEN8m5p16GREYcE-j1qoktNjCXVC9FHLW1r1ckeXNRkDDUlfHOfFcHqBs4ncEwRfZ49HzZUzAEHncgYPHClybrMVRN81zQSz7-Nt_T9W_n_6ABL8nr8OD94Lqnx1LUfSaRjGVarad5L18gRWy3wdbtt7ri3l45mD9qZOsGh1a1EhIuH8sdwiQTbGMBAAA=

I'm guessing this is expected, though? Is this just a result of event delegation?

Conduitry avatar May 08 '24 20:05 Conduitry

Yes it's a result of event delegation, with #11435 as a proposal to possibly fix this.

The reason they run out of order is that action functions are executed in an effect, because else the dom isn't attached yet and certain stuff doesn't work correctly (I don't remember the details, if I do we should document this in the code). I'm not sure how to best fix this, if we want to fix this. Put listeners inside an effect, too? Bindings are already inside effects AFAIK so that order is preserved.

dummdidumm avatar May 08 '24 21:05 dummdidumm