Managers
The library contains several managers, they are mainly used to support smaller subsystems and features.
AppManager
This class is responsible for the application’s online
or offline
state and whether the application window is
focused
or blurred
.
For example, suppose you want to use Hyper Fetch in a React Native environment and the default web browser events won't work. In that case, you need to set the events that are most optimal for the current environment.
Hyper Fetch event listeners return unmounting callbacks for easier cleanup handling.
const unmountFocusListener = client.requestManager.events.onFocus(console.log);
const unmountOfflineListener = client.requestManager.events.onOffline(console.log);
...
unmountFocusListener()
unmountOfflineListener()
Events
{
emitBlur: () => void;
emitFocus: () => void;
emitOffline: () => void;
emitOnline: () => void;
onBlur: (callback: () => void) => VoidFunction;
onFocus: (callback: () => void) => VoidFunction;
onOffline: (callback: () => void) => VoidFunction;
onOnline: (callback: () => void) => VoidFunction;
}
RequestManager
The RequestManager
is responsible for holding the cancellation tokens needed to terminate the requests and for the
events related to the requests lifecycle - request start
, upload
, download
, response
and abort
.
Hyper Fetch event listeners return unmounting callbacks for easier cleanup handling.
const unmountUploadListener = client.requestManager.events.onUploadProgressById(requestId, console.log);
const unmountDownloadListener = client.requestManager.events.onDownloadProgressById(requestId, console.log);
...
unmountUploadListener()
unmountDownloadListener()
AbortKey
Every request added to the dispatcher creates an abort controller for itself. This controller is stored in the Map
object under abort key with the requestId
. This lets you abort whole groups of requests using abortKey
or single
requests using their requestId
. By default, abortKey
is automatically created on request out of its endpoint and
method, but you can set custom values.
Events
{
emitAbort: (abortKey: string, requestId: string, request: RequestInstance) => void;
emitDownloadProgress: (queueKey: string, requestId: string, values: ProgressType, details: RequestEventType<RequestInstance>) => void;
emitLoading: (queueKey: string, requestId: string, values: RequestLoadingEventType) => void;
emitRemove: (queueKey: string, requestId: string, details: RequestEventType<T>) => void;
emitRequestStart: (queueKey: string, requestId: string, details: RequestEventType<RequestInstance>) => void;
emitResponse: (cacheKey: string, requestId: string, response: ResponseReturnType<unknown, unknown, AdapterType>, details: ResponseDetailsType) => void;
emitResponseStart: (queueKey: string, requestId: string, details: RequestEventType<RequestInstance>) => void;
emitUploadProgress: (queueKey: string, requestId: string, values: ProgressType, details: RequestEventType<RequestInstance>) => void;
onAbort: (abortKey: string, callback: (request: RequestInstance) => void) => VoidFunction;
onAbortById: (requestId: string, callback: (request: RequestInstance) => void) => VoidFunction;
onDownloadProgress: (queueKey: string, callback: (values: ProgressType, details: RequestEventType<T>) => void) => VoidFunction;
onDownloadProgressById: (requestId: string, callback: (values: ProgressType, details: RequestEventType<T>) => void) => VoidFunction;
onLoading: (queueKey: string, callback: (values: RequestLoadingEventType) => void) => VoidFunction;
onLoadingById: (requestId: string, callback: (values: RequestLoadingEventType) => void) => VoidFunction;
onRemove: (queueKey: string, callback: (details: RequestEventType<T>) => void) => VoidFunction;
onRemoveById: (requestId: string, callback: (details: RequestEventType<T>) => void) => VoidFunction;
onRequestStart: (queueKey: string, callback: (details: RequestEventType<T>) => void) => VoidFunction;
onRequestStartById: (requestId: string, callback: (details: RequestEventType<T>) => void) => VoidFunction;
onResponse: (cacheKey: string, callback: (response: ResponseReturnType<Response, ErrorType, AdapterType>, details: ResponseDetailsType) => void) => VoidFunction;
onResponseById: (requestId: string, callback: (response: ResponseReturnType<Response, ErrorType, AdapterType>, details: ResponseDetailsType) => void) => VoidFunction;
onResponseStart: (queueKey: string, callback: (details: RequestEventType<T>) => void) => VoidFunction;
onResponseStartById: (requestId: string, callback: (details: RequestEventType<T>) => void) => VoidFunction;
onUploadProgress: (queueKey: string, callback: (values: ProgressType, details: RequestEventType<T>) => void) => VoidFunction;
onUploadProgressById: (requestId: string, callback: (values: ProgressType, details: RequestEventType<T>) => void) => VoidFunction;
}
client.requestManager.events.onResponse((response, details) => {
// ...
});
LoggerManager
LoggerManager is the main instance of the Hyper Fetch logging system. You can initiate single loggers based on it in subsystems; this allows you to isolate these loggers but still give them access to the parent configuration in the Client.
const logger = client.loggerManager.init("My Module")
logger.error(...) // output in console => [My Module] Some message