0.1.8 • Published 2 years ago

potter-fsm v0.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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)
0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago