Skip to content

@yantrix/automata v0.1.0Docs


Yantrix API / @yantrix/automata / CoreLoop

Class: CoreLoop<EventType, EventMetaType>

CoreLoop is a minimal main-loop that connects:

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

Responsibilities:

  • Subscribe the adapter to observed Events and dispatch mapped Actions to the FSM.
  • After each FSM transition, translate emitted Events via the adapter and re-dispatch them to the Event Bus.
  • Manage lifecycle (start/stop) of Sources and Destinations.

Generics:

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

Type Parameters

EventType extends TAutomataBaseEventType = TAutomataBaseEventType

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

Methods

registerAutomata()

ts
registerAutomata<StateType, ActionType, ContextType, PayloadType>(
   id, 
   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

id: string

Unique identifier for the automata within this CoreLoop

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

Automata instance to register

adapter?: AutomataEventAdapter

Optional AutomataEventAdapter; created if not provided

Returns

this

This CoreLoop instance (for chaining)

Throws

Error when an automata with the same id is already registered


registerDestination()

ts
registerDestination(dst): this

Registers a Destination. Destination is responsible for its own subscriptions via bind().

Parameters

dst: IEventDestination<EventType, EventMetaType>

Destination to register

Returns

this

This CoreLoop instance for chaining.

Throws

Error if a Destination with the same id is already registered


registerSource()

ts
registerSource(src): this

Registers a Source. The Source is started immediately if the loop is already running. Note: This method treats the passed-in object as immutable and does NOT mutate src.id. Non-empty src. Id is required.

Parameters

src: IEventSource<EventType, EventMetaType>

Returns

this


unregisterAutomata()

ts
unregisterAutomata(id): this

Unregisters a previously registered automata by id.

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 identifier passed during registration.

Returns

this

This CoreLoop instance for chaining.


unregisterDestination()

ts
unregisterDestination(id): this

Unregisters a previously registered Destination by id.

Behavior:

  • Invokes the unsubscription function returned by Destination.bind().
  • 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.


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.