mirror of https://github.com/sveltejs/svelte
feat: add $effect.root rune (#9638)
* feat: effect-root-rune feat: add $effect.root rune update doc update doc fix validation * cleanup logic * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md * address feedback --------- Co-authored-by: Rich Harris <richard.a.harris@gmail.com>pull/9650/head
parent
2660727a93
commit
81d3e47d1c
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
feat: add $effect.root rune
|
@ -0,0 +1,32 @@
|
||||
import { flushSync } from 'svelte';
|
||||
import { test } from '../../test';
|
||||
|
||||
export default test({
|
||||
get props() {
|
||||
return { log: [] };
|
||||
},
|
||||
|
||||
async test({ assert, target, component }) {
|
||||
const [b1, b2, b3] = target.querySelectorAll('button');
|
||||
|
||||
flushSync(() => {
|
||||
b1.click();
|
||||
b2.click();
|
||||
});
|
||||
|
||||
assert.deepEqual(component.log, [0, 1]);
|
||||
|
||||
flushSync(() => {
|
||||
b3.click();
|
||||
});
|
||||
|
||||
assert.deepEqual(component.log, [0, 1, 'cleanup 1', 'cleanup 2']);
|
||||
|
||||
flushSync(() => {
|
||||
b1.click();
|
||||
b2.click();
|
||||
});
|
||||
|
||||
assert.deepEqual(component.log, [0, 1, 'cleanup 1', 'cleanup 2']);
|
||||
}
|
||||
});
|
@ -0,0 +1,27 @@
|
||||
<script>
|
||||
let { log} = $props();
|
||||
|
||||
let x = $state(0);
|
||||
let y = $state(0);
|
||||
|
||||
const cleanup = $effect.root(() => {
|
||||
$effect(() => {
|
||||
log.push(x);
|
||||
});
|
||||
|
||||
const nested_cleanup = $effect.root(() => {
|
||||
return () => {
|
||||
log.push('cleanup 2') ;
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
log.push('cleanup 1');
|
||||
nested_cleanup();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<button on:click={() => x++}>{x}</button>
|
||||
<button on:click={() => y++}>{y}</button>
|
||||
<button on:click={() => cleanup()}>cleanup</button>
|
Loading…
Reference in new issue