News for hackers. Stuff that matters.
Re:Thoughts on Svelte by pcthrowaway on Monday March 27, 2023 @04:26PM (#35331976)
Interesting, my memory of reading about the Svelte magic reactivity was that it was pretty well explained that the only things 'reactive' would be things which were assigned to in the reactive statement.

So if you want to update y on some changes to x, you do

    $: y = x

And if you need something more complicated, a transformation perhaps, you put the logic in a function and assign y to the result of that

    function parseX(val) { ... }
    $: y = parseX(x)
Re:Thoughts on Svelte by opensas on Monday March 27, 2023 @04:46PM (#35332292)
top level variables are reactive even when updated inside a function, just give it a try:

https://svelte.dev/repl/44e3aece18294ddb834bcdfb4394af48?ver...

` <script>

  let name = 'world';

  const flip = () => name = name === name.toUpperCase() ? name.toLowerCase() : name.toUpperCase()

  $: console.log({ name })
</script>

<h1>Hello {name}!</h1>

<button on:click={flip}>flip!</button> `

Re:Thoughts on Svelte by opensas on Monday March 27, 2023 @04:47PM (#35332322)
the inconsistency comes from the fact that this reactivity, triggered from inside a function, is not taken into account to build the dependency graph and ultimately decide in which order to process the statements