You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m currently following the official Svelte examples in the Dexie.js documentation, which uses very outdated Svelte3/4 syntax, and converting them to Svelte 5. While doing this, I ran into the issue of tracking state inside deferred async callbacks (like Dexie queries, RxJS observables, or even standard setTimeout calls).
The current Svelte docs for $effect and $derived clearly state the limitation (in "Understanding dependencies"):
"Values that are read asynchronously — after an await or inside a setTimeout, for example — will not be tracked."
However, they don't explicitly show the idiomatic workaround for when you do need those values tracked. When bridging Svelte's synchronous reactivity with deferred closures, developers have to capture the state synchronously before the async boundary.
Since some others may hit this same wall, I was thinking of opening a PR to add a small clarification to both the $effect and $derived sections in the Svelte docs to show this pattern.
For instance, in the $effect section (after the current setTimeout example), this may be added:
To ensure the effect re-runs when an asynchronously read value changes, you must explicitly read it synchronously before the asynchronous boundary. You can do this by using the void operator (our use watch() in runed library):
$effect(()=>{// ...canvas setup...// Read `size` synchronously so Svelte registers it as a dependencyvoidsize;consttimeoutId=setTimeout(()=>{context.fillRect(0,0,size,size);},0);return()=>{clearTimeout(timeout);};});
And for the $derived section (after the untrack explanation):
Similarly, if you use $derived.by to return a deferred callback or observable, you must read dependencies synchronously in the main body:
Before I open a PR to the Svelte repo, I wanted to float this here:
Is void stateToTrack1, stateToTrack2,...; considered the most idiomatic way to express this intent in Svelte 5 without using library like runed's watch()?
Would this addition be welcome in the official docs to help people migrating from Svelte 4's compiler-based tracking?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m currently following the official Svelte examples in the Dexie.js documentation, which uses very outdated Svelte3/4 syntax, and converting them to Svelte 5. While doing this, I ran into the issue of tracking state inside deferred async callbacks (like Dexie queries, RxJS observables, or even standard setTimeout calls).
The current Svelte docs for
$effectand$derivedclearly state the limitation (in "Understanding dependencies"):"Values that are read asynchronously — after an await or inside a setTimeout, for example — will not be tracked."
However, they don't explicitly show the idiomatic workaround for when you do need those values tracked. When bridging Svelte's synchronous reactivity with deferred closures, developers have to capture the state synchronously before the async boundary.
Since some others may hit this same wall, I was thinking of opening a PR to add a small clarification to both the
$effectand$derivedsections in the Svelte docs to show this pattern.For instance, in the
$effectsection (after the current setTimeout example), this may be added:To ensure the effect re-runs when an asynchronously read value changes, you must explicitly read it synchronously before the asynchronous boundary. You can do this by using the void operator (our use
watch()in runed library):And for the
$derivedsection (after the untrack explanation):Similarly, if you use $derived.by to return a deferred callback or observable, you must read dependencies synchronously in the main body:
Before I open a PR to the Svelte repo, I wanted to float this here:
void stateToTrack1, stateToTrack2,...;considered the most idiomatic way to express this intent in Svelte 5 without using library like runed'swatch()?(Related conversation on Discord for context: https://discord.com/channels/457912077277855764/1526086042258772109)
Beta Was this translation helpful? Give feedback.
All reactions