@dr.pogodin/react-global-state
Version:
Hook-based global state for React
69 lines (68 loc) • 3.51 kB
TypeScript
/**
* Loads and uses async data into the GlobalState path.
*/
import { type ForceT, type LockT, type TypeLock, type ValueAtPathT } from './utils';
import type GlobalState from './GlobalState';
import type SsrContext from './SsrContext';
export declare const DEFAULT_MAXAGE: number;
export type AsyncDataLoaderT<DataT> = (oldData: null | DataT, meta: {
isAborted: () => boolean;
oldDataTimestamp: number;
setAbortCallback: (cb: () => void) => void;
}) => (DataT | null) | Promise<DataT | null>;
export type AsyncDataReloaderT<DataT> = (loader?: AsyncDataLoaderT<DataT>) => void | Promise<void>;
export type AsyncDataEnvelopeT<DataT> = {
data: null | DataT;
numRefs: number;
operationId: string;
timestamp: number;
};
export type OperationIdT = `${'C' | 'S'}${string}`;
export declare function newAsyncDataEnvelope<DataT>(initialData?: DataT | null, { numRefs, timestamp }?: {
numRefs?: number | undefined;
timestamp?: number | undefined;
}): AsyncDataEnvelopeT<DataT>;
export type UseAsyncDataOptionsT = {
deps?: unknown[];
disabled?: boolean;
garbageCollectAge?: number;
maxage?: number;
noSSR?: boolean;
refreshAge?: number;
};
export type UseAsyncDataResT<DataT> = {
data: DataT | null;
loading: boolean;
reload: AsyncDataReloaderT<DataT>;
timestamp: number;
};
/**
* Executes the data loading operation.
* @param path Data segment path inside the global state.
* @param loader Data loader.
* @param globalState The global state instance.
* @param oldData Optional. Previously fetched data, currently stored in
* the state, if already fetched by the caller; otherwise, they will be fetched
* by the load() function itself.
* @param opIdPrefix operationId prefix to use, which should be
* 'C' at the client-side (default), or 'S' at the server-side (within SSR
* context).
* @return Resolves once the operation is done.
* @ignore
*/
export declare function load<DataT>(path: null | string | undefined, loader: AsyncDataLoaderT<DataT>, globalState: GlobalState<unknown, SsrContext<unknown>>, old?: {
data: DataT | null;
timestamp: number;
}, operationId?: OperationIdT): void | Promise<void>;
/**
* Resolves asynchronous data, and stores them at given `path` of global
* state.
*/
export type DataInEnvelopeAtPathT<StateT, PathT extends null | string | undefined> = Exclude<Extract<ValueAtPathT<StateT, PathT, void>, AsyncDataEnvelopeT<unknown>>['data'], null>;
declare function useAsyncData<StateT, PathT extends null | string | undefined, DataT extends DataInEnvelopeAtPathT<StateT, PathT> = DataInEnvelopeAtPathT<StateT, PathT>>(path: PathT, loader: AsyncDataLoaderT<DataT>, options?: UseAsyncDataOptionsT): UseAsyncDataResT<DataT>;
declare function useAsyncData<Forced extends ForceT | LockT = LockT, DataT = void>(path: null | string | undefined, loader: AsyncDataLoaderT<TypeLock<Forced, void, DataT>>, options?: UseAsyncDataOptionsT): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;
export { useAsyncData };
export interface UseAsyncDataI<StateT> {
<PathT extends null | string | undefined>(path: PathT, loader: AsyncDataLoaderT<DataInEnvelopeAtPathT<StateT, PathT>>, options?: UseAsyncDataOptionsT): UseAsyncDataResT<DataInEnvelopeAtPathT<StateT, PathT>>;
<Forced extends ForceT | LockT = LockT, DataT = unknown>(path: null | string | undefined, loader: AsyncDataLoaderT<TypeLock<Forced, void, DataT>>, options?: UseAsyncDataOptionsT): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;
}