Skip to main content

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:

export type AsyncDataLoaderT<DataT>
= (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.

Arguments

  • oldDataDataT | null — The previously loaded item, 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 given isAborted 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.

      info

      Technically 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.

    • oldDataTimestampnumber — Unix timestamp (milliseconds) of the given oldData, 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 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.