AsyncCollectionLoaderT
import { type AsyncCollectionLoaderT } from '@dr.pogodin/react-global-state';
The AsyncCollectionLoaderT type is the signature of a valid data loader function for the useAsyncCollection() hook.
It is defined as a generic type:
export type AsyncCollectionLoaderT<
DataT,
IdT extends number | string = number | string,
> =
(id: IdT, oldData: null | DataT, meta: {
isAborted: () => boolean;
oldDataTimestamp: number;
}) => DataT | Promise<DataT | null> | null;
Generic Parameters
DataT
— The type of data loaded by the loader function.IdT
— number | string — The type of collection item's index.
Arguments
The data loader function receives two arguments:
-
id
— IdT — The identifier of collection item to load. -
oldData
— DataT | null — The item previously loaded for thisid
, if any; or null. -
meta
— Holds additional information about the loading opeartion:-
isAborted
— () => boolean — In various situations the library may abort an ongoing loading operation (or rather ignore the result of such aborted operation). The loader function may call this givenisAborted
method, to check whether it has been aborted or not, and if it has been aborted it may just exit with null result — as far as the library is concerned the result of that call will be silently discarted anyway.infoTechnically
isAborted()
(if memoized and used in the outer context of the loader) also returns true after the loading operation has completed without an abort; i.e. it actually checks whether the current loading operation, if any, is the same for which this callback has been created. -
oldDataTimestamp
— number — Unix timestamp (milliseconds) of the givenoldData
, if any, or 0. -
setAbortCallback
— (cb: () => void) => void — Allows to register an abort callback, which will be triggered when, and if the current loading operation is aborted. If called repeatedly, the new callback will replace the previous one for that operation.
-
Result
The data loader function, for the given ID, must return either DataT value, or null, or a Promise of DataT value or null. When a Promise is returned, the corresponding envelope in the global state is put into the (re-)loading state while the promise settlement is awaited; otherwise the value (DataT or null) is written into the envelope synchronously, without visiting the intermediate (re-)loading state.