UNPKG

react-solid-flow

Version:

[SolidJS](https://www.solidjs.com/docs/latest/api#control-flow)-inspired basic control-flow components and everyday async state hook library for [React](https://reactjs.org/)

55 lines (54 loc) 2.38 kB
import type { Resource } from "../models/Resource"; export type ResourceReturn<T, TArgs extends readonly unknown[]> = [ Resource<T>, { /** Manually set the value. * * If fetcher was currently pending, it's aborted. */ mutate: (v: Awaited<T>) => void; /** * Call refetch with supplied args. * * Fetcher opts added automatically. If fetcher was currently pending, it's aborted. */ refetch: (...args: TArgs) => Promise<T> | T; /** Imperatively abort the current fetcher call. * * If abort is performed with no reason, or with AbortError instance, then * the state is still considered pending/refreshing, resource.error is * not updated, and onError callback is not called. * Any other reason will result in erorred resource state. * * Resource won't be refetched untill deps change again. */ abort: (reason?: any) => void; } ]; export type ResourceOptions<T> = { /** Initial value for the resource */ initialValue?: Awaited<T> | (() => Awaited<T>); /** resolve callback */ onCompleted?: (data: Awaited<T>) => void; /** rejection callback */ onError?: (error: unknown) => void; /** Skip first run (before params change) */ skipFirstRun?: boolean; /** Skip calls of fetcher (can still be called manually with refresh) * * Can be useful if you're waiting for some of deps to be in certain state * before calling the fetcher or if you want to trigger the fetcher only * manually on some event. */ skip?: boolean; /** Don't memoize getter, rerun it every time it changes */ skipFnMemoization?: boolean; }; export interface FetcherOpts { /** is true, if the call to fetcher was triggered manually with refetch function, * false otherwise */ refetching: boolean; /** can be used to abort operations in fetcher function, i.e. passed to fetch options */ signal: AbortSignal; } export declare function useResource<T, TArgs extends readonly any[]>(fetcher: ((...args: [...TArgs, FetcherOpts]) => Promise<T> | T) | ((...args: [...TArgs]) => Promise<T> | T), deps?: [...TArgs], { initialValue, onCompleted, onError, skipFirstRun, skip, skipFnMemoization, }?: ResourceOptions<T>): ResourceReturn<T, TArgs>;