mirror of https://github.com/sveltejs/svelte
fix: properly lazily evaluate RHS when checking for assignment_value_stale (#17906)
Fixes #17904 by wrapping the RHS in a `() =>`. ### Before submitting the PR, please make sure you do the following - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] Prefix your PR title with `feat:`, `fix:`, `chore:`, or `docs:`. - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. - [x] If this PR changes code within `packages/svelte/src`, add a changeset (`npx changeset`). ### Tests and linting - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint`always-populate-batch-values
parent
0e8f49b25f
commit
58f161dee2
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: properly lazily evaluate RHS when checking for `assignment_value_stale`
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
import { tick } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
compileOptions: {
|
||||||
|
dev: true
|
||||||
|
},
|
||||||
|
async test({ assert, target }) {
|
||||||
|
const button = /** @type {HTMLElement} */ (target.querySelector('button'));
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(target.innerHTML, `<button>go</button><p>count1: 0, count2: 0</p>`);
|
||||||
|
button.click();
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(target.innerHTML, `<button>go</button><p>count1: 1, count2: 1</p>`);
|
||||||
|
button.click();
|
||||||
|
await tick();
|
||||||
|
assert.htmlEqual(target.innerHTML, `<button>go</button><p>count1: 2, count2: 1</p>`);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
<script>
|
||||||
|
let count1 = $state(0);
|
||||||
|
let count2 = $state(0);
|
||||||
|
let cache = $state({});
|
||||||
|
|
||||||
|
function go() {
|
||||||
|
count1++;
|
||||||
|
const value = cache.value ??= get_value();
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_value() {
|
||||||
|
count2++;
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={go}>go</button>
|
||||||
|
<p>count1: {count1}, count2: {count2}</p>
|
||||||
Loading…
Reference in new issue