From dc1370a495e660be3e3f98c78d9d6b63d3ff265e Mon Sep 17 00:00:00 2001 From: Antonio Stoilkov Date: Mon, 18 Mar 2024 13:50:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=80=20set=203=20seconds=20max=20wait?= =?UTF-8?q?=20time=20for=20the=20idle=20callback=20based=20strategy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ricTracker.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/ricTracker.ts b/src/ricTracker.ts index f854fc6..569729d 100644 --- a/src/ricTracker.ts +++ b/src/ricTracker.ts @@ -26,16 +26,26 @@ class RicTracker { return } - this.#idleCallbackId = requestIdleCallback((deadline) => { - this.#idleDeadline = deadline - this.#idleCallbackId = undefined - - this.#deferred.resolve(deadline) - - this.#deferred = withResolvers() - - this.start() - }) + this.#idleCallbackId = requestIdleCallback( + (deadline) => { + this.#idleDeadline = deadline + this.#idleCallbackId = undefined + + this.#deferred.resolve(deadline) + + this.#deferred = withResolvers() + + this.start() + }, + { + // wait 3 seconds max to call the next idle callback: + // - if the browser is actually busy for 3 seconds, we can at least call + // rarely and probably the `deadline` will have very little time left + // - previously Chromium had a bug where it wouldn't fire the idle + // callback. the timeout just in case some browser has such a problem. + timeout: 3000, + }, + ) } stop() {