potter-fsm
v0.1.8
Published
A Simple State Machine Lib Build At ShangHai
Downloads
4
Readme
POTTER-FSM
English | 简体中文
📦 Install:
npm install potter-fsm
yarn add potter-fsm
💻 Development
Use Gitpod, a free online dev environment for GitHub.
Or clone locally:
$ git clone [email protected]:liuwill/potter-fsm.git
$ cd potter-fsm
$ yarn install
$ yarn test
$ yarn build
$ yarn publish
🔧 Example:
import PotterStateMachine, { StateContext, AbstractState } from 'potter-fsm'
function buildPrintState(state) {
return PotterStateMachine.NewState(
state,
(ctx) => {
console.log(`Enter ${state}`)
},
(ctx) => {
console.log(`Achieve ${state}`)
},
(ctx) => {
console.log(`Quit ${state}`)
}
)
}
const account = { balance: 1000 }
const machine = PotterStateMachine.New(
{
schema: [
{
action: 'transfer_money',
source: [PotterStateMachine.StateBegin],
destination: 'transfer',
},
{
action: 'transfer_success',
source: ['transfer'],
destination: PotterStateMachine.StateEnd,
},
],
states: {
transfer: buildPrintState('transfer'),
},
initState: PotterStateMachine.StateBegin,
},
account
)
const actionList = [
'transfer_money',
'transfer_success',
]
for (const act of actionList) {
if (machine.isEnd()) {
console.log('Exit Machine End')
break
}
console.log('do action:', act)
const err = machine.trigger({ type: act })
if (err && err instanceof Error) {
throw err
}
}
console.log('All Action Executed In:', actionList)