potter-fsm
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

POTTER-FSM

CI status NPM version

English | 简体中文

📦 Install:

npm install potter-fsm
yarn add potter-fsm

💻 Development

Use Gitpod, a free online dev environment for GitHub.

Open in Gitpod

Or clone locally:

$ git clone git@github.com: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)

Readme

Keywords

none

Package Sidebar

Install

npm i potter-fsm

Weekly Downloads

1

Version

0.1.8

License

MIT

Unpacked Size

13.3 kB

Total Files

10

Last publish

Collaborators

  • liuwill