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

lite-states

v1.0.2

Published

Zero-dependency finite state machine with strict transitions, lifecycle hooks, and emergency force() escape hatch.

Downloads

188

Readme

lite-states

npm version npm bundle size npm downloads npm total downloads TypeScript Zero Dependencies License: MIT

A zero-dependency finite state machine with strict transition validation, lifecycle hooks, and an emergency escape hatch.

Built for games, UI flows, and any system where state transitions must be predictable and auditable.

Features

  • Strict transitions — only allowed state changes succeed, everything else is blocked with a console warning
  • Lifecycle hooksonEnter, onLeave, onChange with disposer-based cleanup
  • Emergency force() — bypass rules for debug menus, level skips, or error recovery
  • State queriesis(), isAnyOf(), can()
  • Disposer pattern — every hook returns a cleanup function
  • Clean teardowndestroy() clears hooks and freezes the machine
  • Zero dependencies, < 1 KB

Installation

npm install lite-states

Quick Start

import { FSM } from 'lite-states';

const fsm = new FSM('idle', {
    idle:     ['loading'],
    loading:  ['ready', 'error'],
    ready:    ['playing'],
    playing:  ['complete', 'error'],
    complete: ['idle'],
    error:    ['idle'],
});

fsm.set('loading');  // true — valid transition
fsm.set('playing');  // false — blocked (loading → playing not allowed)
fsm.set('ready');    // true
fsm.set('playing');  // true

Defining Transitions

The transition map is a plain object where each key is a state and its value is an array of states it can transition to:

const transitions = {
    idle:     ['loading'],           // idle can only go to loading
    loading:  ['ready', 'error'],    // loading can go to ready OR error
    ready:    ['playing'],           // ready can only go to playing
    playing:  ['complete', 'error'], // playing can go to complete OR error
    complete: ['idle'],              // complete loops back to idle
    error:    ['idle'],              // error recovers to idle
};

Lifecycle Hooks

All hooks return a disposer function for cleanup:

// Fires when entering 'playing' — receives the previous state
const dispose1 = fsm.onEnter('playing', (prevState) => {
    console.log(`Started playing from ${prevState}`);
    startGameLoop();
});

// Fires when leaving 'playing' — receives the next state
const dispose2 = fsm.onLeave('playing', (nextState) => {
    console.log(`Left playing, going to ${nextState}`);
    stopGameLoop();
});

// Fires on EVERY state change — great for logging/analytics
const dispose3 = fsm.onChange((prev, next) => {
    analytics.track('state_change', { from: prev, to: next });
});

// Clean up when done
dispose1();
dispose2();
dispose3();

Hook firing order: onLeaveonEnteronChange

API

Constructor

const fsm = new FSM(initialState, transitions);

State Transitions

| Method | Returns | Description | |--------|---------|-------------| | .set(state) | boolean | Validated transition. Returns true if successful or same-state. | | .force(state) | void | Bypass rules. Fires hooks. Logs a warning. | | .can(state) | boolean | Check if transition is allowed without executing it. |

Queries

| Method | Returns | Description | |--------|---------|-------------| | .is(state) | boolean | Check if current state equals state. | | .isAnyOf(...states) | boolean | Check if current state is in the list. | | .current | string | Current state (getter). |

Hooks

| Method | Returns | Description | |--------|---------|-------------| | .onEnter(state, fn) | Disposer | Called when entering state. fn(prevState). | | .onLeave(state, fn) | Disposer | Called when leaving state. fn(nextState). | | .onChange(fn) | Disposer | Called on every change. fn(prev, next). |

Lifecycle

| Method | Description | |--------|-------------| | .destroy() | Clear all hooks, freeze the FSM. Idempotent. |

Emergency Escape Hatch

Sometimes you need to break the rules — debug menus, level skips, error recovery:

fsm.force('idle'); // FSM Forced: playing -> idle

force() fires all lifecycle hooks normally but skips transition validation. It logs a warning so you can trace unexpected state jumps in production.

TypeScript

import { FSM, type TransitionMap, type Disposer } from 'lite-states';

const transitions: TransitionMap = {
    idle: ['loading'],
    loading: ['ready', 'error'],
};

const fsm = new FSM('idle', transitions);
const dispose: Disposer = fsm.onChange((prev, next) => {
    // fully typed
});

License

MIT