The IdleQueue
class is a helper that allows developers to implement the idle-until-urgent pattern in their code. It's useful for apps that want to split up their logic into a sequence of functions and schedule them to run idly.
This class offers a few benefits over the regular usage of requestIdleCallback()
:
- The queue can be configured so all queued functions are guaranteed to run before the page is unloaded.
- Queued tasks can be run immediately at any time.
- Queued tasks can pass a minimum time budget, below which they won't attempt to run (this minimum time budget can also be configured per queue).
- Queued tasks store the time/visibilityState when they were added to the queue, and are invoked with this data when run.
import {IdleQueue} from 'idlize/IdleQueue.mjs';
const queue = new IdleQueue();
queue.pushTask(() => {
// Some expensive function that can run idly...
});
queue.pushTask(() => {
// Some other task that depends on the above
// expensive function having already run...
});
Name | Description |
---|---|
constructor(options) |
Parameters:
|
pushTask(task, options) |
Parameters:
Adds a task to the end of the queue and schedules the queue to be run when next idle (if not already scheduled). When the task is run, it's invoked with an object containing the following properties:
|
unshiftTask(task, options) |
Parameters:
Adds a task to the beginning of the queue and schedules the queue to be run when next idle (if not already scheduled). When the task is run, it's invoked with an object containing the following properties:
|
runTasksImmediately() |
Runs all queued tasks immediately (synchronously). |
hasPendingTasks() |
Returns: (boolean) True if the queue has any tasks not yet run. |
clearPendingTasks() |
Unschedules all pending tasks in the queue. |
getState() |
Returns: (Object)
|