fix: only re-run directly applied attachment if it changed

pull/15962/head
Rich Harris 4 months ago
parent 7183886a73
commit 110e37ee51

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: only re-run directly applied attachment if it changed

@ -1,15 +1,27 @@
import { effect } from '../../reactivity/effects.js';
/** @import { Effect } from '#client' */
import { block, branch, destroy_effect, effect } from '../../reactivity/effects.js';
/**
* @param {Element} node
* @param {() => (node: Element) => void} get_fn
*/
export function attach(node, get_fn) {
effect(() => {
const fn = get_fn();
/** @type {false | undefined | ((node: Element) => void)} */
var fn = undefined;
// we use `&&` rather than `?.` so that things like
// `{@attach DEV && something_dev_only()}` work
return fn && fn(node);
/** @type {Effect | null} */
var effect;
block(() => {
if (fn !== (fn = get_fn())) {
if (effect) {
destroy_effect(effect);
effect = null;
}
if (fn) {
effect = branch(() => /** @type {(node: Element) => void} */ (fn)(node));
}
}
});
}

@ -0,0 +1,16 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
test({ assert, target, logs }) {
assert.deepEqual(logs, ['up']);
const button = target.querySelector('button');
flushSync(() => button?.click());
assert.deepEqual(logs, ['up']);
flushSync(() => button?.click());
assert.deepEqual(logs, ['up', 'down']);
}
});

@ -0,0 +1,15 @@
<script>
let state = {
count: 0,
attachment(){
console.log('up');
return () => console.log('down');
}
};
</script>
<button onclick={() => state.count++}>{state.count}</button>
{#if state.count < 2}
<div {@attach state.attachment}></div>
{/if}
Loading…
Cancel
Save