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 { 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;
}
| Name | Type | Description |
|---|---|---|
| options | |
Properties
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 }
Methods
add()
Add request to the dispatcher handler
Preview
add(request: RequestInstance)
Parameters
| Name | Type | Description |
|---|---|---|
| request | |
Returns
string
addQueueItem()
Add new element to storage
Preview
addQueueItem<Request>(queryKey: string, element: QueueItemType<Request>)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| element | |
Returns
void
addRunningRequest()
Add request to the running requests list
Preview
addRunningRequest(queryKey: string, requestId: string, request: RequestInstance)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | | |
| request | |
Returns
| Name | Type | Description |
|---|---|---|
| request | | |
| requestId | | |
| timestamp | |
cancelRunningRequest()
Cancel started request, but do NOT remove it from main storage
Preview
cancelRunningRequest(queryKey: string, requestId: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
void
cancelRunningRequests()
Cancel all started requests, but do NOT remove it from main storage
Preview
cancelRunningRequests(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
void
clearQueue()
Clear requests from queue cache
Preview
clearQueue(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requests | | |
| stopped | |
createStorageItem()
Create storage element from request
Preview
createStorageItem<Request>(request: Request)
Parameters
| Name | Type | Description |
|---|---|---|
| request | |
Returns
| Name | Type | Description |
|---|---|---|
| request | | |
| requestId | | |
| resolved | | Resolved when we receive response |
| retries | | |
| stopped | | |
| timestamp | |
delete()
Delete from the storage and cancel request
Preview
delete(queryKey: string, requestId: string, abortKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | | |
| abortKey | |
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
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
void
deleteRunningRequests()
Delete all started requests, but do NOT clear it from queue and do NOT cancel them
Preview
deleteRunningRequests(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
void
flushQueue()
Method used to flush the queue requests
Preview
flushQueue(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
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
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
boolean
getQueue()
Return queue state object
Preview
getQueue<Request>(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requests | | |
| stopped | |
getQueueRequestCount()
Get count of requests from the same queryKey
Preview
getQueueRequestCount(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
number
getRequest()
Return request from queue state
Preview
getRequest<Request>(queryKey: string, requestId: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
undefined | QueueItemType<Request>
getRunningRequest()
Get running request by id
Preview
getRunningRequest(queryKey: string, requestId: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
undefined | RunningRequestValueType
getRunningRequests()
Get currently running requests
Preview
getRunningRequests(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
RunningRequestValueType[]
hasRunningRequest()
Check if request is currently processing
Preview
hasRunningRequest(queryKey: string, requestId: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
boolean
hasRunningRequests()
Get the value based on the currently running requests
Preview
hasRunningRequests(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
boolean
incrementQueueRequestCount()
Add request count to the queryKey
Preview
incrementQueueRequestCount(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
void
initialize()
Preview
initialize(client: ClientInstance<{ adapter: Adapter }>)
Parameters
| Name | Type | Description |
|---|---|---|
| client | |
Returns
Dispatcher<Adapter>
pause()
Pause request queue, but do not cancel already started requests
Preview
pause(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
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
| Name | Type | Description |
|---|---|---|
| storageItem | |
Returns
Promise<void | QueueDataType<RequestInstance>>
setQueue()
Set new queue storage value
Preview
setQueue<Request>(queryKey: string, queue: QueueDataType<Request>)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| queue | |
Returns
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requests | | |
| stopped | |
start()
Start request handling by queryKey
Preview
start(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
void
startRequest()
Start particular request
Preview
startRequest(queryKey: string, requestId: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
void
stop()
Stop request queue and cancel all started requests - those will be treated like not started
Preview
stop(queryKey: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | |
Returns
void
stopRequest()
Stop particular request
Preview
stopRequest(queryKey: string, requestId: string)
Parameters
| Name | Type | Description |
|---|---|---|
| queryKey | | |
| requestId | |
Returns
void