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

js-statemachine

v0.0.7

Published

A Javascript class to help create powerful and robust state machines for components.

Downloads

2

Readme

js-statemachine

Why

I started using state machines after listening to the Full Stack Radio podcast with David Khourshid. I originally just used objects as enums and compared them against a variable, but ultimately I felt something was missing.

I've contributed to one of nvms' repositories vue-atlas and I stumbled across one of his other packages for PHP State Machines, php-nfa, which is where I drew a lot of inspiration from for this library.

I've made a number of key changes to simplify and streamline (in my opinion!) the process.

Usage

This library uses ES6 classes and relies heavily on inheritance. If you are using babel, you need to ensure that this package is not excluded. Many configs exclude node_modules by default.

Example uses Laravel Mix

module: {
      rules: [
        {
          test: /\.jsx?$/,
          exclude: /node_modules\/(?![js-statemachine])/,
          use: [
            {
              loader: 'babel-loader',
              options: Config.babel()
            }
          ]
        }
      ]
    }

Creating your State Engine

The examples will focus on creating a state engine for a Vue component, but this can be applied to anything you wish.

ComponentState.js

import StateMachine from 'js-statemachine';

class ComponentState extends StateMachine {
    initial() {
        return [ComponentState.IDLE];
    }
}

ComponentState.LOADING = 'loading';
ComponentState.IDLE = 'idle';
ComponentState.ERROR = 'error';
ComponentState.HAS_DATA = 'has_data';
ComponentState.EMPTY = 'empty';

export default ComponentState;

First you need to import the state machine class, then extend it. You can override the initial method to set default states for your component. This needs to be an array.

Then, below the class, define some static variables for your states. Here I have loading, idle, error, has_data and empty.

Finally, export your class.

Using your State Engine

Example is a Vue component, idea applicable to other frameworks/vanilla

import ComponentState from '../../classes/ComponentState';
export default {
    data() {
        return {
            state: null, // where our state object will be stored
            ComponentState // allows us to use the static variables in the template
        }
    },
    created() {
        this.state = new ComponentState(); // defaults to ComponentState.IDLE

        // Use the swap method to swap IDLE with LOADING
        this.state.swap(ComponentState.IDLE, ComponentState.LOADING);
        
        // Load some data
        axios.get(`/api/example`)
            .then(this.dataLoaded)
            .catch(this.loadError);
    },
    methods: {
        filesLoaded(response) {
            // Swap LOADING for IDLE and HAS_DATA
            this.state.swap(
                ComponentState.LOADING,
                [ComponentState.IDLE, ComponentState.HAS_DATA]
            );
            console.log(response);
        },
        loadError(error) {
            this.state.set(ComponentState.ERROR); // Override ALL states.
            console.error(error);
        }
    }
}

API

initial()

  • Returns: Array

  • Details:

    Returns an array of the default states. Override this if you would like to change the defaults.

is(state)

  • Arguments:

    • {String} state
  • Returns: Boolean

  • Details:

    Checks if state is active.

not(state)

  • Arguments:

    • {String} state
  • Returns: Boolean

  • Details:

    Checks if state is not active.

set(state)

  • Arguments:

    • {String|Array} state
  • Returns: void

  • Details:

    Sets the current state to state. This will remove any existing states.

swap(oldState, newState)

  • Arguments:

    • {String|Array} oldState
    • {String|Array} newState
  • Returns: void

  • Details:

    Swaps the state(s) in oldState with the state(s) in newState

remove(state)

  • Arguments:

    • {String|Array} state
  • Returns: void

  • Details:

    Removes the state(s) in state.

add(state)

  • Arguments:

    • {String|Array} state
  • Returns: void

  • Details:

    Adds the state(s) in state.