-
Notifications
You must be signed in to change notification settings - Fork 82
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
Syntax sugar for reactive declarations #54
Conversation
I was thinking about this again (and create note for my self, You can ignore these texts for now), and it would require:
let x = 5;
let _x = 7;
let y = 4;
$: z = _x + y; (where We probably can:
I think solution 2 is really good. So:
So in result, we will just remove To point 2, I only see one disadvantage, but that disadvantage is already present by this RFC and feature in beginning, and it's "broking" actual valid code (even when I think, it's not used in any real apps). E: when I think about having |
I suggest to add new feature / syntax sugar, that will not use variable for reactive assignments, if it will be prefixed with `_`, so previous code would be possible to rewrite to something like this: | ||
|
||
```js | ||
$: myfunction(callfuncwhenthisvarchange, _dontcallwhenchange); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should show a whole code sample including the declaration of _dontcallwhenchange
, here we don't know if the _
is on the variable name or just in the reactive statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should also specify the syntax when interacting with store content, is it _$dontcallwhenchange
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, as I wrote in comment... about declaration, it still something, we need to think about, but probably it would be good to have it only as prefix in reactive statement, means after compiled, prefix would be removed from statement, to match variable name without prefix. The only issue is, if You write callback in reactive statement, where _
prefix is used naturally. Hmm... will think about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking more about, and what about #
prefix?
what about using control comments? let a = 1, b = 2, c = 3, d;
// do not trigger for a and b
$: { // svelte:skip a, b
d = a + b + c;
}
// trigger only for b
$: { // svelte:only b
d = a + b + c;
}
// and shortening
$: /* svelte:only c */ z = a*a + b*b + c*c; |
using comments to affects what code is produced by transpiler seems like big "MEH"... not to mentoy it's too noisy, too much characters to write. Not even better than React's useEffects... |
I agree, I haven't seen the usage of comments to control compilers, only for warnings. But this way has a good readability and is quite clear. Another thing I was thinking is whitelists and blacklists: let a = 1, b = 2, c = 3, d;
// whitelist
$: (a,b), d = a + b + c;
// blacklist
$: !(c), d = a + b +c; But here labels work only with the comma operator so this approach doesn't fit the reactive block of code. But what if we use a lambda function to define reactive dependencies: let a = 1, b = 2, c = 3, d;
// re-calculated d only when a or b changes
$: (a,b) => {
d = a + b + c;
}
// do not re-calculated d when c changes
$: !(c) => {
d = a + b + c;
} |
Just gonna leave this here sveltejs/svelte#6730 (comment) 👀 |
Such a random idea: <script>
let y=1;
let z=2;
let a=3;
$(y,z): x = y + z + a;
</script> ...where overwriting the I know that the IMPORTANCE:@gtm-nayan This looks cool, but it would break the code because someone might have used it in a different way before. $$: {y,z} {x = y + z + a} To distinguish one from the other. |
Svelte 5 solves this with the runes API. Any dependencies are tracked at runtime, and you can untrack them using the |
Rendered