From f486281ef2a9c2f3a048dfc6bf46872f67cdc521 Mon Sep 17 00:00:00 2001 From: artwooood <39819339+artwooood@users.noreply.github.com> Date: Mon, 11 Mar 2024 17:24:24 +0700 Subject: [PATCH] fix(common): use `setTimeout` as fallback for `requestIdleCallback` (#2406) Co-authored-by: Kevin Ingersoll --- .changeset/cold-books-love.md | 5 +++++ packages/common/src/utils/waitForIdle.ts | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 .changeset/cold-books-love.md diff --git a/.changeset/cold-books-love.md b/.changeset/cold-books-love.md new file mode 100644 index 0000000000..4d46d1de61 --- /dev/null +++ b/.changeset/cold-books-love.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/common": patch +--- + +`waitForIdle` now falls back to `setTimeout` for environments without `requestIdleCallback`. diff --git a/packages/common/src/utils/waitForIdle.ts b/packages/common/src/utils/waitForIdle.ts index 7dd4763cf8..2357965800 100644 --- a/packages/common/src/utils/waitForIdle.ts +++ b/packages/common/src/utils/waitForIdle.ts @@ -1,5 +1,9 @@ export function waitForIdle(): Promise { return new Promise((resolve) => { - requestIdleCallback(() => resolve()); + if (typeof requestIdleCallback !== "undefined") { + requestIdleCallback(() => resolve()); + } else { + setTimeout(() => resolve(), 1); + } }); }