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

@thomasrandolph/fsm

v0.0.6

Published

### Start quick

Downloads

10

Readme

fsm

Start quick

import { Machine } from "@thomasrandolph/fsm";

var fsm = new Machine( {
	"initial": "idle",
	"states": {
		"idle": {
			"on": {
				"LOAD": "pending"
			}
		},
		"pending": {
			"on": {
				"LOADED": "idle"
			}
		}
	}
} );

fsm.subscribe( ( state ) => {
	console.log( state );
} );

fsm.send( "LOAD" );
console.log( fsm.state );

fsm.send( "LOADED" );
console.log( fsm.state );

/*
Logs:

- "pending"
- "pending"
- "idle"
- "idle"
*/

Context

The state machine can hold data for you so that your state can be associated with arbitrary values.

var fsm = new Machine( {
	"initial": "idle",
	"states": {
		"idle": {}
	},
	"context": {
		"clicks": 0
	}
} );

console.log( fsm.context ); // Logs `{ "clicks": 0 }`

Note that the context getter returns a frozen structuredClone of the internal context object. It is - effectively - a read-only snapshot. structuredClones also cannot contain non-serializable values (like a Function). Including these types of values in your context will result in an Error being thrown when accessing the context getter.

Entry & Exit Actions

You can provide an enter and exit handler for any of your finite states.

The enter and exit action handlers are called with the current state and the current context as their arguments. enter is triggered after switching to the new state, and exit is triggered before leaving the current state.

var fsm = new Machine( {
	"initial": "idle",
	"states": {
		"idle": {
			"on": {
				"LOAD": "pending"
			},
			"enter": ( state, context ) => ({ ...context, "requests": context.requests - 1 }),
			"exit": ( state, context ) => ({ ...context, "clicks": context.clicks + 1 })
		},
		"pending": {
			"on": {
				"LOADED": "idle"
			},
			"enter": ( state, context ) => ({ ...context, "requests": context.requests + 1 })
		}
	},
	"context": {
		"clicks": 0,
		"requests": 0
	}
} );

console.log( fsm.context );
fsm.send( "LOAD" );
console.log( fsm.context );
fsm.send( "LOADED" );
console.log( fsm.context );

/*
Logs:

- { "clicks": 0, "requests": 0 }
- { "clicks": 1, "requests": 1 }
- { "clicks": 1, "requests": 0 }
*/

If the enter or exit action handlers modify the context, it must be returned from the action to be saved on the machine. If a falsey value is returned, the original context is restored to the machine.

If you have no need to modify the context of the machine (or don't use that feature), just omit the action handler return altogether.

Subscribing

You can subscribe to state changes from the machine with .subscribe( callback ).

callback is called after any enter action and is provided the current state and context as its arguments.

.subscribe returns a function that unsubscribes from the machine.

var fsm = new Machine( {
	"initial": "idle",
	"states": {
		"idle": {
			"on": {
				"LOAD": "pending"
			}
		},
		"pending": {
			"on": {
				"LOADED": "idle"
			}
		}
	}
} );
var unsubscribe = fsm.subscribe( ( state, context ) => {
	console.log( state, context );
} );

fsm.send( "LOAD" );
// Logs: "pending" {}

unsubscribe();

fsm.send( "LOADED" );
// No output