fix: ensure derived reactions have their version kept in sync with the reactive graph

pull/15434/head
Dominic Gannaway 1 year ago
parent 00d0c4247a
commit 3028e7ac84

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure derived reactions have their version kept in sync with the reactive graph

@ -0,0 +1,28 @@
import { flushSync } from 'svelte';
import { test } from '../../test';
export default test({
async test({ assert, target, logs }) {
let [btn1] = target.querySelectorAll('button');
btn1?.click();
flushSync();
btn1?.click();
flushSync();
logs.length = 0;
btn1?.click();
flushSync();
assert.deepEqual(logs, ['a', { value: 1 }]);
logs.length = 0;
btn1?.click();
flushSync();
assert.deepEqual(logs, ['a', { value: 2 }, 'b', { a: 2 }, 'c', { b: 2 }]);
}
});

@ -0,0 +1,28 @@
<script>
let trigger = $state(false)
let index = 0;
let a = $derived.by(() => {
trigger;
const value = [1,2,1][index++%3];
console.log("a", { value });
return value
})
let b = $derived.by(() => {
console.log("b", { a });
return a;
});
let c = $derived.by(() => {
console.log("c", { b });
return undefined;
});
$effect(() => {
c;
console.log('effect')
});
</script>
<button onclick={() => trigger = !trigger}>invalidate</button>
Loading…
Cancel
Save