mirror of https://github.com/sveltejs/svelte
parent
b2448dcc5c
commit
b3c002a703
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: correctly compile $effect.root in svelte modules
|
@ -0,0 +1,30 @@
|
|||||||
|
import { flushSync } from 'svelte';
|
||||||
|
import { test } from '../../test';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
html: '<button>0</button><button>0</button><button>cleanup</button>',
|
||||||
|
|
||||||
|
async test({ assert, target, logs }) {
|
||||||
|
const [b1, b2, b3] = target.querySelectorAll('button');
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
b1.click();
|
||||||
|
b2.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(logs, [0, 1]);
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
b3.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(logs, [0, 1, 'cleanup 1', 'cleanup 2']);
|
||||||
|
|
||||||
|
flushSync(() => {
|
||||||
|
b1.click();
|
||||||
|
b2.click();
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(logs, [0, 1, 'cleanup 1', 'cleanup 2']);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,11 @@
|
|||||||
|
<script>
|
||||||
|
import { with_root } from './root.svelte.js';
|
||||||
|
let x = $state(0);
|
||||||
|
let y = $state(0);
|
||||||
|
|
||||||
|
const cleanup = with_root(() => x)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button onclick={() => x++}>{x}</button>
|
||||||
|
<button onclick={() => y++}>{y}</button>
|
||||||
|
<button onclick={cleanup}>cleanup</button>
|
@ -0,0 +1,20 @@
|
|||||||
|
export function with_root(get_x) {
|
||||||
|
const cleanup = $effect.root(() => {
|
||||||
|
$effect(() => {
|
||||||
|
console.log(get_x());
|
||||||
|
});
|
||||||
|
|
||||||
|
const nested_cleanup = $effect.root(() => {
|
||||||
|
return () => {
|
||||||
|
console.log('cleanup 2');
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
console.log('cleanup 1');
|
||||||
|
nested_cleanup();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return cleanup;
|
||||||
|
}
|
Loading…
Reference in new issue