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:
type AsyncCollectionLoaderT<
DataT,
IdT extends number | string = number | string,
> = (id: IdT, oldData: DataT | null, meta: {
abortSignal: AbortSignal;
oldDataTimestamp: number;
}) => DataT | Promise<DataT | null> | null;
- Prior to the library
v0.22.0
the
metaobject passed into the loader includedisAborted(), andsetAbortCallback()methods. They have been removed in favor of the newabortSignalfield.
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:-
abortSignal— AbortSignal — Triggered if the loading operation has been aborted, and the return value of the current loader invokation will be ignored. For the optimal performance, the loader should react on this signal by terminating any asynchronous work early, and resolving null (or any other value). -
oldDataTimestamp— number — Unix timestamp (milliseconds) of the givenoldData, if any, or 0.
-
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.