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

engineering

v1.0.1

Published

Engineer lightweight finite state machines

Downloads

12

Readme

engineering Module Version MIT License Build Status Coverage Status

Engineer lightweight finite state machines.

Available in both Node.js and web browsers using Browserify.

Synopsis

Installation is easy via npm:

npm install [--save] engineering

Defining a state machine is very straightforward:

var engineer = require('engineering');

function Client() {
  // Define state machine for WebSocket state
  this.connection = engineer({
    states: {
      // connected -> disconnecting
      connected: ['disconnecting'],

      // connecting -> connected, disconnected
      connecting: ['connected', 'disconnected'],

      // disconnecting -> disconnected
      disconnecting: ['disconnected'],

      // disconnected -> connecting
      disconnected: ['connecting']
    },

    // Default state is disconnected
    default: 'disconnected'
  });
}

React to state machine transitions easily:

function Client() {
  // ...same as above

  // When connected, relay WebSocket's "message" notification
  this.connection.on('connected', function (ws) {
    var emit = this.emit.bind(this, 'message');

    ws.on('message', function (message) {
      emit(message);
    });
  }, this);
}

Effortlessly drive action based on the machine's state:

Client.prototype.connect = function (url) {
  var self = this;
  var connection = self.connection;

  return new Promise(function (resolve, reject) {
    connection
      // Connected, so resolve immediately
      .at('connected', resolve)
      // Connecting, so wait until connected or disconnected
      .at('connecting', function () {
        connection
          .when('connected', resolve)
          .otherwise(reject);
      })
      // Disconnecting, so wait until disconnected and attempt to connect again
      .at('disconnecting', function () {
        connection.once('disconnected', function () {
          self.connect(url).then(resolve, reject);
        });
      })
      // Disconnected, so attempt WebSocket connection
      .otherwise(function () {
        connection.to('connecting');

        var onError = function (err) {
          connection.to('disconnected');
          reject(err);
        };

        var ws = new WebSocket(url);
        ws.on('error', onError);
        ws.on('open', function () {
          ws.removeListener('error', onError);
          connection.to('connected', ws);
          resolve(ws);
        });
      });
  });
};

Know the machine's state using an expressive and easy-to-understand interface:

Client.prototype.send = function (message) {
  var connection = this.connection;

  connection
    .at('connected', function (ws) {
      ws.send(message);
    })
    .otherwise(function () {
      throw new Error('Client not connected');
    });
};

View the accompanying examples directory for concrete examples.

API

Creating a state machine

engineer(options) → Machine

Creates a new Machine machine with given options.

The following options can be specified:

  • statesObject: A map of state transitions. The keys are states, and the value is an array of transitions. Required.
  • defaultString: The default state. Required.

Querying present machine state

machine.is(state[, fn[, context]]) → Boolean

Indicates whether state machine is currently at state. If at state and fn was passed, the callback will be invoked immediately. state may be an individual state or an array of states.

Returns true if at state; false otherwise.

machine.at(state, fn[, context]) → Query

Begins a chainable query of the machine's current state, queuing fn for invocation if at state.

query.at(state, fn[, context]) → Query

Identical to machine.at(). Used to query additional states.

query.otherwise(fn[, context]) → Any

Performs check of machine's state. If machine is at a state with a previous query, the appropriate callback is invoked; otherwise, fn is invoked.

Returns invoked callback's return value.

Reacting to state transition

machine.on(state, fn[, context]) → Machine

If state machine is at state, invokes fn immediately with context. Additionally, every time the state machine transitions to state, fn will be invoked with context.

machine.once(state, fn) → Machine

If state machine is at state, invokes fn immediately with context; otherwise, invokes fn with context when machine transitions to state.

machine.when(state, fn[, context]) → Watch

Similar to .once(); however, if state machine is not at state and transitions to state next, fn is invoked.

watch.when(state, fn[, context]) → Watch

Identical to machine.watch(). Used to watch additional state transitions.

watch.otherwise(fn[, context]) → Machine

If state machine transitions to any state that does not have a watch, fn is invoked with context.

Transitioning machine state

machine.to(state[, ...args])

Transitions state machine to state. Any passed args will be applied to any callbacks passed to .is(), .at(), .on(), .once(), and .when().

License

MIT