mirror of https://github.com/sveltejs/svelte
Merge fa0524da98 into 5895c637b0
commit
ec221224d5
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"svelte": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: ensure `$derived` returns pre-change value during teardown in eager block effects path
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
<script>
|
||||||
|
import { onDestroy } from 'svelte';
|
||||||
|
|
||||||
|
let { count } = $props();
|
||||||
|
|
||||||
|
let a = $derived(count * 2);
|
||||||
|
let b = $derived(count * 2);
|
||||||
|
let c = $derived(count * 2);
|
||||||
|
let d = $derived(count * 2);
|
||||||
|
|
||||||
|
$effect.pre(() => () => console.log('effect.pre', a));
|
||||||
|
|
||||||
|
function attachment(_node) {
|
||||||
|
return () => console.log('attach', b);
|
||||||
|
}
|
||||||
|
|
||||||
|
$effect(() => () => console.log('effect', c));
|
||||||
|
|
||||||
|
onDestroy(() => console.log('onDestroy', d));
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<span {@attach attachment}>a: {a}</span>
|
||||||
|
<span>b: {b}</span>
|
||||||
|
<span>c: {c}</span>
|
||||||
|
<span>d: {d}</span>
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
import { flushSync } from 'svelte';
|
||||||
|
import { test, ok } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, logs, target }) {
|
||||||
|
const button = target.querySelector('button');
|
||||||
|
ok(button);
|
||||||
|
|
||||||
|
flushSync(() => button.click());
|
||||||
|
|
||||||
|
// All four teardowns should see the pre-change derived value (2), not the post-change value (10)
|
||||||
|
assert.deepEqual(logs, ['effect.pre', 2, 'attach', 2, 'effect', 2, 'onDestroy', 2]);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
<script>
|
||||||
|
import Child from './Child.svelte';
|
||||||
|
|
||||||
|
let source = $state(1);
|
||||||
|
let count = $state(1);
|
||||||
|
|
||||||
|
// State is updated indirectly via an effect — triggers the eager block effects path
|
||||||
|
$effect(() => {
|
||||||
|
count = source;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={() => (source = 5)}>unmount</button>
|
||||||
|
|
||||||
|
{#if count < 5}
|
||||||
|
<Child {count} />
|
||||||
|
{/if}
|
||||||
Loading…
Reference in new issue