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

@lexkrstn/fsm

v1.0.4

Published

A strongly typed finite state machine for TypeScript/JavaScript with zero dependencies.

Downloads

29

Readme

npm version

Finite State Machine

Finite state machines are useful for modeling complicated flows and keeping track of state. The package provides a strongly typed FSM implementation for TypeScript and JavaScript.

Motivation

Theoretically, the FSM must comprise only 2 key elements:

  • states
  • transitions between them

In practice, though, implementations typically have at least one of the following flaws:

  1. Mixing up multiple patterns, such as inheriting States from Strategies. There is nothing wrong with this approach if you're not planning to reuse FSM anywhere else. But it's a bad idea for a library, because not every problem requires this mix. In the example below I'm demonstrating how to make use of the both patterns together separating the concerns.
  2. States with onEnter/onExit handlers only. This approach assumes that these two handlers can independently compose a transition function, which is not the case if the transition depends on both the previous and the next state.
  3. Making the client to extend a state class. First, it doesn't make sense to incapsulate each state in a separate class, unless you're mixing it with the Strategy pattern. Secondly, it's not unusual to find youself having to repeat the same blocks of code again and again, especially if the State implementation has the onEnter/onExit handlers for transitions.

All these reasons prompted me to write this library.

Features:

  • SRP-compliant
  • Type-safe
  • Only 1 KB (minified)
  • Zero dependencies
  • Asynchronous transitions
  • Simple state and transition definition

Installation:

npm i -S @lexkrstn/fsm

Example:

In the following example, I'm modeling a Player class for a game. Notice how multiple patterns can be used together without mixing conserns.

// Declare states and events.
type PlayerStates = 'staying' | 'running' | 'dead';
type PlayerEvents = Event<'stop'> | Event<'run', [number, number]> | Event<'die'>;

// Define player strategies
interface PlayerStrategy {
  onUpdate(dt: number): void;
}

class RunStrategy implements PlayerStrategy {
  public onUpdate(dt: number) {
    console.log('running');
  }
}

class StayStrategy implements PlayerStrategy {
  public onUpdate(dt: number) {
    console.log('staying');
  }
}

class DeadStrategy implements PlayerStrategy {
  public onUpdate(dt: number) {
    console.log('dead');
  }
}

function playerStrategyFactory(state: PlayerStates): PlayerStrategy {
  switch (state) {
    case 'staying': return new StayStrategy();
    case 'running': return new RunStrategy();
    case 'dead': return new DeadStrategy();
    default:
      throw new Error(`Unknown state: ${state}`);
  }
}

class Player implements PlayerStrategy {
  private fsm: FSM<PlayerStates, PlayerEvents>;

  private strategy: PlayerStrategy = new StayStrategy();

  public constructor(initialState: PlayerStates = 'staying') {
    this.strategy = playerStrategyFactory(initialState);
    this.fsm = new FSM<PlayerStates, PlayerEvents>(
      initialState,
      [
        ['running', 'stop', 'staying', this.transitionToStaying],
        ['staying', 'run', 'running', this.transitionToRunning],
        ['*', 'die', 'dead', this.transitionToDead],
      ],
    );
  }

  public stop() { return this.fsm.dispatch('stop'); }

  public die() { return this.fsm.dispatch('die'); }

  public run(x: number, y: number) { return this.fsm.dispatch('run', x, y); }

  public isRunning() { return this.fsm.getState() === 'running'; }

  public isStaying() { return this.fsm.getState() === 'staying'; }

  public isDead() { return this.fsm.getState() === 'dead'; }

  public isControllable() { return !this.fsm.isFinal(); }

  public can(action: EventType<PlayerEvents>) { return this.fsm.can(action); }

  public onUpdate(dt: number) {
    this.strategy.onUpdate(dt);
  }

  private transitionToStaying = async () => {
    this.strategy = new StayStrategy();
    await timeout(); // Emulate some async work
  };

  private transitionToRunning = async (x: number, y: number) => {
    this.strategy = new RunStrategy();
    await timeout(); // Emulate some async work
  };

  private transitionToDead = () => {
    this.strategy = new DeadStrategy();
  };
}

(async () => {
  const player = new Player();
  // Dispatch the 'run' event with 2 parameters (position x, y) and wait
  // for the transition to complete.
  await player.run(0, 0);
  player.isRunning(); // => true

  // Check if the player can stop (i.e. the FSM can dispatch this event)
  player.can('stop'); // => true

  // Kill the player, but don't wait.
  await player.die();

  // Did we reach the final state?
  player.isControllable(); // => false
})();