Socket
Socket
Sign inDemoInstall

synchronous-fsm

Package Overview
Dependencies
2
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    synchronous-fsm

A synchronous FSM that triggers events when entering or leaving a state


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Synchronous finite state machine

A synchronous FSM that also triggers events when entering or leaving a state.

Installation

npm install synchronous-fsm

How to use

Require synchronous-fsm:

var FSM = require("synchronous-fsm");

To initialize the statemachine we need to define states and transitions first:

var states = {
  // It has an 'opened' state
  "opened": [
      // That accepts a 'pass' event that will execute the 'pass' action
      ["pass", function onPass(event) {
          passCalled = event;
      // And when done, it will transit to the 'closed' state
      }, "closed"],

      // It also has a 'action' event that doesn't trigger a transition
      ["coin", function onCoin(event) {
          console.log("thanks");
      }]
  ],

  // It also has a 'closed' state
  "closed": [
      // That accepts a 'coin' event that will execute the 'coin' action
      ["coin", function coin(event) {
          coinCalled = event;
      // And when done, it will transit back to the 'opened' state
      }, "opened"]
  ]
};

Then we can initialize the state machine in the desired state:

// Initialize in the "opened" state
var fsm = new FSM("opened", states);

Trigger a transition:

// Will transit to "closed", executing the onPass handler
fsm.event("pass");

// Could also transit with some data that will be received by the handler
fsm.event("pass", "6km/h", "8:30pm");

Get the current state:

fsm.getCurrent(); // opened

Can also be directly advanced to a given state:

fsm.advance("closed"); // will move the state machine to this state

The stateMachine also triggers an "entry" and an "exit" event when entering and leaving a state. Simply add the handlers like for any other event:

var states = {
  // It has an 'opened' state
  "opened": [
      ...

      // This handler will be called when entering the state
      ["entry", function onEntry(event) {
          console.log("entering state...");
      }],

      // This handler will be called when leaving the state
      ["exit", function onExit(event) {
          console.log("leaving state...");
      }],
  ],

  // It also has a 'closed' state
  "closed": [
      ...
  ]
};

LICENSE

MIT

Keywords

FAQs

Last updated on 13 Apr 2014

Did you know?

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc