mirror of https://github.com/sveltejs/svelte
chore: add "untracked allows writes" test (#17930)
While we don't officially document it, `untrack` also allows to opt out of the "unsafe mutation" validation, which is what we test here. For anyone coming across this: USE WITH CAUTION. This can cause graph inconsistencies leading to wrong values on initial render Doing this because we're gonna make use of it ourselves for remote functions, and this ensures we don't accidentally regress.pull/17919/head
parent
1ebed68322
commit
dcf4865b22
@ -0,0 +1,19 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
// While we don't officially document it, `untrack` also allows to opt out of the "unsafe mutation" validation, which is what we test here
|
||||
export default test({
|
||||
html: '<button>0 0 0</button>',
|
||||
test({ assert, target }) {
|
||||
const button = target.querySelector('button');
|
||||
|
||||
flushSync(() => button?.click());
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>1 1 2</button>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
import { untrack } from "svelte";
|
||||
|
||||
let count = $state(0);
|
||||
let mirrored = $state(0);
|
||||
let double = $derived.by(() => {
|
||||
untrack(() => {
|
||||
mirrored = count;
|
||||
});
|
||||
return count * 2;
|
||||
})
|
||||
</script>
|
||||
|
||||
<button onclick={() => count++}>{count} {mirrored} {double}</button>
|
||||
Loading…
Reference in new issue