mirror of https://github.com/sveltejs/svelte
fix: don't reexecute derived with no dependencies on teardown (#16438)
The prior logic was wrong because it reexecuted when something was clean, but we want to when it's not. The remaining fix was to also check the reactions: If an effect is destroyed and it was the last reaction of a derived then the derived is set to `MAYBE_DIRTY`. We therefore also need to check if the derived still has anyone listening to it, and only then reexecute it. Fixes #16363pull/16449/head
parent
05f6436445
commit
27c90dfa83
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: don't reexecute derived with no dependencies on teardown
|
@ -0,0 +1,11 @@
|
||||
<script>
|
||||
import { onDestroy } from 'svelte'
|
||||
|
||||
const { callback } = $props()
|
||||
|
||||
onDestroy(() => {
|
||||
callback()
|
||||
})
|
||||
</script>
|
||||
|
||||
<div>teardown</div>
|
@ -0,0 +1,30 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>click</button>
|
||||
<div>teardown</div>
|
||||
<div>1</div>
|
||||
<div>2</div>
|
||||
<div>3</div>
|
||||
`
|
||||
);
|
||||
const [increment] = target.querySelectorAll('button');
|
||||
|
||||
increment.click();
|
||||
flushSync();
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>click</button>
|
||||
<div>1</div>
|
||||
<div>3</div>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,25 @@
|
||||
<script>
|
||||
import { SvelteSet } from 'svelte/reactivity'
|
||||
import Teardown from './Teardown.svelte'
|
||||
|
||||
class Test {
|
||||
originalIds = $state.raw([1, 2, 3])
|
||||
ids = $derived(new SvelteSet(this.originalIds))
|
||||
}
|
||||
|
||||
let show = $state(true)
|
||||
const test = new Test()
|
||||
|
||||
function callback() {
|
||||
test.ids.delete(2)
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
<button onclick={() => (show = !show)}>click</button>
|
||||
{#if show}
|
||||
<Teardown {callback} />
|
||||
{/if}
|
||||
{#each test.ids as id}
|
||||
<div>{id}</div>
|
||||
{/each}
|
Loading…
Reference in new issue