Skip to content

feat: add $effect.active rune #9591

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

Merged
merged 2 commits into from
Nov 22, 2023
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/popular-mangos-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: add $effect.active rune
11 changes: 11 additions & 0 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,17 @@ function validate_call_expression(node, scope, path) {
}

if (rune === '$effect') {
const callee = node.callee;
if (
callee.type === 'MemberExpression' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'active'
) {
if (node.arguments.length !== 0) {
error(node, 'invalid-rune-args-length', '$effect.active', [0]);
}
return;
}
if (parent.type !== 'ExpressionStatement') {
error(node, 'invalid-effect-location');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const javascript_visitors_runes = {
for (const declarator of node.declarations) {
const init = declarator.init;
const rune = get_rune(init, state.scope);
if (!rune) {
if (!rune || rune === '$effect') {
if (init != null && is_hoistable_function(init)) {
const hoistable_function = visit(init);
state.hoisted.push(
Expand Down Expand Up @@ -291,5 +291,19 @@ export const javascript_visitors_runes = {
}

context.next();
},
CallExpression(node, { state, next }) {
const rune = get_rune(node, state.scope);
const callee = node.callee;

if (
rune === '$effect' &&
callee.type === 'MemberExpression' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'active'
) {
return b.call('$.effect_active');
}
next();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
EACH_KEYED
} from '../../../../../constants.js';
import { regex_is_valid_identifier } from '../../../patterns.js';
import { javascript_visitors_runes } from './javascript-runes.js';

/**
* Serializes each style directive into something like `$.style(element, style_property, value)`
Expand Down Expand Up @@ -2921,5 +2922,6 @@ export const template_visitors = {
...context.state,
node: b.id('$.document')
});
}
},
CallExpression: javascript_visitors_runes.CallExpression
};
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ const javascript_visitors_runes = {

for (const declarator of node.declarations) {
const rune = get_rune(declarator.init, state.scope);
if (!rune) {
if (!rune || rune === '$effect') {
declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));
continue;
}
Expand Down Expand Up @@ -604,6 +604,20 @@ const javascript_visitors_runes = {
}
}
context.next();
},
CallExpression(node, { state, next }) {
const rune = get_rune(node, state.scope);
const callee = node.callee;

if (
rune === '$effect' &&
callee.type === 'MemberExpression' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'active'
) {
return b.literal(false);
}
next();
}
};

Expand Down Expand Up @@ -1579,7 +1593,9 @@ const template_visitors = {
b.stmt(b.call('$.head', b.id('$$payload'), b.arrow([b.id('$$payload')], b.block(body))))
)
);
}
},
// @ts-ignore: need to extract this out somehow
CallExpression: javascript_visitors_runes.CallExpression
};

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,13 @@ function internal_create_effect(type, init, sync, block, schedule) {
return signal;
}

/**
* @returns {boolean}
*/
export function effect_active() {
return current_effect ? (current_effect.f & MANAGED) === 0 : false;
}

/**
* @param {() => void | (() => void)} init
* @returns {import('./types.js').EffectSignal}
Expand Down
3 changes: 2 additions & 1 deletion packages/svelte/src/internal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export {
onDestroy,
pop,
push,
reactive_import
reactive_import,
effect_active
} from './client/runtime.js';

export * from './client/validate.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test } from '../../test';

export default test({
ssrHtml: `
<p>false</p>
<p>false</p>
<p>false</p>
`,

html: `
<p>false</p>
<p>true</p>
<p>true</p>
`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
const foo = $effect.active();
let bar = $state(false);
$effect.pre(() => {
bar = $effect.active();
});
</script>

<p>{foo}</p>
<p>{bar}</p>
<p>{$effect.active()}</p>