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 🙏

© 2026 – Pkg Stats / Ryan Hefner

stateum

v1.1.1

Published

An attachable state machine for your javascript objects.

Readme

Stateum

Circle CI

An attachable state machine for your javascript objects, made necessary by the need to make sense of complex business logic.

Installation

$  npm install stateum

Stateum is supported in node v4+.

Table of Contents

Use Cases

  • Turning on and off a light switch, e.g. ON, OFF, FAILED
  • User payment states, e.g. UNPAID, PAID, CANCELLED, PENDING
  • Data processing, e.g. PENDING, PROCESSING, COMPLETE, FAILED, CANCELLED
  • Email, e.g. PENDING, SENDING, SENT, FAILED, CANCELLED

Basic Usage

import stateum from 'stateum'

const myMachine = {
  getState() {...},
  states: {
    DUMMYSTATE1: {
      transitions: {...}
    },
    ...
  }
}
const obj = {
    state: 'PAUSE'
}
const stateMachine = stateum(myMachine)

stateMachine(obj)

Four methods are now attached to your object allowing you to interact with the state machine.

Get current state: obj.getState()

List all states in machine: obj.states()

List available states to transition to: obj.transitionState()

Transition to one of the available states: obj.transitionTo()

Setting up a Basic State Machine

The state machine object defines the way you can interact with the states of your object. Two pieces are needed for a basic functioning machine, defining getState and the states with their available transitions.

In this case this state machine expects a value of state on the object to tell us the state of this object. It probably looks something like:

const obj = {
  state: 'PAUSE'
}

Next we need to define the available states and the transitions of those states, in the case of our example we have three available states which we can retrieve by calling obj.getStates(). This would return to us ['PAUSE', 'START', STOP].

Since in our exmaple we arein the PAUSE state I want to list out the available states to transition to and my current state should I can make a logical decision on transitioning. obj.transitionStates() returns:

{
  state: 'PAUSE',
  transitionStates: [ 'START', 'STOP' ] }
}

Finally if I want to request a transition to another state obj.transitionTo('START') will return me the transitioned object with all completed business logic.

export default {

  // Defines how to retrieve the state of your object, context this is of the object.
  getState(){
    if(!this.state){
      throw new Error('Failed to find state');
    }
    return this.state;
  },

  // Define available states
  states: {
    PAUSE: {

      // define available transitions for this state
      transitions: {
        START() {
          this.state = 'START'
          return this
        },
        STOP() {
          this.state = 'STOP'
          return this
        }
      }
    },
    START: {
      transitions: {
        START() {
          return this
        },
        PAUSE() {
          this.state = 'PAUSE'
          return this
        },
        STOP() {
          this.state = 'STOP'
          return this
        }
      }
    },
    STOP: {
      transitions: {
        START() {
          this.state = 'START'
          return this
        }
      }
    }
  }
}

Advanced Stateum usage, but not too advanced

Stateum is not isolated to synchronous calls and returns everything as a promise allowing us to make for complex state transitions.

Two available methods are allowTransition(to, transitionState) which allows for us to change the complexity of what states are available to be transitioned to. allowTransition will be passed the returned object from the transitionStates() method nested in each state (more on this method later) and the state being requested in the transitionTo(to).

In the exmaple below assuming we are in the 'PAUSE' state:

const obj = {
  state: 'PAUSE'
}
const transitionStates = await obj.transitionStates()
// returns:
// {
//  state: 'PAUSE',
//  transitionState: {
//    START: true,
//    STOP: false
//  }
// }

Requesting obj.transitionTo('START') will evaluate in the allowTransition method to valid and transition to start. Because we see 'STOP' evaluated to 'false', requesting obj.transitionTo('STOP') will throw an exception that the transition is not valid.

export default {
  getState() {
    return new Promise((res, rej) => {
      if(!this.state){
       rej(new Error('Failed to find state'))
      }
      res(this.state)
    })
  },
  allowTransition(to, transitionStates) {
    return transitionStates[to]
  },
  states: {
    PAUSE: {
      allowTransitionSTART(){
        return true;
      },
      allowTransitionSTOP(){
        return false;
      },
      transitionStates() {
        return {
          START: this.STATEUM.allowTransitionSTART(),
          STOP: this.STATEUM.allowTransitionSTOP()
        };
      },
      transitions: {
        START() {
          if(!this.STATEUM.allowTransitionSTART()){
            throw new Error('Transition START not permitted');
          }
          return new Promise((res, rej) => {
            this.state = 'START'
            res(this)
          })
        },
        STOP() {
          if(!this.STATEUM.allowTransitionSTOP()){
            throw new Error('Transition STOP not permitted');
          }
          return new Promise((res, rej) => {
            this.state = 'STOP'
            res(this)
          })
        }
      }
    },
    START: {
      transitionStates() {
        return {
          START: true,
          STOP: true,
          PAUSE: true
        };
      },
      transitions: {
        START() {
          return this
        },
        PAUSE() {
          return new Promise((res, rej) => {
            this.state = 'PAUSE'
            res(this)
          })
        },
        STOP() {
          return new Promise((res, rej) => {
            this.state = 'STOP'
            res(this)
          })
        }
      }
    },
    STOP: {
      transitionStates() {
        return {
          START: true,
          STOP: false,
          PAUSE: false
        };
      },
      transitions: {
        START() {
          return new Promise((res, rej) => {
            this.state = 'START'
            res(this)
          })
        }
      }
    }
  }
}

Connecting Stateum to Sequelize models

Attaching Stateum to the instance methods will make the statemachine available on all instances of your model and sequelize available within your state machine.

import stateum from 'stateum'

const myMachine = {
  getState() {...},
  states: {
    DUMMYSTATE1: {
      transitions: {...}
    },
    ...
  }
}

export default (sequelize, DataTypes) => {
  const definition = {
    id: type: DataTypes.INTEGER,
    ...
  }
  const options = {
    instanceMethods: {...}
  }

  const myModelMachine = stateum(myMachine)
  myModelMachine(options.instanceMethods)

  return sequelize.define('myModel', definition, options);
};