Statecharts is a configuration factory, just like merge, namespaced and lazy. It allows you to restrict what actions are to be run in certain states.
statechart
The factory function you use to wrap an Overmind configuration. You add one or multiple charts to the configuration, where the key is the id of the chart.
When a transition state is changed, any exit defined in current transition state will be run first. Nested charts in a transition state with an exit defined will run before parents.
Unlike traditional statecharts Overmind uses its actions as transition types. This keeps a cleaner chart definition and when using Typescript the actions will have correct typing related to their payload. The actions defined are the only actions allowed to run. They can optionally lead to a new transition state, even conditionally lead to a new transition state.
import { Statechart, statechart } from'overmind/config'import*as actions from'./actions'import { state } from'./state'constconfig= { actions, state}constchart:Statechart<typeof config, { STATE_A:void STATE_B:void}> = { initial:'STATE_A', states: { STATE_A: { on: {// Allow execution, but stay on this transition state someAction:null,// Move to new transition state when executed someOtherAction:'STATE_B',// Conditionally move to a new transition state someConditionalAction: { target:'STATE_B',condition: state =>state.isTrue } } }, STATE_B: {} }}exportdefaultstatechart(config, chart)
nested
A nested statechart will operate within its parent transition state. The means when the parent transition state is entered or exited any defined entry and exit actions will be run. When the parent enters its transition state the initial state of the child statechart(s) will be activated.
Multiple statecharts will run in parallel. Either for the factory configuration or nested charts. You can add the same chart multiple times behind different ids.