Skip to content

Commit

Permalink
✨ 🐎 don't start the next task if input is still pending
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Jun 13, 2024
1 parent 28fadcc commit f6c956c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/WorkCycleTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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()
}
Expand All @@ -52,4 +53,8 @@ export default class WorkCycleTracker {
}
return -1
}

#isInputPending(): boolean {
return navigator.scheduling?.isInputPending?.() === true
}
}

0 comments on commit f6c956c

Please sign in to comment.