mirror of https://github.com/sveltejs/svelte
fix: don't mark deriveds while an effect is updating (#18124)
Fixes #18123 This makes setting state inside effects slightly slower theoretically (since they hit the new guard), but I verified that the original issue for which we introduced this (#16658) is still fast with this change. The more we add logic to this the more I think we should investigate switching to a different mechanism. I tried using a `Set` previously but it did hurt the benchmarks a bit - might try to revisit a variant of this. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>pull/18125/head
parent
51736e576d
commit
8e7319063a
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: don't mark deriveds while an effect is updating
|
||||
@ -0,0 +1,7 @@
|
||||
<script>
|
||||
import { store } from "./store.svelte.js";
|
||||
|
||||
// This write marks the derived in main.svelte before it has reactions added to it.
|
||||
// This test checks that this does not cause the WAS_MARKED logic to incorrectly skip marking the derived subsequently.
|
||||
store.set("child-init-write", Math.random());
|
||||
</script>
|
||||
@ -0,0 +1,29 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
async test({ assert, target }) {
|
||||
const [show, hide] = target.querySelectorAll('button');
|
||||
|
||||
hide.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>show</button>
|
||||
<button>hide</button>
|
||||
`
|
||||
);
|
||||
|
||||
show.click();
|
||||
flushSync();
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>show</button>
|
||||
<button>hide</button>
|
||||
<div>visible</div>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,14 @@
|
||||
<script>
|
||||
import Child from "./Child.svelte";
|
||||
import { store } from "./store.svelte.js";
|
||||
|
||||
const visible = $derived(store.get("visible"));
|
||||
const visible2 = $derived(visible);
|
||||
</script>
|
||||
|
||||
<button onclick={() => store.set("visible", true)}>show</button>
|
||||
<button onclick={() => store.set("visible", false)}>hide</button>
|
||||
{#if visible2}
|
||||
<Child />
|
||||
<div>visible</div>
|
||||
{/if}
|
||||
@ -0,0 +1,13 @@
|
||||
class RawStore {
|
||||
values = $state.raw({ visible: true });
|
||||
|
||||
get(key) {
|
||||
return this.values[key];
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.values = { ...this.values, [key]: value };
|
||||
}
|
||||
}
|
||||
|
||||
export const store = new RawStore();
|
||||
Loading…
Reference in new issue