/** * useAction — THE async write (docs/rfc-async.md rev 8). * * The manual counterpart to `useData`: never auto-runs, triggered by * `.run(input)`. `run` never rejects — it resolves a `RunResult` so both * fire-and-forget and `const r = await save.run()` are safe. In-flight * requests are NEVER aborted (an aborted POST is not an undone POST); a * newer `run()` or `reset()` merely supersedes the OBSERVATION — the older * run's promise resolves `{ ok: false, error: SupersededError }` and never * writes state. * * Cross-read invalidation is explicit: on success call `user.refresh()`. * Cache-aware invalidate/optimistic mutate arrive with a pack, attached via * the open `ActionOptions` interface. */ import { type Fetcher, type MatchArms, type ValuePresence } from './async/shared.js'; /** A superseded run resolves { ok: false, error: SupersededError } and never writes `.error`. */ export declare class SupersededError extends Error { readonly name = "SupersededError"; } export type RunResult = { ok: true; value: T; } | { ok: false; error: Error; }; /** OPEN interface — deliberately empty in core; packs augment it. */ export interface ActionOptions { } /** Methods shared by every {@link AsyncAction} member. */ export interface AsyncActionBase { match(arms: MatchArms): R | undefined; /** * Trigger. Never rejects; in-flight runs are never aborted. * `In = void` ⇒ callable as `run()` (TS permits omitting a void-typed * parameter). */ run(input: In): Promise>; /** * Back to 'idle'; clears value/error (dismiss a success message, reuse a * form). Discards observation of an in-flight run (its promise resolves * SupersededError); never aborts the request. */ reset(): void; } /** * A discriminated union like {@link AsyncState} (same narrowing: * `if (a.hasValue) a.value // T`), with action-specific semantics — no * 'refreshing'; `value` is the LAST SUCCESSFUL result and survives both a * re-run ('pending' — a search box renders from it) and a failure * ('errored' — SWR-through-error); `loading` is the blessed double-submit * guard: disabled={a.loading}. Only `reset()` clears the value. */ export type AsyncAction = AsyncActionBase & ({ readonly state: 'idle'; readonly value: null; readonly hasValue: false; readonly error: null; readonly loading: false; } | ({ readonly state: 'pending'; readonly error: null; readonly loading: true; } & ValuePresence) | { readonly state: 'ready'; readonly value: T; readonly hasValue: true; readonly error: null; readonly loading: false; } | ({ readonly state: 'errored'; readonly error: Error; readonly loading: false; } & ValuePresence)); export declare function useAction(fn: Fetcher, opts?: ActionOptions): AsyncAction; //# sourceMappingURL=use-action.d.ts.map