Skip to content

@ue-too/being / index / TemplateStateMachine

類別: TemplateStateMachine<EventPayloadMapping, Context, States, EventOutputMapping>

定義於: interface.ts:561

Concrete implementation of a finite state machine.

備註

This class provides a complete, ready-to-use state machine implementation. It's generic enough to handle most use cases without requiring custom extensions.

Features

  • Type-safe events: Events and their payloads are fully typed via the EventPayloadMapping
  • State transitions: Automatic state transitions based on event handlers
  • Event outputs: Handlers can return values that are included in the result
  • Lifecycle hooks: States can define uponEnter and beforeExit callbacks
  • State change listeners: Subscribe to state transitions
  • Shared context: All states access the same context object for persistent data

Usage Pattern

  1. Define your event payload mapping type
  2. Define your states as a string union type
  3. Create state classes extending TemplateState
  4. Instantiate TemplateStateMachine with your states and initial state

範例

Basic vending machine state machine

typescript
type Events = {
  insertCoin: { amount: number };
  selectItem: { itemId: string };
  cancel: {};
};

type States = "IDLE" | "PAYMENT" | "DISPENSING";

interface VendingContext extends BaseContext {
  balance: number;
  setup() { this.balance = 0; }
  cleanup() {}
}

const context: VendingContext = {
  balance: 0,
  setup() { this.balance = 0; },
  cleanup() {}
};

const machine = new TemplateStateMachine<Events, VendingContext, States>(
  {
    IDLE: new IdleState(),
    PAYMENT: new PaymentState(),
    DISPENSING: new DispensingState()
  },
  "IDLE",
  context
);

// Trigger events
machine.happens("insertCoin", { amount: 100 });
machine.happens("selectItem", { itemId: "A1" });

參閱

Extended by

型別參數

EventPayloadMapping

EventPayloadMapping

Object mapping event names to their payload types

Context

Context extends BaseContext

Context type shared across all states

States

States extends string = "IDLE"

Union of all possible state names (string literals)

EventOutputMapping

EventOutputMapping extends Partial<Record<keyof EventPayloadMapping, unknown>> = DefaultOutputMapping<EventPayloadMapping>

Optional mapping of events to their output types

實作

  • StateMachine<EventPayloadMapping, Context, States, EventOutputMapping>

建構函式

建構函式

new TemplateStateMachine<EventPayloadMapping, Context, States, EventOutputMapping>(states, initialState, context, autoStart): TemplateStateMachine<EventPayloadMapping, Context, States, EventOutputMapping>

定義於: interface.ts:592

參數

states

Record<States, State<EventPayloadMapping, Context, States, EventOutputMapping>>

initialState

States

context

Context

autoStart

boolean = true

回傳

TemplateStateMachine<EventPayloadMapping, Context, States, EventOutputMapping>

屬性

_context

protected _context: Context

定義於: interface.ts:579


_currentState

protected _currentState: "INITIAL" | "TERMINAL" | States

定義於: interface.ts:574


_happensCallbacks

protected _happensCallbacks: (args, context) => void[]

定義於: interface.ts:582

參數

args

EventArgs<EventPayloadMapping, string> | EventArgs<EventPayloadMapping, keyof EventPayloadMapping>

context

Context

回傳

void


_initialState

protected _initialState: States

定義於: interface.ts:590


_stateChangeCallbacks

protected _stateChangeCallbacks: StateChangeCallback<States>[]

定義於: interface.ts:581


_states

protected _states: Record<States, State<EventPayloadMapping, Context, States, EventOutputMapping>>

定義於: interface.ts:575


_statesArray

protected _statesArray: States[]

定義於: interface.ts:580


_timeouts

protected _timeouts: number | undefined = undefined

定義於: interface.ts:589

存取器

currentState

Getter 簽章

get currentState(): "INITIAL" | "TERMINAL" | States

定義於: interface.ts:723

回傳

"INITIAL" | "TERMINAL" | States

實作了

StateMachine.currentState


possibleStates

Getter 簽章

get possibleStates(): States[]

定義於: interface.ts:731

回傳

States[]

實作了

StateMachine.possibleStates


states

Getter 簽章

get states(): Record<States, State<EventPayloadMapping, Context, States, EventOutputMapping>>

定義於: interface.ts:735

回傳

Record<States, State<EventPayloadMapping, Context, States, EventOutputMapping>>

實作了

StateMachine.states

方法

happens()

呼叫簽章

happens<K>(...args): EventResult<States, K extends keyof EventOutputMapping ? EventOutputMapping[K<K>] : void>

定義於: interface.ts:653

型別參數
K

K extends string | number | symbol

參數
args

...EventArgs<EventPayloadMapping, K>

回傳

EventResult<States, K extends keyof EventOutputMapping ? EventOutputMapping[K<K>] : void>

實作了

StateMachine.happens

呼叫簽章

happens<K>(...args): EventResult<States, unknown>

定義於: interface.ts:659

型別參數
K

K extends string

參數
args

...EventArgs<EventPayloadMapping, K>

回傳

EventResult<States, unknown>

實作了

StateMachine.happens


onHappens()

onHappens(callback): void

定義於: interface.ts:711

參數

callback

(args, context) => void

回傳

void

實作了

StateMachine.onHappens


onStateChange()

onStateChange(callback): void

定義於: interface.ts:707

參數

callback

StateChangeCallback<States>

回傳

void

實作了

StateMachine.onStateChange


reset()

reset(): void

定義於: interface.ts:613

回傳

void

實作了

StateMachine.reset


setContext()

setContext(context): void

定義於: interface.ts:727

參數

context

Context

回傳

void

實作了

StateMachine.setContext


start()

start(): void

定義於: interface.ts:619

回傳

void

實作了

StateMachine.start


switchTo()

switchTo(state): void

定義於: interface.ts:648

參數

state

"INITIAL" | "TERMINAL" | States

回傳

void

實作了

StateMachine.switchTo


wrapup()

wrapup(): void

定義於: interface.ts:632

回傳

void

實作了

StateMachine.wrapup