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

@stateforward/hsm

v2.0.0

Published

High-performance Hierarchical State Machine implementation optimized for JavaScript and Espruino

Readme

@stateforward/hsm

Hierarchical state machine runtime for JavaScript with bundled TypeScript declarations.

This package provides:

  • a JavaScript runtime in src/hsm.js
  • declaration files in src/hsm.d.ts
  • a builder-style DSL for defining models
  • runtime helpers for events, attributes, operations, timers, history, and snapshots

It exports both the existing camelCase helpers and PascalCase aliases.

Install

npm install @stateforward/hsm

Import

ES modules:

import * as hsm from "@stateforward/hsm";

CommonJS:

const hsm = require("@stateforward/hsm");

There is no default export.

Example

import * as hsm from "@stateforward/hsm";

class Door extends hsm.Instance {}

const Open = hsm.event("open");
const Close = hsm.event("close");

const model = hsm.define(
  "Door",
  hsm.state(
    "closed",
    hsm.transition(
      hsm.on(Open),
      hsm.target("../open"),
    ),
  ),
  hsm.state(
    "open",
    hsm.entry((ctx, instance, event) => {
      void ctx;
      void instance;
      console.log(`entered ${event.name}`);
    }),
    hsm.transition(
      hsm.on(Close),
      hsm.target("../closed"),
    ),
  ),
  hsm.initial(hsm.target("closed")),
);

const instance = new Door();
const machine = hsm.start(instance, model);

machine.dispatch(Open);
console.log(machine.state()); // "/Door/open"
machine.dispatch(Close);
console.log(machine.state()); // "/Door/closed"

Core API

Model building:

  • define(name, ...partials)
  • state(name, ...partials)
  • initial(...partials)
  • transition(...partials)
  • final(name)
  • choice(name, ...partials)
  • shallowHistory(name, ...partials)
  • deepHistory(name, ...partials)

Transition partials:

  • on(eventOrName)
  • onSet(name)
  • onCall(name)
  • when(nameOrExpression)
  • source(path)
  • target(path)
  • guard(expressionOrOperationName)
  • effect(...operations)
  • after(duration)
  • every(duration)
  • at(timepoint)

State partials:

  • entry(...operations)
  • exit(...operations)
  • activity(...operations)
  • defer(...eventNames)

Runtime helpers:

  • start(...)
  • stop(instance)
  • restart(instance, data?)
  • get(...)
  • set(...)
  • call(...)
  • takeSnapshot(...)
  • dispatchAll(ctx, event)
  • dispatchTo(ctx, event, ...ids)
  • makeGroup(...instances)

Paths

target(...) and source(...) accept relative and absolute paths.

Examples:

  • "." current state
  • "../sibling" sibling state
  • "nested/child" nested state
  • "/Machine/absolute/path" absolute path

Events, Attributes, and Operations

Events:

const Tick = hsm.event("tick");

Attributes:

const model = hsm.define(
  "Counter",
  hsm.attribute("count", 0),
  hsm.state(
    "idle",
    hsm.transition(hsm.onSet("count"), hsm.target(".")),
  ),
  hsm.initial(hsm.target("idle")),
);

Operations:

const model = hsm.define(
  "Worker",
  hsm.operation("save", (ctx, instance, value) => {
    void ctx;
    void instance;
    return value;
  }),
  hsm.state(
    "idle",
    hsm.transition(hsm.onCall("save"), hsm.target(".")),
  ),
  hsm.initial(hsm.target("idle")),
);

TypeScript

The package includes .d.ts files and can be used directly from TypeScript:

import * as hsm from "@stateforward/hsm";

class Counter extends hsm.Instance {}

const model = hsm.define(
  "Counter",
  hsm.attribute("count", 0),
  hsm.state("idle"),
  hsm.initial(hsm.target("idle")),
);

const machine = hsm.start(new Counter(), model);
const count = machine.get("count");

Runtime Notes

  • start(instance, model) returns the machine controller
  • instance._hsm points at the active machine after start
  • state() returns the fully qualified active state path
  • takeSnapshot() returns the current state, attributes, queue length, and registered event metadata
  • PascalCase aliases such as Define, State, Transition, and Event are exported alongside camelCase names

Development

npm install
npm test