Skip to content

interpret

interpret<I, E, A, O>(op, options): InterpretResult<I, E, A, O>

Defined in: Core/Op.ts:774

Attaches a concurrency strategy to an Op, returning a Manager.

Strategy is data, not a method name. The S type parameter is narrowed to only the states reachable for the chosen strategy and options — subscribers cannot reference states that cannot occur.

Strategies:

  • once — fires once. Only the first run() executes; subsequent calls return DroppedNil immediately. State is permanent after completion.
  • restartable — new call cancels the previous (ReplacedNil). Only the latest result matters.
  • exclusive — new calls while in-flight return DroppedNil immediately.
  • queue — calls run in submission order. Queued state shows position.
  • buffered — 1 in-flight + 1 waiting slot. Newer calls evict the slot (EvictedNil).
  • debounced — waits ms ms of quiet before starting. Earlier calls get EvictedNil.

retry and timeout can be combined with any strategy. Both are applied internally per run() call — set the policy once, not at every call site. The timeout wraps the entire retry sequence (one deadline for all attempts). When retry is present, Retrying is added to the subscriber type.

I

E

A

O extends AllInterpretOptions<I, E>

Op<I, E, A>

O

InterpretResult<I, E, A, O>

// Load once on mount — further calls are no-ops
const getUser = Op.interpret(fetchUser, { strategy: "once" });
getUser.subscribe(state => {
  if (state.kind === "Pending") showSpinner();
  if (state.kind === "Ok")      render(state.value);
});
getUser.run(userId);

// Search: cancel the previous query when a new one starts
const search = Op.interpret(searchOp, { strategy: "restartable" });

// Form submit: ignore double-clicks while in-flight
const submit = Op.interpret(submitOp, {
  strategy: "exclusive",
  retry: { attempts: 3, backoff: n => n * 500 },
  timeout: { ms: 10_000, onTimeout: () => new ApiError("timed out") },
});

// Auto-save: current save commits fully; latest pending edit saves next
const save = Op.interpret(saveOp, { strategy: "buffered" });