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

Reactive assignment doesn't work when dependent variable is hidden in function #2956

Closed
ChrisS85 opened this issue Jun 5, 2019 · 7 comments

Comments

@ChrisS85
Copy link

ChrisS85 commented Jun 5, 2019

Broken or unsupported
Working

For more enhanced or reusable calculations it would be nice to be able to wrap the reactive assignements inside functions. I didn't find any information about this limitation in the tutorial or the documentation but maybe I missed it.

@Conduitry
Copy link
Member

This is intentional actually, but perhaps it should be documented. The functions themselves could also be reassigned at runtime, which would break any dependence that we could figure out at compile time. And, although it's not generally recommended, it's also occasionally convenient to be able to 'hide' dependencies from the compiler.

@Conduitry
Copy link
Member

But also, if you do want to use separate functions and have your dependencies still counted, you can make all of the relevant dependencies be arguments to those functions, so the compiler can see them in the reactive block.

@Conduitry Conduitry added the docs label Jun 5, 2019
@ChrisS85
Copy link
Author

ChrisS85 commented Jun 5, 2019

But also, if you do want to use separate functions and have your dependencies still counted, you can make all of the relevant dependencies be arguments to those functions, so the compiler can see them in the reactive block.

That's actuually what I'm doing now and it works for me. Thanks for the clarification!
Maybe a warning could be emitted if a function call is used for a reactive assignement and there are no other dependent variables in the expression?

@BennyHinrichs
Copy link

If I understand it right, in JS there are four immutable data types: number, string, boolean, and undefined. Everything else is just a reference. So in your "unsupported" example, you're assigning the reactive variable to a reference to an object (functions are objects in JS, yay!). That reference never changes, so your value variable never changes.

Where it behaves differently than I'd expect is that @Conduitry says you can pass the variables as arguments, and value will get updated. That reference still isn't changing, but it apparently updates. Is that a feature of the compiler then?

@btk5h
Copy link
Contributor

btk5h commented Jun 29, 2019

@BennyHinrichs I think you have a misunderstanding about how the compiler interprets reactive assignments.

The compiler looks for the dependencies of a reactive statement by determining which reactive variables are read. In the "unsupported" example, even though f() reads the reactive variable count in its body, because the variable count isn't present in the reactive statement itself, the statement is not seen as having a dependency on count.

@lukejoliat
Copy link

@Conduitry I'd be happy to take on this issue, if no one else has up to this point!

@dummdidumm
Copy link
Member

This is documented now: https://svelte.dev/docs#3_$_marks_a_statement_as_reactive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants