AsyncDataLoaderT
import { type AsyncDataLoaderT } from '@dr.pogodin/react-global-state';
The AsyncDataLoaderT type is the signature of a valid data loader function for the useAsyncData() hook.
It is defined as a generic type:
type AsyncDataLoaderT<DataT>
= (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
Arguments
-
oldData— DataT | null — The previously loaded item, 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 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.