cleanup logic

pull/9638/head
Dominic Gannaway 3 years ago
parent d7367dc522
commit e13b35e069

@ -176,8 +176,6 @@ const runes = {
'invalid-state-location': () => 'invalid-state-location': () =>
`$state() can only be used as a variable declaration initializer or a class field`, `$state() can only be used as a variable declaration initializer or a class field`,
'invalid-effect-location': () => `$effect() can only be used as an expression statement`, 'invalid-effect-location': () => `$effect() can only be used as an expression statement`,
'invalid-effect-root-location': () =>
`$effect.root() can only be used as a variable declaration initializer`,
/** /**
* @param {boolean} is_binding * @param {boolean} is_binding
* @param {boolean} show_details * @param {boolean} show_details

@ -524,8 +524,6 @@ function validate_call_expression(node, scope, path) {
if (node.arguments.length < 1 || node.arguments.length > 2) { if (node.arguments.length < 1 || node.arguments.length > 2) {
error(node, 'invalid-rune-args-length', '$effect.root', [1, 2]); error(node, 'invalid-rune-args-length', '$effect.root', [1, 2]);
} }
if (parent.type === 'VariableDeclarator') return;
error(node, 'invalid-effect-root-location');
} }
} }

@ -135,7 +135,7 @@ export const javascript_visitors_runes = {
for (const declarator of node.declarations) { for (const declarator of node.declarations) {
const init = declarator.init; const init = declarator.init;
const rune = get_rune(init, state.scope); const rune = get_rune(init, state.scope);
if (!rune || rune === '$effect.active') { if (!rune || rune === '$effect.active' || rune === '$effect.root') {
if (init != null && is_hoistable_function(init)) { if (init != null && is_hoistable_function(init)) {
const hoistable_function = visit(init); const hoistable_function = visit(init);
state.hoisted.push( state.hoisted.push(
@ -209,17 +209,6 @@ export const javascript_visitors_runes = {
continue; continue;
} }
const args = /** @type {import('estree').CallExpression} */ (declarator.init).arguments; const args = /** @type {import('estree').CallExpression} */ (declarator.init).arguments;
if (rune === '$effect.root') {
const serialized_args = /** @type {import('estree').Expression[]} */ (
args.map((arg) => visit(arg))
);
declarations.push(
b.declarator(declarator.id, b.call('$.user_root_effect', ...serialized_args))
);
continue;
}
const value = const value =
args.length === 0 args.length === 0
? b.id('undefined') ? b.id('undefined')

@ -191,8 +191,6 @@ This allows you to (for example) add things like subscriptions without causing m
The `$effect.root` rune is an advanced feature that creates a non-tracked scope that doesn't auto-cleanup. This is useful for The `$effect.root` rune is an advanced feature that creates a non-tracked scope that doesn't auto-cleanup. This is useful for
nested effects that you want to manually control. This rune also allows for creation of effects outside of the component initialisation phase. nested effects that you want to manually control. This rune also allows for creation of effects outside of the component initialisation phase.
> `$effect.root` can only be used in variable declaration initializer, this is to ensure the return signature (the cleanup function) is always used.
```svelte ```svelte
<script> <script>
let count = $state(0); let count = $state(0);

Loading…
Cancel
Save