fix: keep spread non-delegated event handlers up to date (#16180)

* fix: keep spread non-delegated event handlers up to date

#15961 introduced a regression where non-delegated events that were spread and updated were not getting updated. This fixes that by ensuring prev is actually updated to the most recent value

* fix
pull/16195/head
Simon H 3 months ago committed by GitHub
parent 838f881550
commit a5f500e7c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: keep spread non-delegated event handlers up to date

@ -483,8 +483,8 @@ export function attribute_effect(
block(() => {
var next = fn(...deriveds.map(get));
set_attributes(element, prev, next, css_hash, skip_warning);
/** @type {Record<string | symbol, any>} */
var current = set_attributes(element, prev, next, css_hash, skip_warning);
if (inited && is_select && 'value' in next) {
select_option(/** @type {HTMLSelectElement} */ (element), next.value, false);
@ -501,9 +501,11 @@ export function attribute_effect(
if (effects[symbol]) destroy_effect(effects[symbol]);
effects[symbol] = branch(() => attach(element, () => n));
}
current[symbol] = n;
}
prev = next;
prev = current;
});
if (is_select) {

@ -0,0 +1,18 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
test({ assert, target }) {
const [change, increment] = target.querySelectorAll('button');
increment.click();
flushSync();
assert.htmlEqual(target.innerHTML, '<button>change handlers</button><button>1 / 1</button>');
change.click();
flushSync();
increment.click();
flushSync();
assert.htmlEqual(target.innerHTML, '<button>change handlers</button><button>3 / 3</button>');
}
});

@ -0,0 +1,27 @@
<script>
let delegated = $state(0);
let non_delegated = $state(0);
let attrs = $state({
onclick: () => {
delegated += 1;
},
onclickcapture: () => {
non_delegated += 1;
}
});
</script>
<button
onclick={() =>
(attrs = {
onclick: () => {
delegated += 2;
},
onclickcapture: () => {
non_delegated += 2;
}
})}
>
change handlers
</button>
<button {...attrs}>{delegated} / {non_delegated}</button>
Loading…
Cancel
Save