Skip to content

Commit 6d6547d

Browse files
committed
fix: ensure tick resolves within a macrotask
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 sveltejs/kit#14220
1 parent 5e6fed6 commit 6d6547d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

.changeset/wise-bottles-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure tick resolves within a macrotask

packages/svelte/src/internal/client/runtime.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,13 @@ export function update_effect(effect) {
500500
*/
501501
export async function tick() {
502502
if (async_mode_flag) {
503-
return new Promise((f) => requestAnimationFrame(() => f()));
503+
return new Promise((f) => {
504+
// Race them against each other - in almost all cases requestAnimationFrame will fire first,
505+
// but e.g. in case the window is not focused or a view transition happens, requestAnimationFrame
506+
// will be delayed and setTimeout helps us resolve fast enough in that case
507+
requestAnimationFrame(() => f());
508+
setTimeout(() => f());
509+
});
504510
}
505511

506512
await Promise.resolve();

0 commit comments

Comments
 (0)