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 🙏

© 2024 – Pkg Stats / Ryan Hefner

hsmkit

v1.0.14

Published

`hsmkit` is a lightweight JavaScript library alternative to [xState](https://xstate.js.org/) but designed for simplicity and ease of use.

Downloads

831

Readme

hsmkit: Hierarchical State Machine Library

hsmkit is a lightweight JavaScript library alternative to xState but designed for simplicity and ease of use.

hsmkit uses the same syntax as xState without actors, services, parallel states, and other advanced features. If you need any of these, you should consider using xState instead.

It provides a simple API to define states, transitions, actions, and more, making it ideal for developers who want to implement state machines without the extra complexity.

Features

  • Supports hierarchical state machine structures.
  • Allows actions and guards to be defined for transitions.
  • Provides an event-driven API for dispatching events.
  • Allows deep state context modification using the assign function.
  • Supports asynchronous actions within state transitions.
  • Support * wildcard events for handling unspecified events.
  • Listening to events/transitions of state machine.
  • Passing data to actions and guards using the context object.

Differences from xState

  • No support for actors, services, parallel states, and other advanced features.
  • No always transition type. Why? You can use * wildcard events instead.
  • No support for history states. Why? You can always save your states in an array.
  • No support for delayed transitions. Why? setTimeout should be enough.
  • No support for invoking other machines.

Installation

npm install hsmkit

Quick Start

Below is a quick example to create and use a state machine with hsmkit.

import { create, assign } from 'hsmkit';

// Define the state machine configuration
const config = {
  id: 'websocket',
  initial: 'disconnected',
  context: {
    socket: null,
    keepalive: false,
    keepaliveTimeout: 0
  },
  states: {
    disconnected: {
      entry: [assign({ socket: null }), 'notifyDisconnected'],
      on: {
        CONNECT: 'connecting',
        DISCONNECT: 'disconnecting'
      }
    },
    connecting: {
      entry: ['connectWebSocket'],
      on: {
        CONNECT_SUCCESS: 'connected',
        CONNECT_FAILURE: 'disconnected'
      }
    },
    connected: {
      initial: 'idle',
      entry: [assign({ socket: {} }), 'notifyConnected'],
      on: {
        DISCONNECT: 'disconnecting'
      },
      states: {
        idle: {},
        sending: {}
      }
    },
    disconnecting: {
      entry: ['disconnectWebSocket'],
      on: {
        DISCONNECT_SUCCESS: 'disconnected'
      }
    }
  }
};

// Define actions and setup
const setup = {
  actions: {
    notifyDisconnected: ({ context, machine, event }) =>
      console.log('Disconnected'),
    connectWebSocket: ({ context, machine, event }) =>
      console.log('Connecting WebSocket'),
    notifyConnected: ({ context, machine, event }) => console.log('Connected'),
    disconnectWebSocket: ({ context, machine, event }) =>
      console.log('Disconnecting WebSocket')
  }
};

// Create the state machine
const machine = create({
  config,
  setup
});

// Start the state machine
machine.start();

// Dispatch events
machine.dispatch('CONNECT');
machine.dispatch('DISCONNECT');

// Listen to events and transitions
machine.on('event', event => {
  console.log(`Event occurred: ${event.type}`);
});

machine.on('transition', (next, prev) => {
  console.log(`Transitioned from ${prev.name} to ${next.name}`);
});

License

This project is licensed under the MIT License.