Skip to main content
Version: Current

ActBuilder

@rotorsoft/act-root


@rotorsoft/act-root / act/src / ActBuilder

Interface: ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor, TLanes>

Defined in: libs/act/src/builders/act-builder.ts:110

Fluent builder interface for composing event-sourced applications.

Provides a chainable API for:

  • Registering states via .withState()
  • Registering slices via .withSlice()
  • Registering projections via .withProjection()
  • Locking a custom actor type via .withActor<TActor>()
  • Declaring drain lanes via .withLane({name, ...}) (ACT-1103)
  • Defining event reactions via .on() โ†’ .do() โ†’ .to()
  • Building the orchestrator via .build()

Seeโ€‹

  • act for usage examples
  • Act for the built orchestrator API

Extendsโ€‹

  • BuilderBase<ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor, TLanes>, TEvents, TActions, TActor, TLanes>

Type Parametersโ€‹

TSchemaRegโ€‹

TSchemaReg extends SchemaRegister<TActions>

Schema register for states (maps action names to state schemas)

TEventsโ€‹

TEvents extends Schemas

Event schemas (maps event names to event data schemas)

TActionsโ€‹

TActions extends Schemas

Action schemas (maps action names to action payload schemas)

TStateMapโ€‹

TStateMap extends Record<string, Schema> = { }

Map of state names to state schemas

TActorโ€‹

TActor extends Actor = Actor

Actor type extending base Actor

TLanesโ€‹

TLanes extends string = typeof DEFAULT_LANE

Union of declared lane names (ACT-1103). Narrowed by .withLane({name}) calls so .to({lane}) and ActOptions.onlyLanes reject typos at compile time. Starts at "default".

Propertiesโ€‹

buildโ€‹

build: (options?) => Act<TSchemaReg, TEvents, TActions, TStateMap, TActor>

Defined in: libs/act/src/builders/act-builder.ts:250

Builds and returns the Act orchestrator instance.

Parametersโ€‹

options?โ€‹

ActOptions<TLanes>

Optional runtime overrides (see ActOptions). options.onlyLanes is narrowed to the declared TLanes union, so onlyLanes: ["typo"] is a compile error when the lane wasn't declared via .withLane(...).

Returnsโ€‹

Act<TSchemaReg, TEvents, TActions, TStateMap, TActor>

The Act orchestrator instance

Seeโ€‹

Act for available orchestrator methods


eventsโ€‹

readonly events: EventRegister<TEvents>

Defined in: libs/act/src/builders/builder-base.ts:117

The registered event schemas and their reaction maps.

Inherited fromโ€‹

BuilderBase.events


onโ€‹

on: <TKey>(event) => ReactionOn<ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor, TLanes>, TEvents, TActions, TActor, TLanes, TKey>

Defined in: libs/act/src/builders/builder-base.ts:113

Begins defining a reaction. Chain .do(...) to run immediately, or .defer(when).do(...) to hold the reaction until a schedule is due (#1091). .to(...) follows .do(...) in both cases and, for a deferred reaction, routes it onto its own target so the hold doesn't stall the stream's other reactions.

Type Parametersโ€‹

TKeyโ€‹

TKey extends string | number | symbol

Parametersโ€‹

eventโ€‹

TKey

Returnsโ€‹

ReactionOn<ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor, TLanes>, TEvents, TActions, TActor, TLanes, TKey>

Inherited fromโ€‹

BuilderBase.on


withActorโ€‹

withActor: <TNewActor>() => ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TNewActor, TLanes>

Defined in: libs/act/src/builders/act-builder.ts:206

Locks a custom actor type for this application.

This is a pure type-level method โ€” it returns the same builder at runtime but narrows the TActor generic so that app.do() and reaction dispatchers require the richer actor shape.

Type Parametersโ€‹

TNewActorโ€‹

TNewActor extends Readonly<{[key: string]: unknown; id: string; name: string; }>

Custom actor type extending base Actor

Returnsโ€‹

ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TNewActor, TLanes>

The same builder with TActor locked to TNewActor

Exampleโ€‹

type MyActor = { id: string; name: string; role: string; tenantId: string };

const app = act()
.withActor<MyActor>()
.withState(Counter)
.build();

// Now app.do() requires MyActor in the target
await app.do("increment", {
stream: "counter-1",
actor: { id: "1", name: "Alice", role: "admin", tenantId: "t1" }
}, { by: 5 });

withLaneโ€‹

withLane: <TConfig>(config) => ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor, TLanes | TConfig["name"]>

Defined in: libs/act/src/builders/act-builder.ts:229

Declares a drain lane (ACT-1103). Lane name narrows TLanes so .to({lane}) and ActOptions.onlyLanes type-check against it.

Type Parametersโ€‹

TConfigโ€‹

TConfig extends LaneConfig

Parametersโ€‹

configโ€‹

TConfig

Returnsโ€‹

ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor, TLanes | TConfig["name"]>

Exampleโ€‹

const app = act()
.withState(Counter)
.withLane({ name: "slow", leaseMillis: 60_000, streamLimit: 5 })
.on("OrderConfirmed")
.do(deliverWebhook)
.to({ target: "webhooks-out", lane: "slow" })
.build();

withProjectionโ€‹

withProjection: <TNewEvents>(projection) => ActBuilder

Defined in: libs/act/src/builders/builder-base.ts:101

Registers a standalone projection. Its events must be a subset of events already registered via .withState() (or .withSlice() on the Act builder); handlers keep their (event, stream) signature.

Type Parametersโ€‹

TNewEventsโ€‹

TNewEvents extends Schemas

Parametersโ€‹

projectionโ€‹

[Exclude<keyof TNewEvents, keyof TEvents>] extends [never] ? Projection<TNewEvents> : never

Returnsโ€‹

ActBuilder

Inherited fromโ€‹

BuilderBase.withProjection


withSliceโ€‹

withSlice: <TNewSchemaReg, TNewEvents, TNewActions, TNewMap, TNewLanes>(slice) => ActBuilder<TSchemaReg & TNewSchemaReg, TEvents & TNewEvents, TActions & TNewActions, TStateMap & TNewMap, TActor, TLanes | TNewLanes>

Defined in: libs/act/src/builders/act-builder.ts:157

Registers a slice with the builder.

Merges all the slice's states and reactions into the application. State names, action names, and event names must be unique across the application (partial states with the same name are merged automatically).

Type Parametersโ€‹

TNewSchemaRegโ€‹

TNewSchemaReg extends SchemaRegister<TNewActions>

TNewEventsโ€‹

TNewEvents extends Schemas

TNewActionsโ€‹

TNewActions extends Schemas

TNewMapโ€‹

TNewMap extends Record<string, Schema>

TNewLanesโ€‹

TNewLanes extends string

Parametersโ€‹

sliceโ€‹

Slice<TNewSchemaReg, TNewEvents, TNewActions, TNewMap, Readonly<{[key: string]: unknown; id: string; name: string; }>, TNewLanes>

Returnsโ€‹

ActBuilder<TSchemaReg & TNewSchemaReg, TEvents & TNewEvents, TActions & TNewActions, TStateMap & TNewMap, TActor, TLanes | TNewLanes>

Throwsโ€‹

If duplicate action or event names are detected


withStateโ€‹

withState: <TNewState, TNewEvents, TNewActions, TNewName>(state) => ActBuilder<TSchemaReg & { [K in string | number | symbol]: TNewState }, TEvents & TNewEvents, TActions & TNewActions, TStateMap & { [K in string]: TNewState }, TActor, TLanes>

Defined in: libs/act/src/builders/act-builder.ts:133

Registers a state definition with the builder.

State names, action names, and event names must be unique across the application (partial states with the same name are merged automatically).

Type Parametersโ€‹

TNewStateโ€‹

TNewState extends Schema

TNewEventsโ€‹

TNewEvents extends Schemas

TNewActionsโ€‹

TNewActions extends Schemas

TNewNameโ€‹

TNewName extends string = string

Parametersโ€‹

stateโ€‹

State<TNewState, TNewEvents, TNewActions, TNewName>

Returnsโ€‹

ActBuilder<TSchemaReg & { [K in string | number | symbol]: TNewState }, TEvents & TNewEvents, TActions & TNewActions, TStateMap & { [K in string]: TNewState }, TActor, TLanes>

Throwsโ€‹

If duplicate action or event names are detected