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

async-state-machine

v1.2.3

Published

An Asynchronous State Machine Implementation written in Javascript

Downloads

20

Readme

Async Js State Machine

This is a package that uses the state machine pattern in an asynchronous manner.

This allows us to do some cool things in our code and extends the capabilities of state machines to not have to rely on synchronous steps.

Basic Overview of available Objects

State

State's are your big

To set new states-

const OffState = Object.create(State)
  .setName('off')
  .onExit(() => console.log('Leaving off State'))
  .onEntry(() => console.log('Switching Off'))
  .onSuccess(() => console.log('State is now off'));

const OnState = Object.create(State).setName('on');

The lifecycles methods onEntry, onExit, and onSuccess are all optional.

Available Methods with State Object

| name | params | returns | description | | ------------ | ------------------------- | ------- | ----------------------------------------------- | | setName | string | self | Set name of current state object | | setOnEntry | function (async optional) | self | Set function to be fired when state is entered | | setOnExit | function (async optional) | self | Set function to be fired when state is left | | setOnSuccess | function (async optional) | self | Set function to be fired when state has changed |

Transition

A transition is a simple object which only contains it's name and conditions

If we wanted to create a transition which only fires when the sky is blue we can say

const switch_off = Object.create(Transition)
  .setName('switch_off')
  .addCondition((context, params) => params.heat === 100);

but typically your transition might be as simple as

const switch_on = Object.create(Transition).setName('switch_on');

I've opted to prefer to use Object.create(Transition) for the more simple objects versus the class syntax new Transition() as I don't believe a Transition needs to be a class.

This is a personaly preferance and if there is a case made for Transition's being classes we can go that route.

Available Methods with Transition Object

| name | params | returns | description | | ------------ | ------------------------ | ------- | ---------------------------------------- | | setName | string | self | Set the name for this state | | addCondition | condition: () => boolean | self | Adds a new condition for this transition |

Edge

An edge is defined as:

Edge.new()
  .from([LIST, OF, FROM, STATES])
  .to(SINGLE_TO_STATE)
  .with(name_of_transition);

Edges are used to define your state machine steps, where each step is a new edge- for example given a simple state graph:

OFF_STATE (switch_on)-> ON_STATE (switch_off)-> OFF_STATE

We can define this with the two edges being:

const OFF_TO_ON = Edge.new().from([OFF_STATE]).to(ON_STATE).with(switch_on)
const ON_TO_OFF = Edge.new().from([ON_STATE]).to(OFF_STATE).with(switch_off)

Available Methods with Edge Object

| name | params | returns | description | | ---------- | ------------------------------ | --------------- | -------------------------------------- | | new | - | new Edge object | Return a new Edge Object | | from | fromStates: Array | self | Set from states | | to | toState: StateObject | self | Set to state | | transition | transition: StateObject | self | Set transition applicable to this edge | | with | transition: StateObject | self | Set transition applicable to this edge |

Machine

From the Machine class we can add our edges

const MyMachine = new Machine();
MyMachine.registerEdge(OFF_TO_ON)
  .registerEdge(ON_TO_OFF)
  .setInitialState(OFF);

To then trigger a transition we simple use

MyMachine.triggerTransition(switch_off);

Available Methods with Machine Class

| name | params | returns | description | | ----------------- | -------------------------------------------------------------- | ------------- | -------------------------------------------------------------------------------------- | | getTransitions | none | Array | Returns available transitions from this state | | getState | none | string | Returns current state | | setDataGetter | function | self | passed in function is bound to context of Machine and is called whene data is returned | | setInitialState | state: StateObject, params: object, delayMachineStart: boolean | self | set's state which machine should start at, optionally do not start machine here | | triggerTransition | transition: TransitionObject | Promise | Transitions to next state | | registerEdge | edge: EdgeObject | self | Register a new Edge onto the Machine | | carry | none | self | Useful when dot chaining | | start | params?: Object | Promise | Transitions to initial state |

Usage with Redux

import Machine from 'async-state-machine';
import redux from '../store';

class ReduxMachine extends Machine {
  reduxEnabled: boolean;
  machineKey: string;

  constructor(reduxEntry: string | void, enableLog: boolean = false) {
    super(enableLog);
    this.reduxEnabled = false;
    if (reduxEntry !== null) {
      // add entry as soon as class is created
      this.redux(reduxEntry);
    }
  }

  redux(name: string) {
    // add entry as soon as class is created
    this.machineKey = name;
    redux.dispatch({
      type: 'REGISTER_REDUX_MACHINE',
      payload: { machine: this.machineKey, newState: '' },
    });
    this.reduxEnabled = true;
  }

  storeRedux(newState) {
    if (this.reduxEnabled) {
      redux.dispatch({
        type: 'UPDATE_REDUX_MACHINE',
        payload: { machine: this.machineKey, newState },
      });
    }
  }

  setState(nextState: string) {
    super.setState(nextState);
    this.storeRedux(nextState.toString());
  }
}

export default ReduxMachine;