New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

phaze

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

phaze

Finite state machine for Node robotics

  • 0.0.71
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-37.5%
Maintainers
1
Weekly downloads
 
Created
Source

npm travis

phaze

Phaze is a finite state machine for Node robotics. It is intended to help manage the transitions from one state to the next in a deterministic manner.

Concepts

Some basics to consider when using a state machine:

  • State - a discrete, non-transitory situation that a system may be in at any given moment, e.g.:
    • idling
    • moving forward
    • reversing
  • Transition - the process of moving from one state to another

A stimulus is required to initiate a transition - this can be an event or an explicit call to change state.

Design

States

  • Phaze is designed to allow an optional callback function to be triggered once a state has been entered.
  • Once this function has completed, an output event is fired, which will initiate a transition change.

Transitions

  • A transition moves the system from the current state to the next state. A transition is triggered by the output event of a previous state.
  • A transition has a many-to-one relationship between states, i.e.: a transition can have multiple "from" states but ONLY one "to" state.

Persistence

  • Persistent storage of state is achieved through passing save and get functions into the initialise function of the state machine.
  • These functions are used to save or retrieve state using injected logic.

Nesting

  • A state machine can be nested within the state of an outer state machine.
  • Construction and initialisation of the inner state machine is done within the "do" function of a state. ​

Usage

/***************
STATES
***************/
var startState = {
  do: function () {
   },
   outputEvent: 'startEvent'
 };

var walkingState = {
  do: function (callback) {
    callback(null, 'Walking...');
  },
  outputEvent: 'walkEvent'
};

// no outputEvent will cause the state machine to exit the loop
var runningState = {
  do: function (callback) {
    callback(null, 'Running...');
  }
};

/***************
TRANSITIONS
***************/
var transition1 = {
  eventId: 'startEvent',
  from: ['startState'],
  to: 'walkingState'
};

var transition2 = {
  eventId: 'walkEvent',
  from: ['walkingState'],
  to: 'runningState'
};

/***************
STATE PERSISTENCE FUNCTIONS (these can wrap database calls etc.)
***************/
var getStateFunc = function (callback) {
  callback(null, 'startState');
};

var saveStateFunc = function (state, callback) {
  callback();
};

/***
STATE MACHINE
***/
var stateMachine = new StateMachine();

// initialise - first argument is the number of times the machine will loop (0 = infinite)
stateMachine.initialise(1, getStateFunc, saveStateFunc, function (err) {
  if (err)
    return done(err);

  stateMachine.addState('startState', startState);
  stateMachine.addState('walkingState', walkingState);
  stateMachine.addState('runningState', runningState);
  stateMachine.addTransition(transition1);
  stateMachine.addTransition(transition2);

  // start the machine and confirm the final result
  stateMachine.start('startEvent', function (err, result) {
    if (err)
      return done(err);

    expect(result).to.equal('Running...');
    done();
  })
});

FAQs

Package last updated on 04 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc