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

jcfsm

v2.0.0

Published

Compact implementation of a finite state machine

Readme

jCFSM

A compact finite state machine for JavaScript and TypeScript, with async callback support and zero runtime dependencies.

Features

  • Explicit transitions — only registered transitions succeed; all others return false
  • Four callback hooksOnBefore, OnLeave, OnAfter, OnEnter — sync or async
  • Guard supportOnBefore callbacks abort a transition by returning false
  • Re-entrant safeStateSet calls nested inside a callback return false without side effects
  • Zero dependencies — no runtime dependencies
  • TypeScript — full type definitions included

Installation

npm / bundler:

npm install jcfsm

Browser (CDN):

<script type="module">
	import jCFSM from 'https://unpkg.com/[email protected]/jcfsm.min.js';

	const fsm = new jCFSM( 'idle' );
	fsm.StateAdd( 'running' );
	fsm.TransitionAdd( 'idle', 'running' );

	await fsm.StateSet( 'running' );
	console.log( fsm.StateGet() ); // 'running'
</script>

Quick Start

import jCFSM from 'jcfsm';

const fsm = new jCFSM( 'idle' );

fsm.StateAdd( 'running' );
fsm.StateAdd( 'stopped' );

fsm.TransitionAdd( 'idle', 'running' );
fsm.TransitionAdd( 'running', 'stopped' );

fsm.StateOnEnterAdd( 'running', ( current, prev ) => {
	console.log( `Entered ${current} from ${prev}` );
} );

fsm.TransitionOnBeforeAdd( 'running', 'stopped', async () => {
	const allowed = await checkIfStopIsAllowed();
	return allowed;
} );

await fsm.StateSet( 'running' ); // true
await fsm.StateSet( 'idle' );    // false — no transition defined
await fsm.StateSet( 'stopped' ); // true or false — depends on the guard

Callback Execution Order

When StateSet succeeds, callbacks fire in this order:

OnBefore → OnLeave → OnAfter → OnEnter

If any OnBefore callback returns false, the transition aborts immediately. OnLeave, OnAfter, and OnEnter do not fire.

API

Constructor

new jCFSM( initialState: string )

Creates a new FSM. The initial state is registered automatically.


State management

| Method | Returns | Description | |---|---|---| | StateAdd( state ) | boolean | Registers a new state. Returns false if the state already exists. | | StateDel( state ) | boolean | Removes a state and all its associated transitions. Returns false if the state does not exist. | | StateGet() | string | Returns the current state. | | StateSet( state ) | Promise<boolean> | Triggers a transition to state. Returns false if the transition is not defined, if a guard aborts it, or if another transition is already in progress. |


State callbacks

These callbacks fire whenever the machine enters or leaves a specific state, regardless of which transition triggered the change.

Signatures:

type FunctionOnEnter = ( currentState: string, prevState: string ) => void | Promise<void>;
type FunctionOnLeave = ( currentState: string, nextState: string ) => void | Promise<void>;

| Method | Description | |---|---| | StateOnEnterAdd( state, func ) | Registers a callback that fires after entering state. Returns false if the state does not exist or the callback is already registered. | | StateOnEnterDel( state, func ) | Removes an enter callback. Returns false if the callback is not found. | | StateOnLeaveAdd( state, func ) | Registers a callback that fires before leaving state. Returns false if the state does not exist or the callback is already registered. | | StateOnLeaveDel( state, func ) | Removes a leave callback. Returns false if the callback is not found. |


Transition management

Transitions define which state changes are allowed. StateSet fails unless the corresponding transition is registered.

| Method | Returns | Description | |---|---|---| | TransitionAdd( from, to ) | boolean | Registers the transition from from to to. Returns false if either state does not exist or the transition already exists. | | TransitionDel( from, to ) | boolean | Removes the transition. Returns false if the transition does not exist. |


Transition callbacks

These callbacks fire only for a specific from → to pair.

Signatures:

type FunctionOnTransitionBefore = () => boolean | Promise<boolean>;
type FunctionOnTransitionAfter  = () => void    | Promise<void>;

| Method | Description | |---|---| | TransitionOnBeforeAdd( from, to, func ) | Registers a guard that fires before the transition. Return false to abort. Returns false if the transition does not exist. | | TransitionOnBeforeDel( from, to, func ) | Removes a before callback. Returns false if the callback is not found. | | TransitionOnAfterAdd( from, to, func ) | Registers a callback that fires after the transition completes. Returns false if the transition does not exist. | | TransitionOnAfterDel( from, to, func ) | Removes an after callback. Returns false if the callback is not found. |


License

See LICENSE.md.