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

@pxtrick/fsm

v0.0.7

Published

A finite-state machine

Downloads

3

Readme

FSM (Finite-State Machine)

A JavaScript module which provides simple finite-state machine behavior.

These core elements of a traditional finite-state machine are available for configuration:

  • State names
  • State transitions (or "events")
  • Initial state
  • Pre-transition behavior
  • Post-transition behavior
  • Actions always occurring upon state entry
  • Actions always occurring upon state exit

Input Configuration Specification

(See configuration sample below for JSON structure)

  • startState: The state at which a newly created state machine will be initialized. [type: string]
  • states: An object specifying all possible name-value object pairs for states and transition events. [type: JS object]
    • events: Contains named objects which give details for the possible transition events for this state. [type: JS object]
      • toState: The state name to which we will transition when receiving this event. [type: string]
      • onBefore: A callback which is fired before the state change has occurred. [type: function]
      • onAfter: A callback which is fired after the state change has occurred. [type: function]
    • actions: The actions which will always be executed when a state is entered or exited. (This is kind of conceptually similar to the execution order of constructors/destructors.) [type: JS object]
      • onEnter: A callback which is fired every time the state is entered, regardless of where it just transitioned from. [type: function]
      • onExit: A callback which is fired every time the state is exited, regardless of where it is transitioning to. [type: function]

NOTE: During creation of a new state machine object, the configuration is validated. If errors are found, the value returned by method isValid() will be false, and the value from method status() will hold the status code of the problem.

Sample Module Usage (example 1)

Let's model a simple light switch; Here's the state diagram: login sequence state diagram And the corresponding code would be ...

// +-----------------------+
// | Model a light switch. |
// +-----------------------+
var config = {
    startState: 'Off',
    states: {
        'Off': { events: { 'turnOn': { toState: 'On' } } },
        'On': { events: { 'turnOff': { toState: 'Off' } } }
    }
};

// Load the module and call the factory method to create a new instance.
var FSM = require('@pxtrick/fsm');
var fsm = FSM.newFSM(config);

if (fsm.isValid()) {
    // Handle state-changing events.
    fsm.handleEvent('turnOn');   // fsm.currentState() === 'On';
    fsm.handleEvent('turnOff');  // fsm.currentState() === 'Off';
}

NOTE: For brevity, the idempotent transitions of [Off]->(turnOff)->[Off] and [On]->(turnOn)->[On] are not included in this code example.

Sample Module Usage (example 2)

Let's model a more complex login sequence; Here's the state diagram: login sequence state diagram And the corresponding code would be ...

// +-------------------------+
// | Model a login sequence. |
// +-------------------------+
var config = {
    startState: 'LoggedOut',
    states: {
        'LoggedOut': {
            events: {
                'login': { toState: 'LoggingIn'}
            },
            actions: {
                onEnter: function() { console.log('You have been logged out.') }
            }
        },
        'LoggingIn': {
            events: {
                'success': { toState: 'LoggedIn'},
                'failure': {
                    toState: 'LoggedOut',
                    onBefore: function() { console.log('Login FAILED.') }
                }
            }
        },
        'LoggedIn': {
            events: {
                'logout': {
                    toState: 'LoggedOut',
                    onAfter: function() { console.log('Thanks for playing!') }
                }
            },
            actions: {
                onEnter: function() { console.log('You are now logged in.') }
            }
        }
    }
};

// Load the module and call the factory method to create a new instance.
var FSM = require('@pxtrick/fsm');
var fsm = FSM.newFSM(config);

if (fsm.isValid()) {
    // Handle state-changing events.
    fsm.handleEvent('login');    // fsm.currentState() === 'LoggingIn';
    fsm.handleEvent('failure');  // fsm.currentState() === 'LoggedOut';
    fsm.handleEvent('login');    // fsm.currentState() === 'LoggingIn';
    fsm.handleEvent('success');  // fsm.currentState() === 'LoggedIn';
    fsm.handleEvent('logout');   // fsm.currentState() === 'LoggedOut';
}

NOTE: For brevity, no idempotent transitions are included in this code example.

Dependencies:

This module is dependent on lodash.