Skip to content

Commit dd9abea

Browse files
trueadmdummdidummRich-Harris
authored
fix: ensure spread attribute events are attached synchronously (#14387)
* 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]>
1 parent 6a5f30b commit dd9abea

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

.changeset/rich-worms-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: attach spread attribute events synchronously

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { create_event, delegate } from './events.js';
55
import { add_form_reset_listener, autofocus } from './misc.js';
66
import * as w from '../../warnings.js';
77
import { LOADING_ATTR_SYMBOL } from '../../constants.js';
8-
import { queue_idle_task, queue_micro_task } from '../task.js';
8+
import { queue_idle_task } from '../task.js';
99
import { is_capture_event, is_delegated, normalize_attribute } from '../../../../utils.js';
1010
import {
1111
active_effect,
@@ -209,8 +209,6 @@ export function set_attributes(
209209

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

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

280-
if (!prev) {
281-
events.push([
282-
key,
283-
value,
284-
() => (current[event_handle_key] = create_event(event_name, element, handle, opts))
285-
]);
286-
} else {
287-
current[event_handle_key] = create_event(event_name, element, handle, opts);
288-
}
278+
current[event_handle_key] = create_event(event_name, element, handle, opts);
289279
} else {
290280
// @ts-ignore
291281
element[`__${event_name}`] = value;
@@ -325,19 +315,6 @@ export function set_attributes(
325315
}
326316
}
327317

328-
// On the first run, ensure that events are added after bindings so
329-
// that their listeners fire after the binding listeners
330-
if (!prev) {
331-
queue_micro_task(() => {
332-
if (!element.isConnected) return;
333-
for (const [key, value, evt] of events) {
334-
if (current[key] === value) {
335-
evt();
336-
}
337-
}
338-
});
339-
}
340-
341318
return current;
342319
}
343320

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
test({ assert, logs }) {
5+
assert.deepEqual(logs, ['onfocus']);
6+
}
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
const focus = (input) => {
3+
input.focus();
4+
};
5+
</script>
6+
7+
<input {...({})} onfocus={() => console.log("onfocus")} use:focus />

0 commit comments

Comments
 (0)