Socket
Socket
Sign inDemoInstall

statemachinejs

Package Overview
Dependencies
137
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    statemachinejs

Implementation of State Machine Pattern in JavaScript.


Version published
Maintainers
1
Install size
29.5 MB
Created

Readme

Source

StateMachineJS

Implementation of State Machine Pattern in JavaScript.

Get Started

  1. Define states in host object

    var app = {                     // host object for state machine
        states: [{                  // states definition
            name: 'STATE_A'         // state name
            entry: 'stateAEntry'    // state entry function
            exit: 'stateAExit'      // state exit function
            transitions: [{         // transitions definition
                trigger: 'event1'   // transition trigger
                dest: 'STATE_B'     // transition destination
                action: 'action1'   // transition action function
                guard: 'guard1'     // transition guard function
            }]
        }, {
            name: 'STATE_B'         // minimal state definition
        }],
    
        // host object is used as the context (`this`) of all state functions
    
        stateAEntry: function() {
            // this function will be called when entering STATE_A
        },
    
        stateAExit: function() {
            // this function will be called when leaving STATE_A
        },
    
        action1: function() {
            // this function will be called when "event1" transition is taking place
        },
    
        guard1: function() {
            // this function returns `false` to prevent "event1" transition from
            // happening
        }
    };
    
  2. Initialize host object

    StateMachine.init(app);
    
  3. Check current state

    app.getCurrentStateName();          // => 'STATE_A', app is now in the first state
    
  4. Trigger state transition

    app.handleStateTrigger('event1')
    
  5. Check current state again

    app.getCurrentStateName();          // => 'STATE_B', app is now in the STATE_B state
    

API Document

TODO

Keywords

FAQs

Last updated on 10 Aug 2013

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc