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

afsm

v2.4.7

Published

automatic finite state machine

Downloads

5,154

Readme

智能状态机

  • 目的:更加自动和简洁的实现状态的迁移。
  • 原理:通过装饰器,对异步函数进行包装,使得调用异步函数可以自动修改状态,并且自动实现异步调用过程中的中间状态。

👉演示视频

安装

npm install afsm

使用


import { FSM, ChangeState } from 'afsm'

class MyFSM extends FSM {
  @ChangeState(FSM.INIT,'state1')
  async gotoState1() {

  }
  @ChangeState('state1','state2')
  async gotoState2() {

  }
}
const obj =  new MyFSM()
obj.gotoState2()//will throw error
obj.gotoState1().then(()=>{//will change to state1
  return obj.gotoState2()//then change to state2
})

事件

其他对象如果希望得知状态机的变化,可以通过监听事件实现。

每一种状态变更时,都会发出事件,如果是中间过渡状态则会自动加ing。

const obj =  new MyFSM()
obj.on('gotoState2ing',(oldState) => {
  console.log(obj,'is going to state2')
})
obj.on('state2',(oldState) => {
  console.log(obj,'is state2 now')
})

此外为了方便监听所有事件,还会同时发出changeStated事件。异步调用的成功与失败都会导致状态变更,可以通过下面的监听来判断。

const obj =  new MyFSM()
obj.on(FSM.STATECHANGED,(newState:State, oldState:State) => {
 
})

其中State的定义参考下面的源码中的定义

export type State = string | MiddleState;
// 中间过渡状态
export class MiddleState {
  constructor(public oldState: State, public newState: string, public action: string) {
  }
  toString() {
    return `${this.action}ing`;
  }
}

使用浏览器扩展可视化状态机

afsm浏览器扩展可以在开发人员工具的标签页中生成一个可视化页面,供实时观察代码运行的情况。

使用方法

1.将仓库 clone 下来后,打开浏览器扩展管理,点击加载解压缩的扩展,选择仓库中的 devtools/dist 目录。 2.打开一个页面,并打开控制台,选择智能状态机标签,即可看到扩展页面。 3.当项目中使用了AFSM后,会自动向扩展页面发送信息,扩展页面可以看到项目中的状态机图以及状态机的变更记录。