import type { Getter } from "./Getter.ts"; /** Lifecycle status constants for resources. */ export declare const ResourceStatus: { /** No load is currently running. */ readonly Idle: "idle"; /** A load is currently running. */ readonly Loading: "loading"; /** The last load completed successfully. */ readonly Ready: "ready"; /** The last load failed. */ readonly Failed: "failed"; /** The resource has been disposed. */ readonly Disposed: "disposed"; }; /** Lifecycle status of a resource. */ export type ResourceStatus = typeof ResourceStatus[keyof typeof ResourceStatus]; /** Loads a resource value from the current source state. */ export type ResourceLoader = (source: S, abortSignal: AbortSignal) => T | Promise; /** Options for creating a resource. */ export interface CreateResourceOptions { /** * Compares the previous and next resource value. * * Returning true suppresses the value update and keeps dependent computations clean. Set this to false to force a value update for every * successful load. * * @param previous - The previous resource value. * @param next - The next resource value. * @returns True when both values should be treated as equal. */ equals?: false | ((previous: T | Init, next: T | Init) => boolean); /** The initial resource value visible before the first successful load. */ initialValue?: Init; /** * Returns true when the current source value should skip loading. * * When loading is skipped, the loader is not called, the current resource value stays unchanged, the resource enters `Idle` state, and * any previous resource error is cleared. * * @param source - The current source value. * @returns True when loading should be skipped for the current source value. */ skip?: (source: S) => boolean; } /** Controls and status accessors returned together with a resource value getter. */ export interface Resource extends Disposable { /** * Returns the last resource error, if any. * * @returns The last resource error or undefined. */ error(): Error | undefined; /** * Returns the current resource status. * * @returns The current resource status. */ status(): ResourceStatus; /** * Reloads the resource for the current source value. */ reload(): void; } /** * Creates a reactive async resource and returns a value getter together with status and control methods. * * The resource eagerly loads from the current source value and reloads whenever the source changes or `resource.reload()` is called. * Concurrent loads are cancelled through an abort signal and stale results are ignored. The returned resource object can be manually * disposed manually. * * @param source - Getter providing the current load source. * @param load - Loads the resource value for the current source. * @param options - Optional resource behavior overrides without an explicit initial value. * @returns A resource value getter and a resource controller object. */ export declare function createResource(source: Getter, load: ResourceLoader, options?: CreateResourceOptions & { initialValue?: never; }): [Getter, Resource]; /** * Creates a reactive async resource and returns a value getter together with status and control methods. * * The resource eagerly loads from the current source value and reloads whenever the source changes or `resource.reload()` is called. * Concurrent loads are cancelled through an abort signal and stale results are ignored. The returned resource object can be manually * disposed manually. * * @param source - Getter providing the current load source. * @param load - Loads the resource value for the current source. * @param options - Optional resource behavior overrides with an explicit initial value. * @returns A resource value getter and a resource controller object. */ export declare function createResource(source: Getter, load: ResourceLoader, options: CreateResourceOptions & { initialValue: Init; }): [Getter, Resource];