UNPKG

fluidstate

Version:

Library for fine-grained reactivity state management

49 lines (48 loc) 1.68 kB
import { NameOptions } from "../extras/reactive-names"; /** * Represents the possible states of a reactive promise. */ export declare enum PromiseStatus { /** * The promise is currently in flight. */ Loading = 0, /** * The promise has resolved successfully. */ Success = 1, /** * The promise has been rejected with an error. */ Error = 2 } /** * A discriminated union representing the outcome of a reactive promise. * It can be in one of three states: Loading, Success, or Error. */ export type PromiseResult<T> = { status: PromiseStatus.Success; result: T; } | { status: PromiseStatus.Error; error: unknown; } | { status: PromiseStatus.Loading; }; export declare const providePromiseAtomName: (promise: Promise<unknown>, nameOptions?: NameOptions) => void; /** * Retrieves the current result of a given promise in a reactive way. * * This function wraps a native promise and makes its resolution (or rejection) * a signal in the reactive system. When the promise settles, any reactive * computations or reactions that depend on this result will be re-evaluated. * * If this is the first time `getResult` is called for a particular promise, * it initializes the reactive tracking for that promise and subscribes the * current reactions to it. * * @param promise The native promise to get the reactive result for. * @returns A `PromiseResult` object representing the current state and value (or error) of the promise. * This result is reactive and will cause an update when the promise settles. */ export declare const getResult: <T>(promise: Promise<T>) => PromiseResult<T>;