API Reference
Primary Exports
createMachine(config)creates a machine descriptor with a statechart-shaped config.MobXStateMachinestarts and observes the machine through MobXstate's runtime.@orderofchaos/mobxstate/decoratorsexports a decorator-compatibleMobXStateMachinewith the same inheritance API for stores that use MobX property decorators.MachineOptions<Store, Event, Typegen>provides fallback and overrideactions,guards,effects, anddelays.MachineAction,MachineGuard, andMachineEffectdescribe callback contracts wherethisis the MobX store and the first argument is the event.
MobX Decorator Stores
Use the default package entrypoint when subclasses call makeObservable with an annotation map:
import { MobXStateMachine } from "@orderofchaos/mobxstate";
Use the decorators entrypoint when subclasses use MobX property decorators:
import { MobXStateMachine } from "@orderofchaos/mobxstate/decorators";
import { makeObservable, observable } from "mobx";
class CounterStore extends MobXStateMachine<CounterStore, CounterEvent> {
@observable
public count = 0;
constructor() {
super(counterMachine);
makeObservable(this);
}
}
Both entrypoints keep the same extends MobXStateMachine<Store, Event> store shape and share the same state machine runtime.
Runtime Behavior
- Named actions resolve to store methods first and run inside
runInAction. - Named guards resolve to store getters, boolean properties, or pure methods.
- Named delays resolve to store number getters, properties, or methods.
- Full macrosteps are batched in one MobX transaction.
invoke: "methodName"starts a store lifecycle effect that may return a promise, cleanup function, child machine, orvoid.- Promise rejections and synchronous invoke failures route through
onErrorwhen possible. - Action and guard failures are fatal runtime errors and clean up active resources before rethrowing.
- Cleanup failures are reported as
MachineCleanupErrorafter all cleanup functions have been attempted.
Observable State
stateis the current observable state value.snapshotis the latest snapshot withvalue,event, andmatches(...).send(event)accepts object events and typed string events without required payload fields.matches(state)checks the current state.readyresolves after the internal actor has started.
Persistence
persistentKeystores the current state underMachinesStorage.- Saved values are validated before restore.
versionstores versioned persistence records.transformPersistedState(state, fromVersion)can normalize persisted values before validation.
Feature Support
- Supported:
entry,exit, named actions, named guards, named delays, numeric delays,after,always,invoke, promise effects, cleanup effects, child machines, nested states, parallel states, final states, shallow history,onDone,onError, typedsend, persistence validation, and persistence versioning. - Removed:
MachineOptions.services,MachineOptions.activities, state nodeactivities, and callback invoke services. - Explicit runtime error:
history: "deep". - Out of scope: separate XState-style machine
contextruntime.