From f6c956c35e6ae0f1d9dccf74666f1e875f4d4a6c Mon Sep 17 00:00:00 2001 From: Antonio Stoilkov Date: Thu, 13 Jun 2024 15:52:15 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=F0=9F=90=8E=20don't=20start=20the?= =?UTF-8?q?=20next=20task=20if=20input=20is=20still=20pending?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WorkCycleTracker.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/WorkCycleTracker.ts b/src/WorkCycleTracker.ts index 9658bf8..47fc510 100644 --- a/src/WorkCycleTracker.ts +++ b/src/WorkCycleTracker.ts @@ -17,21 +17,22 @@ export default class WorkCycleTracker { } canWorkMore(task: SchedulingTask): boolean { - const isInputPending = navigator.scheduling?.isInputPending?.() === true - return !isInputPending && this.#calculateDeadline(task) - Date.now() > 0 + return !this.#isInputPending() && this.#calculateDeadline(task) - Date.now() > 0 } async nextWorkCycle(task: SchedulingTask): Promise { - if (task.type === 'frame-based') { - await Promise.race([frameTracker.waitAfterFrame(), waitHiddenTask()]) - } else if (task.type === 'idle-based') { - if (ricTracker.available) { - await ricTracker.waitIdleCallback() - } else { - // todo: use waitHiddenTask() with a timeout - await frameTracker.waitAfterFrame() + do { + if (task.type === 'frame-based') { + await Promise.race([frameTracker.waitAfterFrame(), waitHiddenTask()]) + } else if (task.type === 'idle-based') { + if (ricTracker.available) { + await ricTracker.waitIdleCallback() + } else { + // todo: use waitHiddenTask() with a timeout + await frameTracker.waitAfterFrame() + } } - } + } while (this.#isInputPending()) this.#workCycleStart = Date.now() } @@ -52,4 +53,8 @@ export default class WorkCycleTracker { } return -1 } + + #isInputPending(): boolean { + return navigator.scheduling?.isInputPending?.() === true + } }