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

@seyan/ts-statemachine

v1.0.2

Published

![npm (tag)](https://img.shields.io/npm/v/@seyan/ts-statemachine/latest) ![NPM License](https://img.shields.io/npm/l/%40seyan%2Fts-statemachine)

Downloads

3

Readme

TypeScript有限状态机

npm (tag) NPM License

使用TypeScript装饰器实现的有限状态机,需要在tsconfig.json里启用experimentalDecorators编译器选项

安装

npm install @seyan/ts-statemachine

使用示例

import {
  AFSM,
  Event,
  Status,
  StateMachine,
  EventHandlerResponse,
  StatusType
} from '@seyan/ts-statemachine';

@StateMachine()
class FSM extends AFSM {
  @Status("s1")
  @Event('e1')
  f(): EventHandlerResponse {
    return { next: 's2' }
  }
}

@StateMachine()
class TestStm extends AFSM {
  // 在s1状态下触发e1事件
  @Status("s1")
  @Event("e1")
  s1_e1(): EventHandlerResponse {
    // 业务逻辑处理
    // ...
    // 转换为s2状态
    return { next: "s2" };
  }

  // 在s1状态下触发e2事件
  @Status("s1")
  @Event("e2")
  s1_e2(): EventHandlerResponse {
    // 业务逻辑处理
    // ...
    // 转换为s3状态
    return { next: "s3" };
  }

// 在s2状态下触发e1事件
  @Status("s2")
  @Event("e1")
  s2_e1(): EventHandlerResponse {
    // 业务逻辑处理
    // ...
    // 转换为s1状态
    return { next: "s1" };
  }

  // 在s2状态下触发e2事件
  @Status("s2")
  @Event("e2")
  s2_e2(): EventHandlerResponse {
    // 业务逻辑处理
    // ...
    // 转换为s3状态
    return { next: "s3" };
  }

  // 在s3状态下触发e1事件
  @Status("s3")
  @Event("e1")
  s3_e1(): EventHandlerResponse {
    // 业务逻辑处理
    // ...
    // 转换为s1状态
    return { next: "s1" };
  }

  @Status("s3")
  @Event("e2")
  s3_e2(): EventHandlerResponse {
    return { next: "s2" };
  }

  // 状态工厂可以支持状态列表 在s1 s2 s3状态下触发setState处理逻辑相同
  @Status(["s1", "s2", "s3"])
  @Event("setState")
  setState(state: StatusType): EventHandlerResponse {
    return { next: state };
  }

  // 可以省略状态装饰器 表示任意状态下都可以触发
  @Event("noChange")
  noChange() {
    // 返回值为空 表示不改变状态
    return;
  }

  // 常规函数成员
  getCurrentState() {
    // 获取当前状态
    return this.currentState;
  }
  
  // 常规函数成员
  nomalFunction2(s?: string) {
    return s;
  }
}

(async function test() {
  const fsm = new TestStm();
  fsm.setState("s1");
  await fsm.handleEvent("e1"); // currentState => s2
  await fsm.handleEvent("e2"); // currentState => s3
  await fsm.handleEvent("noChange"); // currentState => s3
  await fsm.handleEvent("unregisteredEvent") // Promise.reject(UnregisteredEventError("触发未注册事件"))
})()