@hyperfrontend/state-machine/coordinated-async-operation

coordinated-async-operation

Orchestrator for multiple related async processes, exposing aggregate state across the group.

CoordinatedAsyncProcess wraps a set of AsyncOperation instances and presents them as one unit: lifecycle actions (start, pause, cancel) fan out to every member; selectors (inProgress, done, failed, ...) report the group-level state derived from the constituents. Use this when an application step depends on several async pieces completing together (fetching a record and its related entities, say) and you want a single object to render against rather than coordinating the individual processes by hand.

API Reference§

◇ Classes

§class

CoordinatedAsyncProcess

Coordinates multiple async operations, allowing them to be started, cancelled, or paused together.
Constructor
§new CoordinatedAsyncProcess()
Methods
§readonly cancelAll(): void
Cancels every registered operation, in registration order.
Each cancellation only changes that operation's state; the processes themselves keep running to completion.
§readonly pauseAll(): void
Pauses every registered operation, in registration order.
Each pause only changes that operation's state; the processes themselves are not suspended.
§readonly registerProcess(process: AsyncProcess): CoordinatedAsyncProcess
Adds a process to the group, wrapped in an AsyncOperation of its own.
Registration does not run the process; startAll does. Processes keep the order they were registered in.

Parameters

NameTypeDescription
§process
AsyncProcessThe asynchronous work to add

Returns

CoordinatedAsyncProcess
This coordinator, so registrations can be chained
§readonly startAll(): Promise<void[]>
Starts every registered process at once.
Each process runs through its own operation, which catches its rejection and records it as that operation's failure, so one process failing neither stops the others nor rejects the returned promise.

Returns

Promise<void[]>
Resolves once every process has settled, with one entry per operation and no values
Example

Coordinating multiple async operations

const coordinator = new CoordinatedAsyncProcess()
  .registerProcess(fetchUsers)
  .registerProcess(fetchOrders)
await coordinator.startAll()