mirror of https://github.com/sveltejs/svelte
fix: make `$effect.active()` true when updating deriveds (#11500)
* fix: make `$effect.active()` true when updating deriveds * WIP * this seems to work? * prevent effects being created in unowned deriveds * update test * fix issue --------- Co-authored-by: Dominic Gannaway <dg@domgan.com>pull/11518/head
parent
30caaef2e5
commit
8742823e39
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: make `$effect.active()` true when updating deriveds
|
@ -0,0 +1,41 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
html: `
|
||||
<button>toggle outer</button>
|
||||
<button>toggle inner</button>
|
||||
<button>reset</button>
|
||||
`,
|
||||
|
||||
test({ assert, target }) {
|
||||
const [outer, inner, reset] = target.querySelectorAll('button');
|
||||
|
||||
flushSync(() => outer?.click());
|
||||
flushSync(() => inner?.click());
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>toggle outer</button>
|
||||
<button>toggle inner</button>
|
||||
<button>reset</button>
|
||||
<p>v is true</p>
|
||||
`
|
||||
);
|
||||
|
||||
flushSync(() => reset?.click());
|
||||
flushSync(() => inner?.click());
|
||||
flushSync(() => outer?.click());
|
||||
|
||||
assert.htmlEqual(
|
||||
target.innerHTML,
|
||||
`
|
||||
<button>toggle outer</button>
|
||||
<button>toggle inner</button>
|
||||
<button>reset</button>
|
||||
<p>v is true</p>
|
||||
`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,31 @@
|
||||
<script>
|
||||
let value = $state(false);
|
||||
const fn = () => {
|
||||
if ($effect.active()) {
|
||||
$effect(() => {
|
||||
value = true;
|
||||
});
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
let outer = $state(false);
|
||||
let inner = $state(false);
|
||||
let v = $derived(inner ? fn() : false);
|
||||
</script>
|
||||
|
||||
<button onclick={() => outer = !outer}>
|
||||
toggle outer
|
||||
</button>
|
||||
|
||||
<button onclick={() => inner = !inner}>
|
||||
toggle inner
|
||||
</button>
|
||||
|
||||
<button onclick={() => outer = inner = value = false}>
|
||||
reset
|
||||
</button>
|
||||
|
||||
{#if outer && v}
|
||||
<p>v is true</p>
|
||||
{/if}
|
Loading…
Reference in new issue