/** * `@nifrajs/web-react/fn` - React binding for a server function's pending/error state. * * Calling a server function needs no hook: the client stub is `(input) => Promise`. This adds * the part a component usually wants around it - in flight, last result, last error - and nothing more. * * The state machine is `@nifrajs/web`'s `createServerFnStore`, shared with every other adapter, so * "is it pending" has one answer rather than five that drift. This file contributes only the `useSyncExternalStore` subscription. */ import type { ClientServerFn, ServerFnReference } from "@nifrajs/web/fn" import { createServerFnStore, type ServerFnState } from "@nifrajs/web/fn-state" import { useMemo, useRef, useSyncExternalStore } from "react" /** A server function's state plus the call itself. */ export interface ServerFnHandle extends ServerFnState { /** Invoke it. Records state, and rejects on failure - attach `.catch` if you only render state. */ readonly call: (input: Input) => Promise /** Back to idle. */ readonly reset: () => void } /** * Track one server function's call state. * * const addTodo = useServerFn(fns.addTodo) *