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

[BUG] Dependence of reactivity on the order of declaration #5525

Closed
rodshtein opened this issue Oct 13, 2020 · 6 comments
Closed

[BUG] Dependence of reactivity on the order of declaration #5525

rodshtein opened this issue Oct 13, 2020 · 6 comments

Comments

@rodshtein
Copy link

Reactive statement declaration through $: label before the second reactive statement that changes the first one through function is not reactive;

REPL: https://ru.svelte.dev/repl/7a9593fcd21848f7bdac90bd93fb2f9e?version=3.29.0

image

@pushkine
Copy link
Contributor

duplicate #4516

@rodshtein
Copy link
Author

rodshtein commented Oct 13, 2020

duplicate #4516

I wouldn't be so sure.


To be clear: without function all works fine ↓

image

@pushkine
Copy link
Contributor

pushkine commented Oct 13, 2020

yep my bad, it works as expected then,
there's two unintuitive things happening here, 1) the compiler does not link changes made in functions to the reactive declarations they're called from and 2) reactive declarations only run once per tick even if further updates happen within them

the compiler will try to re-order the executing order of declarations depending on each other ( so in the second screenshot, foo,bar++ is reordered to run first ) but doesn't in this case as it does not take into account mutations made within functions

You can check the executing order under the JS compiled tab at $$self.$$.update = () =>

@Conduitry
Copy link
Member

Indeed, this is the expected behavior. When the compiler sorts reactive declarations/blocks according to dependencies, it only looks at variables referenced and assigned to within the block itself. This lets you specifically 'hide' references and updates from the compiler, and also makes the whole ordering process much more feasible.

@rodshtein
Copy link
Author

rodshtein commented Oct 13, 2020

Indeed, this is the expected behavior.

@Conduitry no is not. Is surprising behavior.
Why is't any word about in docs?

@PaulMaly
Copy link
Contributor

PaulMaly commented Oct 13, 2020

@Conduitry I'm not sure, but I think you didn't catch the whole problem described by example. Perhaps, because this example doesn't fully reflect the problem and concentrates on unnecessary things.

Let's say, we've a much cleaner example:

<script>
  let foo = 0, 
      bar = 0;
	
  $: bar, alert();
  $: foo, func();

  function func() {
    bar += 2;
  } 
</script>

{foo} {bar}
<button on:click={e => ++foo}>
  Increment
</button>

The alert() will fire only once here, no matter that actual bar value will be changed as many times as you like. DOM updates will be performed as appropriate, but the reactive expression won't. So, here we can't say "alert() will be performed each time dependency (bar value) changed". But worst of all, if we swap these 2 reactive expressions, we'll get the converse effect.

I know why it's happening, but is it really not a problem? @Rich-Harris

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

No branches or pull requests

4 participants