Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Latest commit

 

History

History
143 lines (128 loc) · 5.24 KB

IdleQueue.md

File metadata and controls

143 lines (128 loc) · 5.24 KB

IdleQueue.mjs

idlize/IdleQueue.mjs

Overview

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.

Exports

Usage

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...
});

IdleQueue

Methods

Name Description
constructor(options)

Parameters:

  • options.ensureTasksRun (boolean) Adds Page Lifecycle callbacks to ensure the queue is run before the user leaves the page (default: false).
  • options.defaultMinTaskTime: (number) The default amount of idle time remaining in order for a task to be run (default: 0).
pushTask(task, options)

Parameters:

  • task: (function(Object)) The task to add to the end of the queue.
  • options.minTaskTime: (number) The minimum amount of idle time remaining in order for a task to be run. If no value is passed, the queue default is used.

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:

  • time: (number) The time (epoch time in milliseconds) when the task was added to the queue.
  • visibilityState: (string) The visibility state of the document when the task was added to the queue.
unshiftTask(task, options)

Parameters:

  • task: (function(Object<{{time: number, visibilityState: string}}>)) The task to add to the beginning of the queue.
  • options.minTaskTime: (number) The minimum amount of idle time remaining in order for a task to be run. If no value is passed, the queue default is used.

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:

  • time: (number) The time (epoch time in milliseconds) when the task was added to the queue.
  • visibilityState: (string) The visibility state of the document when the task was added to the queue.
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)

  • {time}: (number) The time (milliseconds, in epoch time) the task was added to the queue.
  • {visibilityState}: (string) The document's visibility state when the task was added to the queue.