import { Signal } from "@preact-signals/unified-signals"; import { FlatStore } from "../flat-store"; import { FlatStoreSetter } from "../flat-store/setter"; import { Dispose } from "../hooks/utility"; import { Accessor, AnyReactive, GetTruthyValue, GetValue, Setter } from "../utils"; declare const NO_INIT: unique symbol; /** * A resource that waits for a source to be truthy before fetching. */ export interface Unresolved { state: "unresolved"; loading: false; error: undefined; latest: T | undefined; (): undefined; } export interface Pending { state: "pending"; loading: true; error: undefined; latest: T | undefined; (): undefined; } export interface Ready { state: "ready"; loading: false; error: undefined; latest: T; (): T; } export interface Refreshing { state: "refreshing"; loading: true; error: undefined; latest: T | undefined; (): undefined; } export interface Errored { state: "errored"; loading: false; error: unknown; latest: T | undefined; (): undefined; } export type ResourceState = Unresolved | Pending | Ready | Refreshing | Errored; export type InitializedResource = Ready | Refreshing | Errored; export type ResourceActions = { mutate: Setter; refetch: (info?: TRefetch) => TResult | Promise | undefined | null; }; export type ResourceSource = () => S | false | null | undefined; export type ResourceFetcher = (k: TSourceData, info: ResourceFetcherInfo) => TResult | Promise; export type ResourceFetcherInfo = { value: TSourceData | undefined; refetching: TRefreshing | boolean; /** will be aborted if source is updated or resource disposed */ signal: AbortSignal; }; export type ResourceOptions, TRefreshing = boolean, TSourceData extends GetTruthyValue = GetTruthyValue> = { /** * Optional. An initial value for the resource. If is provided resource will be in ready state. */ initialValue?: TResult; /** * Optional. A function or signal that can be used as a source for fetching the resource. * This can be useful if you need to base your fetch operation on the value of another signal or even resource */ source?: TSource; /** * A function that is used to fetch or refresh the resource. */ fetcher: ResourceFetcher; } & ({ /** * lazy: Optional. If true, the resource will not be fetched until access of ResourceState properties. */ lazy?: boolean; } | { /** * Optional. If true, the resource will not subscribe to the source signal, before be activated. */ manualActivation?: boolean; }); type ResourceStore = { state: ResourceState["state"]; value: TResult | undefined | typeof NO_INIT; error: unknown; latest: TResult | undefined; readonly source: GetValue; readonly callResult: TResult | undefined; }; export type Resource, TRefreshing = boolean, TSourceData extends GetTruthyValue = GetTruthyValue> = ResourceState & { /** * A function that should be used to activate the resource with manualActivation option enabled. */ activate(): Dispose; dispose(): void; mutate: ResourceActions["mutate"]; refetch: ResourceActions["refetch"]; /** @internal */ pr: Promise | null; /** @internal */ _state: FlatStore>; /** @internal */ setter: FlatStoreSetter>; /** @internal */ abortController: AbortController | null; /** @internal */ _onRead(): void; /** @internal */ manualActivation: boolean; /** @internal */ refreshDummy$: Signal; /** @internal */ refetchData: boolean | TRefreshing; /** @internal */ refetchDetector(): { source: GetValue; refetching: TRefreshing | boolean; }; /** @internal */ refetchEffect: null | (() => void); /** @internal */ fetcher: ResourceFetcher; /** @internal */ get initialized(): boolean; /** @internal */ isInitialValueProvided: boolean; /** @internal */ _read(): ReturnType>; /** @internal */ _init(): void; /** @internal */ _fetch(data: { source: GetTruthyValue; refetching: TRefreshing | boolean; }): void; /** @internal */ _latest(): TResult | undefined; /** @internal */ _refetch: ResourceActions["refetch"]; /** @internal */ _mutate: ResourceActions["mutate"]; }; /** * More preact signals like resource api */ export declare const resource: , TRefreshing = boolean, TSourceData extends GetTruthyValue = GetTruthyValue>(options: ResourceOptions) => Resource; export {};