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

stateflowjs

v2.0.0

Published

A simple state machine to create workflows in JavaScript

Readme

stateflowjs

A simple state machine (FSM) for creating workflows in JavaScript.

Requires Node.js ≥ 18. ESM only.

Installation

npm install stateflowjs

Usage

import StateFlow from 'stateflowjs'

const fsm = new StateFlow({
  name: 'my-machine',
  initialState: 'idle',
  states: {
    idle: {
      onEnter () { console.log('entered idle') },
      start () { this.transition('running') }
    },
    running: {
      onExit () { console.log('leaving running') },
      stop () { this.transition('idle') }
    }
  }
})

fsm.start()
fsm.handle('start')

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | name | string | "unknown" | Human-readable machine name | | initialState | string | "uninitialized" | State entered when start() is called | | states | object | — | Map of state names to state definitions |

State definition

Each state is an object with optional lifecycle hooks and any number of action handlers. Inside every handler, this refers to the StateFlow instance.

{
  stateName: {
    onEnter (...payload) { /* called when entering this state */ },
    onExit ()            { /* called when leaving this state  */ },
    someAction (...args) { this.transition('otherState') }
  }
}

onEnter and onExit are reserved — they cannot be invoked via handle().

Methods

start(parent?, ...payload)boolean

Starts the machine and enters initialState. payload is forwarded to onEnter. Returns false (and emits already_running) if already running.

stop(parentAction?, ...payload)boolean

Stops the machine and resets to idle. If parentAction is provided and a parent machine is set, the parent will handle that action with payload.

transition(toState, ...payload)boolean

Transitions to toState. Calls onExit on the current state and onEnter on the target state. Returns false and emits error if toState is not defined.

handle(action, ...payload)boolean

Invokes the named action on the current state. Returns false if the action is not defined.

getCurrentState()string

Returns the current state key.

isRunning()boolean

Returns true if the machine has been started and not yet stopped.

Events

| Event | Payload | Description | |-------|---------|-------------| | started | { name, parent } | Machine started | | transition | { name, prevState, nextState, parent } | State changed | | action_handled | { name, state, action } | Action executed | | already_running | { name, parent } | start() called while running | | error | { code, name, prevState, nextState, parent } | Transition to undefined state |

Parent / child machines

A child machine can reference its parent by passing it to start(). When the child stops with a parentAction, the parent will handle that action automatically.

const child = new StateFlow({ name: 'child', initialState: 'init', states: { init: {} } })
const parent = new StateFlow({
  name: 'parent',
  initialState: 'waiting',
  states: {
    waiting: {
      onEnter () { child.start(this) },
      childDone (result) { console.log('child finished with', result) }
    }
  }
})

parent.start()
child.stop('childDone', { status: 'ok' })

Example — ATM

See examples/atm/atm.js for a three-state machine (uninitializedunauthorizedauthorized) with deposit and withdrawal actions.

node examples/atm/atm.js

Migrating from v1

| v1 | v2 | |----|----| | require('stateflowjs') | import StateFlow from 'stateflowjs' | | options.intialstate | options.initialState | | Node.js any | Node.js ≥ 18 |