Improve scheduling of requestIdleCallback tasks (#3477)
This commit is contained in:
parent
3652a39de0
commit
0e12a8dab9
4 changed files with 36 additions and 1 deletions
|
@ -0,0 +1,29 @@
|
|||
// Wrapper to call requestIdleCallback() to schedule low-priority work.
|
||||
// See https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API
|
||||
// for a good breakdown of the concepts behind this.
|
||||
|
||||
import Queue from 'tiny-queue';
|
||||
|
||||
const taskQueue = new Queue();
|
||||
let runningRequestIdleCallback = false;
|
||||
|
||||
function runTasks(deadline) {
|
||||
while (taskQueue.length && deadline.timeRemaining() > 0) {
|
||||
taskQueue.shift()();
|
||||
}
|
||||
if (taskQueue.length) {
|
||||
requestIdleCallback(runTasks);
|
||||
} else {
|
||||
runningRequestIdleCallback = false;
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleIdleTask(task) {
|
||||
taskQueue.push(task);
|
||||
if (!runningRequestIdleCallback) {
|
||||
runningRequestIdleCallback = true;
|
||||
requestIdleCallback(runTasks);
|
||||
}
|
||||
}
|
||||
|
||||
export default scheduleIdleTask;
|
Loading…
Add table
Add a link
Reference in a new issue