mirror of https://github.com/sveltejs/svelte
fix: ensure deriveds values are correct across batches (#17917)
capture derived updates aswell so they become part of current/previous so that `batch_values` computation is correct when e.g. using `$state.eager` with a derived. Fixes #17849pull/17922/head
parent
63686ae22c
commit
44c4f213e9
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: ensure deriveds values are correct across batches
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target }) {
|
||||||
|
await tick();
|
||||||
|
const [increment, shift] = target.querySelectorAll('button');
|
||||||
|
|
||||||
|
increment.click();
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`<button>clicks: 0 - 0 - 0</button> <button>shift</button> <p>true - true</p>`
|
||||||
|
);
|
||||||
|
|
||||||
|
shift.click();
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(
|
||||||
|
target.innerHTML,
|
||||||
|
`<button>clicks: 1 - 1 - 1</button> <button>shift</button> <p>false - false</p>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
<script>
|
||||||
|
|
||||||
|
let count = $state(0);
|
||||||
|
const delayedCount = $derived(await push(count));
|
||||||
|
const derivedCount = $derived(count);
|
||||||
|
|
||||||
|
let resolvers = [];
|
||||||
|
|
||||||
|
function push(value) {
|
||||||
|
if (!value) return value;
|
||||||
|
const { promise, resolve } = Promise.withResolvers();
|
||||||
|
resolvers.push(() => resolve(value));
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={() => count += 1}>
|
||||||
|
clicks: {count} - {delayedCount} - {derivedCount}
|
||||||
|
</button>
|
||||||
|
<button onclick={() => resolvers.shift()?.()}>shift</button>
|
||||||
|
|
||||||
|
<p>{$state.eager(count) !== count} - {$state.eager(derivedCount) !== derivedCount}</p>
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target, logs }) {
|
||||||
|
const [btn] = target.querySelectorAll('button');
|
||||||
|
|
||||||
|
btn.click();
|
||||||
|
await tick();
|
||||||
|
assert.deepEqual(logs, [10]);
|
||||||
|
|
||||||
|
btn.click();
|
||||||
|
await tick();
|
||||||
|
assert.deepEqual(logs, [10, 10]);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
<script>
|
||||||
|
import { fork } from 'svelte';
|
||||||
|
|
||||||
|
let s = $state(1);
|
||||||
|
let d = $derived(s * 10);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button
|
||||||
|
onclick={() => {
|
||||||
|
const f = fork(() => {
|
||||||
|
// d has not been read yet, so this write happens with an uninitialized old value
|
||||||
|
s = 2;
|
||||||
|
d = 99;
|
||||||
|
});
|
||||||
|
|
||||||
|
f.discard();
|
||||||
|
console.log(d);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
test
|
||||||
|
</button>
|
||||||
Loading…
Reference in new issue