Skip to content

Commit

Permalink
fix: correctly compile $effect.root in svelte modules (#12315)
Browse files Browse the repository at this point in the history
Fixes #12222
  • Loading branch information
trueadm authored Jul 5, 2024
1 parent b2448dc commit b3c002a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smart-fans-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly compile $effect.root in svelte modules
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ const global_visitors = {
return b.literal(false);
}

if (rune === '$effect.root') {
const args = /** @type {import('estree').Expression[]} */ (
node.arguments.map((arg) => context.visit(arg))
);
// Just call the function directly
return b.call(args[0]);
}

if (rune === '$state.snapshot') {
return /** @type {import('estree').Expression} */ (context.visit(node.arguments[0]));
}
Expand Down Expand Up @@ -571,7 +579,7 @@ const javascript_visitors_runes = {
for (const declarator of node.declarations) {
const init = declarator.init;
const rune = get_rune(init, state.scope);
if (!rune || rune === '$effect.tracking' || rune === '$inspect') {
if (!rune || rune === '$effect.tracking' || rune === '$inspect' || rune === '$effect.root') {
declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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']);
}
});
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit b3c002a

Please sign in to comment.