Skip to content

Commit

Permalink
fix: ensure spread attribute events are attached synchronously (#14387)
Browse files Browse the repository at this point in the history
* fix: ensure spread attribute events are attached synchronously

* fix: ensure spread attribute events are attached synchronously

* Update .changeset/rich-worms-burn.md

Co-authored-by: Rich Harris <[email protected]>

* simplify

---------

Co-authored-by: Simon H <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent 6a5f30b commit dd9abea
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-worms-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: attach spread attribute events synchronously
27 changes: 2 additions & 25 deletions packages/svelte/src/internal/client/dom/elements/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { create_event, delegate } from './events.js';
import { add_form_reset_listener, autofocus } from './misc.js';
import * as w from '../../warnings.js';
import { LOADING_ATTR_SYMBOL } from '../../constants.js';
import { queue_idle_task, queue_micro_task } from '../task.js';
import { queue_idle_task } from '../task.js';
import { is_capture_event, is_delegated, normalize_attribute } from '../../../../utils.js';
import {
active_effect,
Expand Down Expand Up @@ -209,8 +209,6 @@ export function set_attributes(

// @ts-expect-error
var attributes = /** @type {Record<string, unknown>} **/ (element.__attributes ??= {});
/** @type {Array<[string, any, () => void]>} */
var events = [];

// since key is captured we use const
for (const key in next) {
Expand Down Expand Up @@ -277,15 +275,7 @@ export function set_attributes(
current[key].call(this, evt);
}

if (!prev) {
events.push([
key,
value,
() => (current[event_handle_key] = create_event(event_name, element, handle, opts))
]);
} else {
current[event_handle_key] = create_event(event_name, element, handle, opts);
}
current[event_handle_key] = create_event(event_name, element, handle, opts);
} else {
// @ts-ignore
element[`__${event_name}`] = value;
Expand Down Expand Up @@ -325,19 +315,6 @@ export function set_attributes(
}
}

// On the first run, ensure that events are added after bindings so
// that their listeners fire after the binding listeners
if (!prev) {
queue_micro_task(() => {
if (!element.isConnected) return;
for (const [key, value, evt] of events) {
if (current[key] === value) {
evt();
}
}
});
}

return current;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
test({ assert, logs }) {
assert.deepEqual(logs, ['onfocus']);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const focus = (input) => {
input.focus();
};
</script>

<input {...({})} onfocus={() => console.log("onfocus")} use:focus />

0 comments on commit dd9abea

Please sign in to comment.