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

machinarium

v0.0.4

Published

Fluent, framework-agnostic, and type-safe state machine library, that aims for simplicity and great developer experience

Readme

Machinarium

Fluent, framework-agnostic, and type-safe state machine library, that aims for simplicity and great developer experience

Usage

To create a state machine using Machinarium, use the createMachine function. This function accepts generic type parameters for the possible states and the events, and returns a chainable builder object that allows you to define the state machine.

Basic Usage

Let's start by building a state machine for a light bulb that can be either on or off, based on this simple state diagram:

Simple light bulb diagram

import { createMachine } from 'machinarium';

type State = 'on' | 'off';
type Event = 'turn-on' | 'turn-off';

const bulbStateMachine = createMachine<State, Event>({
  initialState: 'off',
})
  .when('off', (b) => {
    b.on('turn-on').transitionTo('on');
  })

  .when('on', (b) => {
    b.on('turn-off').transitionTo('off');
  });

For each state, we need to define which events it'll respond to and what state it should transition to when that event is received.

To do that, we use the when method that accepts the state name and a callback. The callback receives a builder object that lets you build the transitions for that state using the on and transitionTo methods.

Then, to actually transition between the states in the machine, use the send method and pass the event name to it:

bulbStateMachine.send('turn-on');

console.log(bulbStateMachine.getState()); // 'on'

Multi-State Transitions

You can also define an array of states in the when method to easily support multiple states with the same transitions:

type State = 'on' | 'off' | 'broken';
type Event = 'turn-on' | 'turn-off' | 'break';

const bulbStateMachine = createMachine<State, Event>({
  initialState: 'off',
})
  .when('off', (b) => {
    b.on('turn-on').transitionTo('on');
  })

  .when('on', (b) => {
    b.on('turn-off').transitionTo('off');
  })

  .when(['off', 'on'], (b) => {
    b.on('break').transitionTo('broken');
  });

In addition, you can pass a function to the transitionTo method to dynamically determine the next state based on the previous state:

type State = 'on' | 'off';
type Event = 'toggle';

const bulbStateMachine = createMachine<State, Event>({
  initialState: 'off',
}).when(['on', 'off'], (b) => {
  b.on('toggle').transitionTo((prev) => {
    return prev === 'on' ? 'off' : 'on';
  });
});

Checking for Transitions

You might also want to check whether a transition can be performed on the current state. To do that, use the can method and pass the event you want to check:

type State = 'on' | 'off';
type Event = 'turn-on' | 'turn-off';

const bulbStateMachine = createMachine<State, Event>({
  initialState: 'off',
})
  .when('off', (b) => {
    b.on('turn-on').transitionTo('on');
  })
  .when('on', (b) => {
    b.on('turn-off').transitionTo('off');
  });

console.log(bulbStateMachine.can('turn-on')); // true
console.log(bulbStateMachine.can('turn-off')); // false

Listening for Changes

To listen for state transitions, use the subscribe method:

const unsubscribe = bulbStateMachine.subscribe(() => {
  console.log('New state:', bulbStateMachine.getState());
});

bulbStateMachine.send('turn-on'); // Logs 'New state: on'

unsubscribe();

Usage with React

To use Machinarium with React, use the useMachine hook from machinarium/react. This hook will provide you with the current state and a send function to transition between states:

import { createMachine } from 'machinarium';
import { useMachine } from 'machinarium/react';

type State = 'on' | 'off';
type Event = 'turn-on' | 'turn-off';

const bulbStateMachine = createMachine<State, Event>({
  initialState: 'off',
})
  .when('off', (b) => {
    b.on('turn-on').transitionTo('on');
  })
  .when('on', (b) => {
    b.on('turn-off').transitionTo('off');
  });

function Bulb() {
  const { state, send } = useMachine(bulbStateMachine);

  return (
    <div>
      <p>The bulb is {state}</p>
      <button onClick={() => send('turn-on')}>Turn on</button>
      <button onClick={() => send('turn-off')}>Turn off</button>
    </div>
  );
}

Each time the machine's state will change (either by using the send function from the hook, or by using the send method from the machine itself), the component will re-render with the new state.

You can also determine reactively whether a transition can be performed using the canTransition function from the hook:

function Bulb() {
  const { state, send, canTransition } = useMachine(bulbStateMachine);

  return (
    <div>
      <p>The bulb is {state}</p>
      <button
        disabled={!canTransition('turn-on')}
        onClick={() => send('turn-on')}
      >
        Turn on
      </button>

      <button
        disabled={!canTransition('turn-off')}
        onClick={() => send('turn-off')}
      >
        Turn off
      </button>
    </div>
  );
}