UNPKG

@dr.pogodin/react-global-state

Version:
69 lines (68 loc) 4.11 kB
/** * Loads and uses async data into the GlobalState path. */ import type GlobalState from './GlobalState'; import type SsrContext from './SsrContext'; import { type ForceT, type LockT, type TypeLock, type ValueAtPathT } from './utils'; export declare const DEFAULT_MAXAGE: number; export type AsyncDataLoaderT<DataT> = (oldData: DataT | null, meta: { abortSignal: AbortSignal; oldDataTimestamp: number; }) => DataT | Promise<DataT | null> | null; export type AsyncDataReloaderT<DataT> = (loader?: AsyncDataLoaderT<DataT>) => Promise<void> | void; export type AsyncDataEnvelopeT<DataT> = { data: DataT | null; 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>; set: (data: DataT | null) => void; timestamp: number; }; export declare function loadAsyncData<StateT, PathT extends null | string | undefined, DataT extends DataInEnvelopeAtPathT<StateT, PathT> = DataInEnvelopeAtPathT<StateT, PathT>>(path: PathT, loader: AsyncDataLoaderT<DataT>, globalState: GlobalState<StateT, SsrContext<StateT>>, old?: { data: DataT | null; timestamp: number; }, operationId?: OperationIdT): Promise<void> | void; export declare function loadAsyncData<Forced extends ForceT | LockT = LockT, DataT = unknown>(path: null | string | undefined, loader: AsyncDataLoaderT<TypeLock<Forced, void, DataT>>, globalState: GlobalState<unknown, SsrContext<unknown>>, old?: { data: DataT | null; timestamp: number; }, operationId?: OperationIdT): Promise<void> | 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 LoadAsyncDataI<StateT> { <PathT extends null | string | undefined, DataT extends DataInEnvelopeAtPathT<StateT, PathT> = DataInEnvelopeAtPathT<StateT, PathT>>(path: PathT, loader: AsyncDataLoaderT<DataT>, globalState: GlobalState<StateT, SsrContext<StateT>>, old?: { data: DataT | null; timestamp: number; }, operationId?: OperationIdT): Promise<void> | void; <Forced extends ForceT | LockT = LockT, DataT = unknown>(path: null | string | undefined, loader: AsyncDataLoaderT<TypeLock<Forced, void, DataT>>, globalState: GlobalState<unknown, SsrContext<unknown>>, old?: { data: DataT | null; timestamp: number; }, operationId?: OperationIdT): Promise<void> | void; } 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>>; }