Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly compile $effect.root in svelte modules #12315

Merged
merged 5 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading