Skip to content

@yantrix/automata v0.6.0 • Docs


Yantrix API / @yantrix/automata / CoreLoop

Class: CoreLoop<EventType, EventMetaType, ModelType> ​

CoreLoop is the main loop that connects:

  • an Event Bus (pub/sub for Events),
  • one or more FSMs (IAutomata) through an Event Adapter,
  • Sources (IDataSource, producers of Events),
  • Destinations (IDataDestination, consumers of Events).

Driver-agnostic: the loop only knows how to drain Sources on a tick(). When to tick is left to a driver — call tick() manually, from a reactive observer, or use TimedCoreLoop for a clock.

Generics:

  • EventType: enum/number of Events
  • EventMetaType: a map EventType -> meta payload shape

Extended by ​

Type Parameters ​

• EventType extends TAutomataBaseEventType = TAutomataBaseEventType

• EventMetaType extends { [K in EventType]: any } = Record<EventType, any>

• ModelType extends object = Record<string, any>

Methods ​

registerAutomata() ​

ts
registerAutomata<StateType, ActionType, ContextType, PayloadType>(machine, adapter?): this

Registers an automata and wires data flow:

  • Event Bus -> Event Adapter -> Actions -> dispatch to the Automata
  • FSM transitions -> Event Adapter -> Events -> Event Bus (via EventBus-aware adapter)

Assumptions:

  • The adapter is already configured with addEventListener(...) and addEventEmitter(...) mappings.

Type Parameters ​

• StateType extends number

Automata state enum (number)

• ActionType extends number

Automata action enum (number)

• ContextType extends { [K in number]: any } = Record<StateType, any>

State -> Context mapping

• PayloadType extends { [K in number]: any } = Record<ActionType, any>

Action -> Payload mapping

Parameters ​

• machine: IAutomata<StateType, ActionType, EventType, ContextType, PayloadType, EventMetaType>

Automata instance to register; keyed by its own correlationId

• adapter?: IAutomataEventAdapter<StateType, ActionType, EventType, ContextType, PayloadType, EventMetaType>

Optional AutomataEventAdapter; created if not provided

Returns ​

this

This CoreLoop instance (for chaining)

Throws ​

Error when the same automata instance is already registered


registerDestination() ​

ts
registerDestination<DataModel, DataPacketType, ResolveResultType, ErrorType>(dst): this

Registers a Destination (IDataDestination). The loop subscribes each of the Destination's bound events (getBoundEvents()) to the bus and forwards matching events through update(event), where the Destination's selector + resolver pipeline takes over.

Follow-up events (if any) are emitted by a paired Data Source (see createPromiseDataAdapter), which the loop pumps on its tick — not via the bus handler's result.

Type Parameters ​

• DataModel extends null | object = null | object

• DataPacketType extends object = object

• ResolveResultType = any

• ErrorType = Error

Parameters ​

• dst: IDataDestination<EventType, EventMetaType, DataModel, DataPacketType, ResolveResultType, ErrorType>

Destination to register; must expose a non-empty string id.

Returns ​

this

This CoreLoop instance for chaining.

Throws ​

Error if id is empty or already registered.


registerSlice() ​

ts
registerSlice(slice): this

Registers a Slice's FSMs and Effect Matrix as one composition unit.

Parameters ​

• slice: IAutomataSlice<EventType, EventMetaType, ModelType>

Returns ​

this


registerSource() ​

ts
registerSource<DataPacketType>(src): this

Registers a Source (IDataSource). The Source only enqueues packets; the loop pulls them on each tick (tick() -> drainSource). The Source is started immediately if the loop is already running; otherwise it is started by CoreLoop.start.

Type Parameters ​

• DataPacketType = unknown

Parameters ​

• src: IDataSource<EventType, EventMetaType, DataPacketType>

Data Source to register; must expose a non-empty string id.

Returns ​

this

This CoreLoop instance for chaining.

Throws ​

Error if id is empty or already registered.


tick() ​

ts
tick(): void

One pump cycle: drain every active Source's eventEmitter() into the bus. Public so the loop can be driven deterministically in tests (or manually) instead of by the clock.

Returns ​

void


unregisterAutomata() ​

ts
unregisterAutomata(id): this

Unregisters a previously registered automata.

Behavior:

  • Safely unsubscribes all event bus subscriptions created during registration.
  • Removes automata entry from the registry. No-op if not found.

Parameters ​

• id: string

Automata correlationId passed during registration.

Returns ​

this

This CoreLoop instance for chaining.


unregisterDestination() ​

ts
unregisterDestination(id): this

Unregisters a previously registered Destination by id.

Behavior:

  • Unsubscribes the destination's bus handlers and stops it.
  • Removes the destination from the registry. No-op if not found.

Parameters ​

• id: string

Destination identifier assigned on registration.

Returns ​

this

This CoreLoop instance for chaining.


unregisterSlice() ​

ts
unregisterSlice(id): this

Removes a Slice's FSMs and Effect Matrix together.

Parameters ​

• id: string

Returns ​

this


unregisterSource() ​

ts
unregisterSource(id): this

Unregisters a previously registered Source by id.

Behavior:

  • Calls Source.stop() if present.
  • Removes the source from the registry. No-op if not found.

Parameters ​

• id: string

Source identifier assigned on registration.

Returns ​

this

This CoreLoop instance for chaining.


whenIdle() ​

ts
whenIdle(): Promise<void>

Resolves after the Event cascade and its Effect batch have both completed.

Returns ​

Promise<void>