fix: ensure tick resolves within a macrotask (#16825)

Race them against each other - in almost all cases requestAnimationFrame will fire first, but e.g. in case the window is not focused or a view transition happens, requestAnimationFrame will be delayed and setTimeout helps us resolve fast enough in that case

Fixes #16429
Fixes https://github.com/sveltejs/kit/issues/14220
pull/16737/merge
Simon H 1 day ago committed by GitHub
parent 5e6fed6bab
commit 1ef297f25d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure tick resolves within a macrotask

@ -500,7 +500,13 @@ export function update_effect(effect) {
*/ */
export async function tick() { export async function tick() {
if (async_mode_flag) { if (async_mode_flag) {
return new Promise((f) => requestAnimationFrame(() => f())); return new Promise((f) => {
// Race them against each other - in almost all cases requestAnimationFrame will fire first,
// but e.g. in case the window is not focused or a view transition happens, requestAnimationFrame
// will be delayed and setTimeout helps us resolve fast enough in that case
requestAnimationFrame(() => f());
setTimeout(() => f());
});
} }
await Promise.resolve(); await Promise.resolve();

Loading…
Cancel
Save