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

@trasukg/state-machine

v1.0.1

Published

A fairly simple JavaScript finite state machine utility.

Downloads

17

Readme

@trasukg/state-machine

Description

This is a fairly simple finite state machine utility. It supports transitions from state to state, functions executed on transitions, and entry and exit functions.

Usage

var StateMachine=require('@trasukg/state-machine');  
var states={
  A: {
    go: 'B',
    takeArgs: ['A', function(val) {
      calledValue=val;
    }],
    stay: function() {
      stayWasCalled=true;
    },
    value: ['B', function() { return 1; }]
  },
  B: {
    go: ['A', function() { trB(); }],
    value: ['A', function() { return 2; }],
    onEntry: function() {
      entryCalled=true;
    },
    onExit: function() {
      exitCalled=true;
    }  
  }  
};

var machine=new StateMachine(states,'A');  
machine.go();
var c=machine.value();
machine.takeArgs(12);
  • First, import the state machine class:
  • Then, create a descriptor for the states. This is a JavaScript object that contains a key/value pair for each state. The key is the name of the state, and the value an object that defines the events the state responds to.
  • Instantiate a state machine, by calling the StateMachine constructor, with the state descriptor, and the name of the initial state of the machine:
  • Now, you can call events on the state machine.

What happens when you call the event depends on the state definitions. To see the possibilities, lets' take a look at the definition for State 'A' above:

A: {
  go: 'B',
  takeArgs: ['A', function(val) {
    calledValue=val;
  }],
  stay: function() {
    stayWasCalled=true;
  },
  value: ['B', function() { return 1; }]
},

You can probably figure out that in this object, the key is the name of the event function that we can call. The value defines what happens when we call that event function while in that state.

  • A simple string value, like the value of 'go' above, indicates that the machine will transition to the named state. So if we're in state 'A' and we call 'machine.go()', the machine transitions to state 'B'.
  • If we want to execute a function on transition, then we provide an array that contains the next state and the function, as in the 'takeArgs' event above. The function will be executed and the machine will transition to the named state. The function can take arguments, so for example in the case above, we could call 'machine.takeArgs(12)', and the arguments are passed on to the transition function. The return value from the transition function is returned to the caller.
  • If we want to execute a function, but then remain in the same state, we can just provide the function, as in the 'stay' event shown above.

In addition to the transition functions, we can also provide entry and exit functions for a state. Simply define events named 'onEntry' and/or 'onExit', and these functions will be executed when the machine enters or exits the state. The entry and exit functions are called without arguments.

B: {
  go: ['A', function() { trB(); }],
  value: ['A', function() { return 2; }],
  onEntry: function() {
    entryCalled=true;
  },
  onExit: function() {
    exitCalled=true;
  }
}

The transition functions and entry/exit functions are executed in the context of the state machine object (i.e. this==machine).

For more demonstrations of the usage, have a look at 'specs/StateMachineSpec.js'.

License

Apache Software License 2.0