0.0.5 • Published 8 years ago

sicoris-statemachine-js v0.0.5

Weekly downloads
3
License
BSD-3-Clause
Repository
bitbucket
Last release
8 years ago

sicoris-statemachine-js

Simplified state machine library for JS, following most of the principles of https://bitbucket.org/xferro/sicoris-statemachine but with just non-reentrant strategy.

It is a very lightweight but powerful library. A State Machine consists of the following:

  • A set of states. One and only one is the start state. We can have multiple final states
  • A set of events that can be processed by the state machine
  • A set of transition. A transition is a tuple of: source state, event and target state.
  • Each transition has three phases
    • Exit or leave phase: when the transition starts, it's the only phase where we can cancel the transition
    • Transition phase: where we do all the work
    • Enter phase: sometimes, we want to force the transition to move to another state. Useful for condition states
  • If we try to process an event, and the transition has not been defined, it will throw an exception.

build status

Installation

This module is installed via npm:

$ npm install sicoris-statemachine-js

Example Usage with some conditions

      var stateMachine = require('sicoris-statemachine-js');
git
      var cfg = { // State machine with 2 states: STATE_A and STATE_B
          STATE_A : {
              startState: true, // When the SM is created, it will be initialized to STATE_A
              transitions: {
                  EVENT_AB : "STATE_B" // When in STATE_A, we can trigger a transition by processing EVENT_AB
              }
          },
          STATE_B : {
              endState: true // STATE_B is a final state
          }
      };

      var def = stateMachine(cfg);
      var processor = def.processorBuilder()
          // We can register a listener when exiting a state
          .onExit("STATE_A", function(transition) {
              console.log("leaving state " + transition.source 
                          + " to state " + transition.target 
                          + " because of event " + transition.event);
              return true;  // Returning true allows the transition to continue
          })
          .onEnter("STATE_B", function(transition) {
              console.log("entering " + transition.target 
                          + " from state " + transition.source 
                          + " because of event " + transition.event);
          })
          .onTransition("STATE_A", "EVENT_AB", function(transition) {
              console.log("transitioning from state " + transition.source 
                          + " to state " + transition.target 
                          + " because of event " + transition.event);
        })
        .build();

    console.log(processor.getCurrentState());
    processor.process(EVENT_AB);
    console.log(processor.getCurrentState());
0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago