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

fsm.js

v2.0.1

Published

A tiny implementation of a finite state machine.

Downloads

27

Readme

Logo

Fsm

Build Status CodeFactor DeepSource

A tiny implementation of a finite state machine written in Javascript.

Current version: 2.0.0

Lead Maintainer: Halim Qarroum

Install

npm install --save fsm.js

Usage

Import the FSM

Fsm.js is distributed as an ESM module that you can import in your implementation.

import Fsm from 'fsm.js';

Creating the FSM

To create an instance of the state machine you simply have to call its constructor :

const fsm = new Fsm();

Creating a state

A state is an instance of a Fsm.State object. It takes a reference to the state machine it is associated with as well as optional callbacks associated with the state lifecycle.

As you can see below, creating a state is pretty straightforward :

const state = new Fsm.State({

  /**
   * An optional name for the state.
   */
  name: 'initialization',

  /**
   * A reference to the used state machine.
   */
  fsm: fsm,

  /**
   * An optional callback invoked when the state is
   * entered.
   */
  onEntry: () => {},

  /**
   * An optional callback invoked upon a received
   * event.
   */
  onEvent: (event) => {
    // Do something with the received `event`.
  },

  /**
   * An optional callback invoked when the state is
   * exited.
   */
  onExit: () => {}
});

Starting the state machine

When a state machine is created, it is stopped by default. To start a state machine you need to give it a reference to the initial state in which it should be starting :

fsm.start(state);

Playing around with events

Usually, you will want states to react to received events by executing actions and transitioning between one another. This can be achieved through the postEvent and transitionTo primitives as shown in the example below.

/**
 * The `initialization` state of the system.
 */
const initialization = new Fsm.State({
  fsm: fsm,
  name: 'initialization',
  onEntry: () => console.log('Initialization ...'),
  onExit:  () => console.log('Initialization completed'),
  onEvent: (event) => {
    if (event === 'init.done') {
      this.transitionTo(initialized);
    }
  }
});

/**
 * The `initialized` state.
 */
const initialized = new Fsm.State({
  fsm: fsm,
  name: 'initialized',
  onEntry: () => console.log('System initialized')
});

// Starting the fsm.
fsm.start(initializing);

// Simulating an asynchronous event.
setTimeout(() => fsm.postEvent('init.done'), 1000);

The above code will output :

Initialization ...
Initialization completed
System initialized

Example

In the elevator example, we implemented, as an example of a real life problem, the bare minimum functions provided by an elevator using a finite state machine.

For the sake of simplicity, the implementation is really dummy, meaning that smart behaviours such as ordering of the levels given their location along the elevator path are not implemented.