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

Assignment in if disables reactivity when it evaluates to false #3467

Closed
zwz opened this issue Aug 28, 2019 · 3 comments
Closed

Assignment in if disables reactivity when it evaluates to false #3467

zwz opened this issue Aug 28, 2019 · 3 comments
Labels

Comments

@zwz
Copy link

zwz commented Aug 28, 2019

It's a tricky corner case. I don't know how to describe it clearly.
But it is quite easy to reproduce and demonstrate itself.
Just open the writable store example (https://svelte.dev/examples#writable-stores),
and replace count_value = value (line 10) with

if(count_value = value)
  console.log(count_value)

You will see that it is not reactive when the value is 0.

@zwz zwz changed the title Assignment in if that evaluates to false disables reactivity Assignment in if disables reactivity when it evaluates to false Aug 28, 2019
@Conduitry Conduitry added the bug label Aug 28, 2019
@Conduitry
Copy link
Member

The $$invalidate call in the generated code is getting stuck inside the body of the if block, and so is only getting called when the value is truthy. I'm unsure whether it would be best to call it inside the if condition or after the whole if block.

@mrkishi
Copy link
Member

mrkishi commented Aug 30, 2019

I believe it couldn't go after the if block since a return or error after the assignment would bypass the invalidation.

What if $$invalidate returned the passed in value so that all assignments could be replaced as-is?

if (name = stuff) {}
if ($$invalidate('name', name = stuff)) {}

fn(name = value);
fn(name = value); $$invalidate('name', value);
fn($$invalidate('name', name = value));

const fn = (value) => name = value;
const fn = (value) => { const $$result = name = value; $$invalidate('name', name); return $$result; };
const fn = (value) => $$invalidate('name', name = value);

@Conduitry
Copy link
Member

It looks like this was also fixed in 3.10.1 by #3533.

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

No branches or pull requests

3 participants