Skip to main content
Version: v7.0.0

Dispatcher

Dispatcher controls and manages the requests that are going to be executed with adapter. It manages them based on the options provided with request. This class can also run them with different modes like deduplication, cancelation, queueing or run-all-at-once mode. With it's help we can pause, stop, start and cancel requests.


Import
import { Dispatcher } from "@hyper-fetch/core"

Parameters

class Dispatcher<Adapter> {
constructor(options: DispatcherOptionsType) {};
storage: DispatcherStorageType;
options: DispatcherOptionsType;
events: { emitDrained: <Request>(values: QueueDataType<Request>, isTriggeredExternally?: boolean) => void; emitQueueChanged: <Request>(values: QueueDataType<Request>, isTriggeredExternally?: boolean) => void; emitQueueStatusChanged: <Request>(values: QueueDataType<Request>, isTriggeredExternally?: boolean) => void; onDrained: <Request>(callback: (values: QueueDataType<Request>) => void) => VoidFunction; onDrainedByKey: <Request>(queryKey: string, callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueChange: <Request>(callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueChangeByKey: <Request>(queryKey: string, callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueStatusChange: <Request>(callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueStatusChangeByKey: <Request>(queryKey: string, callback: (values: QueueDataType<Request>) => void) => VoidFunction };
emitter: EventEmitter;
add(request: RequestInstance) => string;
addQueueItem<Request>(queryKey: string, element: QueueItemType<Request>) => void;
addRunningRequest(queryKey: string, requestId: string, request: RequestInstance) => RunningRequestValueType;
cancelRunningRequest(queryKey: string, requestId: string) => void;
cancelRunningRequests(queryKey: string) => void;
clear() => void;
clearQueue(queryKey: string) => { queryKey: string; requests: never[]; stopped: boolean };
createStorageItem<Request>(request: Request) => QueueItemType<Request>;
delete(queryKey: string, requestId: string, abortKey: string) => undefined | QueueDataType<RequestInstance>;
deleteRunningRequest(queryKey: string, requestId: string) => void;
deleteRunningRequests(queryKey: string) => void;
flush() => Promise<void>;
flushQueue(queryKey: string) => Promise<void>;
getAllRunningRequests() => RunningRequestValueType[];
getIsActiveQueue(queryKey: string) => boolean;
getQueue<Request>(queryKey: string) => QueueDataType<Request>;
getQueueRequestCount(queryKey: string) => number;
getQueuesKeys() => string[];
getRequest<Request>(queryKey: string, requestId: string) => undefined | QueueItemType<Request>;
getRunningRequest(queryKey: string, requestId: string) => undefined | RunningRequestValueType;
getRunningRequests(queryKey: string) => RunningRequestValueType[];
hasRunningRequest(queryKey: string, requestId: string) => boolean;
hasRunningRequests(queryKey: string) => boolean;
incrementQueueRequestCount(queryKey: string) => void;
initialize(client: ClientInstance<{ adapter: Adapter }>) => Dispatcher<Adapter>;
pause(queryKey: string) => void;
performRequest(storageItem: QueueItemType) => Promise<void | QueueDataType<RequestInstance>>;
setQueue<Request>(queryKey: string, queue: QueueDataType<Request>) => QueueDataType<Request>;
start(queryKey: string) => void;
startRequest(queryKey: string, requestId: string) => void;
stop(queryKey: string) => void;
stopRequest(queryKey: string, requestId: string) => void;
}
Parameters
NameTypeDescription
options
DispatcherOptionsType

Properties

emitter

Type
EventEmitter

events

Type
{ emitDrained: <Request>(values: QueueDataType<Request>, isTriggeredExternally?: boolean) => void; emitQueueChanged: <Request>(values: QueueDataType<Request>, isTriggeredExternally?: boolean) => void; emitQueueStatusChanged: <Request>(values: QueueDataType<Request>, isTriggeredExternally?: boolean) => void; onDrained: <Request>(callback: (values: QueueDataType<Request>) => void) => VoidFunction; onDrainedByKey: <Request>(queryKey: string, callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueChange: <Request>(callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueChangeByKey: <Request>(queryKey: string, callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueStatusChange: <Request>(callback: (values: QueueDataType<Request>) => void) => VoidFunction; onQueueStatusChangeByKey: <Request>(queryKey: string, callback: (values: QueueDataType<Request>) => void) => VoidFunction }

options

Type
DispatcherOptionsType

storage

Type
DispatcherStorageType

Methods

add()

Add request to the dispatcher handler

Preview
add(request: RequestInstance)
Parameters
Parameters
NameTypeDescription
request
RequestInstance
Returns
string

addQueueItem()

Add new element to storage

Preview
addQueueItem<Request>(queryKey: string, element: QueueItemType<Request>)
Parameters
Parameters
NameTypeDescription
queryKey
string
element
QueueItemType<Request>
Returns
void

addRunningRequest()

Add request to the running requests list

Preview
addRunningRequest(queryKey: string, requestId: string, request: RequestInstance)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
request
RequestInstance
Returns
addRunningRequest
NameTypeDescription
request
RequestInstance
requestId
string
timestamp
number

cancelRunningRequest()

Cancel started request, but do NOT remove it from main storage

Preview
cancelRunningRequest(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
void

cancelRunningRequests()

Cancel all started requests, but do NOT remove it from main storage

Preview
cancelRunningRequests(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
void

clear()

Clear all running requests and storage

Preview
clear()
Returns
void

clearQueue()

Clear requests from queue cache

Preview
clearQueue(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
clearQueue
NameTypeDescription
queryKey
string
requests
never[]
stopped
boolean

createStorageItem()

Create storage element from request

Preview
createStorageItem<Request>(request: Request)
Parameters
Parameters
NameTypeDescription
request
Request
Returns
createStorageItem
NameTypeDescription
request
Request
requestId
string
resolved
boolean

Resolved when we receive response

retries
number
stopped
boolean
timestamp
number

delete()

Delete from the storage and cancel request

Preview
delete(queryKey: string, requestId: string, abortKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
abortKey
string
Returns
undefined | QueueDataType<RequestInstance>

deleteRunningRequest()

Delete request by id, but do NOT clear it from queue and do NOT cancel them

Preview
deleteRunningRequest(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
void

deleteRunningRequests()

Delete all started requests, but do NOT clear it from queue and do NOT cancel them

Preview
deleteRunningRequests(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
void

flush()

Flush all available requests from all queues

Preview
flush()
Returns
Promise<void>

flushQueue()

Method used to flush the queue requests

Preview
flushQueue(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
Promise<void>

getAllRunningRequests()

Get currently running requests from all queryKeys

Preview
getAllRunningRequests()
Returns
RunningRequestValueType[]

getIsActiveQueue()

Get value of the active queue status based on the stopped status

Preview
getIsActiveQueue(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
boolean

getQueue()

Return queue state object

Preview
getQueue<Request>(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
getQueue
NameTypeDescription
queryKey
string
requests
QueueItemType<Request>[]
stopped
boolean

getQueueRequestCount()

Get count of requests from the same queryKey

Preview
getQueueRequestCount(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
number

getQueuesKeys()

Return all

Preview
getQueuesKeys()
Returns
string[]

getRequest()

Return request from queue state

Preview
getRequest<Request>(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
undefined | QueueItemType<Request>

getRunningRequest()

Get running request by id

Preview
getRunningRequest(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
undefined | RunningRequestValueType

getRunningRequests()

Get currently running requests

Preview
getRunningRequests(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
RunningRequestValueType[]

hasRunningRequest()

Check if request is currently processing

Preview
hasRunningRequest(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
boolean

hasRunningRequests()

Get the value based on the currently running requests

Preview
hasRunningRequests(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
boolean

incrementQueueRequestCount()

Add request count to the queryKey

Preview
incrementQueueRequestCount(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
void

initialize()

Preview
initialize(client: ClientInstance<{ adapter: Adapter }>)
Parameters
Parameters
NameTypeDescription
client
ClientInstance<{ adapter: Adapter }>
Returns
Dispatcher<Adapter>

pause()

Pause request queue, but do not cancel already started requests

Preview
pause(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
void

performRequest()

Request can run for some time, once it's done, we have to check if it's successful or if it was aborted It can be different once the previous call was set as cancelled and removed from queue before this request got resolved

Preview
performRequest(storageItem: QueueItemType)
Parameters
Parameters
NameTypeDescription
storageItem
QueueItemType
Returns
Promise<void | QueueDataType<RequestInstance>>

setQueue()

Set new queue storage value

Preview
setQueue<Request>(queryKey: string, queue: QueueDataType<Request>)
Parameters
Parameters
NameTypeDescription
queryKey
string
queue
QueueDataType<Request>
Returns
setQueue
NameTypeDescription
queryKey
string
requests
QueueItemType<Request>[]
stopped
boolean

start()

Start request handling by queryKey

Preview
start(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
void

startRequest()

Start particular request

Preview
startRequest(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
void

stop()

Stop request queue and cancel all started requests - those will be treated like not started

Preview
stop(queryKey: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
Returns
void

stopRequest()

Stop particular request

Preview
stopRequest(queryKey: string, requestId: string)
Parameters
Parameters
NameTypeDescription
queryKey
string
requestId
string
Returns
void