The name of the machine.
The chart and other configuration.
The current state of the machine and the machine itself.
import m from 'mithril'
import { withHooks } from 'mithril-hooks'
import { useStatebotFactory } from 'statebot/hooks/mithril'
const CHART = `
idle ->
loading -> (loaded | failed) ->
idle
`
const EVENT = {
START_LOADING: 'start-loading',
LOAD_SUCCESS: 'load-success',
LOAD_ERROR: 'load-error'
}
const LoadingButton = withHooks(props => {
const { state, bot } = useStatebotFactory(
'loading-button',
{
chart: CHART,
startIn: 'idle',
logLevel: 4,
performTransitions: ({ Emit }) => ({
'idle -> loading': {
on: EVENT.START_LOADING,
then: () => setTimeout(
Emit(EVENT.LOAD_SUCCESS),
1000
)
},
'loading -> loaded': {
on: EVENT.LOAD_SUCCESS
},
'loading -> failed': {
on: EVENT.LOAD_ERROR
}
}),
onTransitions: () => ({
'loading -> failed': () => {
console.log('Oops...')
}
})
}
)
return (
<button
className={state}
onClick={bot.Emit(EVENT.START_LOADING)}
disabled={bot.inState('loading')}
>
{bot.inState('idle', 'Load')}
{bot.inState('loading', 'Please wait...')}
{bot.inState('loaded', 'Done!')} ({state})
</button>
)
})
For Statebots whose life-cycles are tied to the components using them,
useStatebotFactory: