Constructor

Queue

new Queue(name, options)

Creates an instance of Queue with the given options

Parameter

name

string

The unique name for this queue. This name must be unique as it's used to register sync events and store requests in IndexedDB specific to this instance. An error will be thrown if a duplicate name is detected.

options

Optional

Object

Values in options have the following properties:

Parameter

onSync

Optional

function()

A function that gets invoked whenever the 'sync' event fires. The function is invoked with an object containing the queue property (referencing this instance), and you can use the callback to customize the replay behavior of the queue. When not set the replayRequests() method is called. Note: if the replay fails after a sync event, make sure you throw an error, so the browser knows to retry the sync event later.

maxRetentionTime

Optional

number

The amount of time (in minutes) a request may be retried. After this amount of time has passed, the request will be deleted from the queue.

Property

name

Returns

string 

Methods

getAll

async

getAll() returns Promise containing Array of Object

Returns all the entries that have not expired (per maxRetentionTime). Any expired entries are removed from the queue.

Returns

Promise containing Array of Object 

popRequest

async

popRequest() returns Promise containing Object

Removes and returns the last request in the queue (along with its timestamp and any metadata). The returned object takes the form: {request, timestamp, metadata}.

Returns

Promise containing Object 

pushRequest

async

pushRequest(entry)

Stores the passed request in IndexedDB (with its timestamp and any metadata) at the end of the queue.

Parameter

entry

Object

Values in entry have the following properties:

Parameter

request

Request

The request to store in the queue.

metadata

Optional

Object

Any metadata you want associated with the stored request. When requests are replayed you'll have access to this metadata object in case you need to modify the request beforehand.

timestamp

Optional

number

The timestamp (Epoch time in milliseconds) when the request was first added to the queue. This is used along with maxRetentionTime to remove outdated requests. In general you don't need to set this value, as it's automatically set for you (defaulting to Date.now()), but you can update it if you don't want particular requests to expire.

registerSync

async

registerSync()

Registers a sync event with a tag unique to this instance.

replayRequests

async

replayRequests()

Loops through each request in the queue and attempts to re-fetch it. If any request fails to re-fetch, it's put back in the same position in the queue (which registers a retry for the next sync event).

shiftRequest

async

shiftRequest() returns Promise containing Object

Removes and returns the first request in the queue (along with its timestamp and any metadata). The returned object takes the form: {request, timestamp, metadata}.

Returns

Promise containing Object 

unshiftRequest

async

unshiftRequest(entry)

Stores the passed request in IndexedDB (with its timestamp and any metadata) at the beginning of the queue.

Parameter

entry

Object

Values in entry have the following properties:

Parameter

request

Request

The request to store in the queue.

metadata

Optional

Object

Any metadata you want associated with the stored request. When requests are replayed you'll have access to this metadata object in case you need to modify the request beforehand.

timestamp

Optional

number

The timestamp (Epoch time in milliseconds) when the request was first added to the queue. This is used along with maxRetentionTime to remove outdated requests. In general you don't need to set this value, as it's automatically set for you (defaulting to Date.now()), but you can update it if you don't want particular requests to expire.