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

Svelte 5: $runes reactivity doesn't work inside setTimeout. #10264

Closed
HighFunctioningSociopathSH opened this issue Jan 23, 2024 · 2 comments · Fixed by #10267
Closed

Svelte 5: $runes reactivity doesn't work inside setTimeout. #10264

HighFunctioningSociopathSH opened this issue Jan 23, 2024 · 2 comments · Fixed by #10267
Assignees

Comments

@HighFunctioningSociopathSH
Copy link

HighFunctioningSociopathSH commented Jan 23, 2024

Describe the bug

reactivity is only triggered with assignment inside setTimeout and other methods of updating the $state, for example, the push method of arrays results in no reaction. This seems to happen when using $inspect

Reproduction

  let numbers = $state<number[]>([1, 2, 3]);

  setTimeout(() => {
    // numbers = [1]; This works and $inspect correctly logs the updated value.
    numbers.push(numbers.length + 1) // But this doesn't work and $inspect logs nothing.
  }, 3000);

  $inspect(numbers);

Logs

No response

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-12650H
    Memory: 7.82 GB / 15.63 GB
  Binaries:
    Node: 18.14.2 - C:\Program Files\nodejs\node.EXE
    npm: 9.7.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (120.0.2210.144)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    svelte: ^5.0.0-next.37 => 5.0.0-next.37

Severity

annoyance

@rmunn
Copy link
Contributor

rmunn commented Jan 23, 2024

Note that if you have a <p>numbers: {numbers}</p> element, it updates correctly after the timeout (repro). Only $inspect is failing in this repro.

@rmunn
Copy link
Contributor

rmunn commented Jan 23, 2024

Also, it only fails once. If you duplicate that setTimeout three times, you'll see the following logged:

  • init [1, 2, 3]
  • update [1, 2, 3, 4, 5]
  • update [1, 2, 3, 4, 5, 6]

The only update that $inspect fails to log is the first one, where 4 is pushed. Here's another repro that pushes 1000, 2000 and 3000 so it's more obvious which setTimeout call is causing which update. The first setTimeout to run is the one that $inspect doesn't see; the others are correctly showing up in console logs.

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

Successfully merging a pull request may close this issue.

3 participants