npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@awefeng/state-machine

v1.1.2

Published

TS实现的状态机,包含FSM

Downloads

10

Readme

state-machine

TS实现的简单状态机,目前支持FSM(finite state machine)

license @awefeng/state-machine

introduction

用TS实现了一个简单的状态机,目前仅支持FSM状态机,后续会增加其他类型状态机。

支持commonjses moudle两种规范。

install

项目使用 nodenpm,请确保你本地安装了它们。

npm i @awefeng/state-machine

Finite State Machine

定义: 有限状态机

示例: FSM Demo

初始化一个FSM

  1. 从包中引入FiniteStateMachine
  2. 初始化一个FSM对象,其中statetransitions为必填项,onTransiteError为转换过程出现错误时的回调函数
  3. state为默认状态
  4. transitions为各状态之间的关系:from状态通过event时间到达下一状态to
  import { FiniteStateMachine } from '@awefeng/state-machine'

  const fsm = new FiniteStateMachine({
    state: 'opened', // 默认状态
    transitions: [{
      // 门开启时关门流程,从状态“门开着的”通过事件“关门”到达下一状态“门关着的”
      { event: 'close', from: 'opened', to: 'closed' }, 
      {
        event: 'open',
        from: 'closed',
        to: 'opened',
        // beforeTransite 状态过渡前的回调函数,参数from,to,event
        beforeTransite: ({ from, to, event }) => {},
        // afterTransite 状态过渡后的回调函数,参数from,to,event
        afterTransite: ({ from, to, event }) => {},
        //  状态过渡的完以后需要执行的事件(某种意义上和afterTransite是一样的)
        action: ({ from, to, event }) => {}
      }
    }],
    // 转换状态时错误兜底函数
    onTransiteError: ({from, to, event}) => console.log
  })

API

fsm.getState()

获取当前所在状态,返回值为当前state

fsm.setState(state)

设置当前状态,该API不会调用任何回调,只是单纯设置状态

fsm.getStateTransitions()

获取当前状态下可以进行的转换,返回值为部分transitions

fsm.canTransite(event)

获取当前状态下是否可以进行某一event的转换,返回值为boolean类型。例:state'closed'状态下,fsm.canTransite('close') //false

fsm.getHistory()

获取状态改变的历史数据,返回值为一个数组{from: string, to: string, event: string, type :'setState' |'transite'}[],其中type指的是通过setState改变或者通过transite改变。

fsm.transite(event?)

转换状态,将当前状态转换到下一状态,返回值为boolean类型,转换成功为true,失败为false

如果关系图中当前状态没有传入的event关系,则转换失败。

如果当前状态下,该event只有一个关系图时,可以不传入event,因为是唯一的;如果不是唯一的(即当前状态对应多个下一状态),则必须指定event,否则转换失败。

欢迎贡献

非常欢迎!提一个Issue 或者提交一个 Pull Request。