Skip to main content

useQueue


Introduction

This hook is created to control the dispatcher queues. It uses the Dispatcher to read the actual value of the queue based on the queueKey retrieved from command passed as first argument.

The minimum requirement to use useQueue is to have a prepared Command.


Initialization

const { requests, stopped, stop, start, pause } = useQueue(getUsers);

How it works?

useQueue use the command to read the dispatcher queue value based on it's queueKey. It uses the events emitted by dispatcher to keep this hook up to date. It is giving you the possibilities to controll every single request made in your application, and possibilities to manipulate whole queues and single requests. It works the best with the one-by-one dispatching mode, when command queued option is set to true.

caution

Remember to make sure your command queueKey is static when you use this hook, because of the auto-generation of keys it may return wrong values if your command has the params or query params used. To avoid it, it is recommended to provide custom queueKey when command gets created.

const { requests, stopped, stop, start, pause } = useQueue(getUsers);

requests.forEach((req) => {
req.stopRequest();
req.startRequest();
});

stop();
pause();
start();

Read more about differences between stop, pause and start at the Dispatcher doc page.


Options

Configuration options for this hook provided as a second parameter.

const { ... } = useQueue(command, options)

Returns

Returned values from this hook.

const values = useQueue(command);

Name Type Description
requests QueueRequest[] List of requests for provided command
stopped boolean Queue status for provided command
pause () => void Callback which allow to pause queue. It will allow ongoing requests to be finished, but will stop next from being send.
start () => void Callback which allow to start queue.
stop () => void Callback which allow to stop queue, it will cancel ongoing requests.

Request interface

Name Type Description
downloading FetchProgressType Downloading progress for given request
uploading FetchProgressType Uploading progress for given request
deleteRequest () => void Removes request from the queue
startRequest () => void Callback which allow to start previously stopped request.
stopRequest () => void Callback which allow to stop request and cancel it if it's ongoing.