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 statements and unexpected line execution order #4516

Closed
pushkine opened this issue Mar 6, 2020 · 4 comments
Closed

Reactive statements and unexpected line execution order #4516

pushkine opened this issue Mar 6, 2020 · 4 comments

Comments

@pushkine
Copy link
Contributor

pushkine commented Mar 6, 2020

The compiler reorganizes the order at which lines are run such that reactive statements are only run at the end, that already by itself creates unexpected output such as :

let value = "hi"
$: value = "hello";
console.log(value) // prints "hi"

but then, when you'd expect reactive statements to at least keep the right order between themselves, this happens:

let value = "hi"
$: console.log(value) // prints "hello"
$: value = "hello";

the current component initialization order is the following :

  1. run static statements
  2. run "one time" dynamic statements
  3. run dynamic statements

related : #4371

@Conduitry
Copy link
Member

I don't think either of these are bugs, but they're probably something we should document somewhere.

The second one definitely isn't a bug, because we need to topologically sort the reactive blocks according to what depends on what. The first reactive block depends on value and the second one assigns value so the second one happens first during each update loop.

Given that we're reordering the blocks, having them be 'inline' with the rest of rest of the static top-level statements doesn't really make sense (not to mention the question of whether it would be considered a breaking change).

@Conduitry Conduitry added the docs label Mar 6, 2020
@pushkine
Copy link
Contributor Author

pushkine commented Mar 6, 2020

What about adding a dev warning for reactive statements that aren't reactive ?
I mean what do they do apart from relocating your code toward the end of the block ? You could do that yourself anyway so what's the point of even allowing them in the first place ?

And yes that really should be highlighted in the docs since being able to pool together static top level and reactive statements heavily implies that they can interact with each other

edit : another idea could be to have the officially supported formatter reorder the lines in the order they run in

@tivac
Copy link
Contributor

tivac commented Mar 7, 2020

I wrote a custom lint rule that tries to identify static assignments in reactive blocks, it's not very smart but it helped us catch a couple instances of this sort of thing.

@dummdidumm
Copy link
Member

Closing because reactive statements are deprecated in Svelte 5

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

Successfully merging a pull request may close this issue.

7 participants