Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
/ fsm.js Public archive

JSSM: A lightweight JavaScript finite state machine library. Define states, transitions, and actions with a clean API. Chain transitions, handle multiple source states, and customize behavior with minimal overhead.

Notifications You must be signed in to change notification settings

sanyabeast/fsm.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

JSSM

A lightweight JavaScript finite state machine library.

License: MIT

Overview

JSSM is a simple yet powerful finite state machine implementation for JavaScript. It allows you to define states, transitions between them, and actions to execute during transitions.

Installation

Direct include

<script src="path/to/jssm.min.js"></script>

AMD

define(['path/to/JSSM'], function(JSSM) {
  // Your code here
});

Basic Usage

// Create a new state machine
const turnstile = new JSSM({
  transitions: [
    // Initial state transition (automatically invoked)
    { name: 'init', from: 'none', to: 'locked' },
    
    // Regular transitions
    { name: 'insertCoin', from: 'locked', to: 'unlocked' },
    { name: 'push', from: 'unlocked', to: 'locked' }
  ],
  
  // Actions to execute during transitions
  actions: {
    init: function() {
      console.log('Initialized to locked state');
    },
    insertCoin: function(info, coin) {
      console.log(`Coin inserted: ${coin}`);
    },
    push: function(info) {
      console.log('Turnstile pushed');
    }
  },
  
  // Optional context for actions
  context: null
});

Triggering Transitions

// Trigger transitions
turnstile.insertCoin(25); // Passes 25 as the coin value
turnstile.push();

// Chain transitions
turnstile.insertCoin(25).push().insertCoin(25);

Configuration

const fsm = new JSSM({
  // Optional settings
  _settings: {
    log: true,     // Enable console logging
    strict: false, // Don't throw errors for invalid transitions
    history: false // Don't track transition history
  },
  
  transitions: [
    // Your transitions here
  ],
  
  actions: {
    // Your actions here
  }
});

State Management

// Get current state
const currentState = turnstile.current();  // or turnstile._current

// Get previous state
const previousState = turnstile._previous;

// Check if a transition is possible
if (turnstile.can('push')) {
  console.log('Can push the turnstile');
}

// Check if a transition is not possible
if (turnstile.cannot('insertCoin')) {
  console.log('Cannot insert coin in current state');
}

Transition Info

The transition info object is always passed as the first argument to action callbacks:

function insertCoin(info, coin) {
  console.log(info); 
  // Outputs: { name: 'insertCoin', from: 'locked', to: 'unlocked' }
  
  console.log(`Inserted ${coin} cents`);
}

Advanced Usage

Multiple 'from' States

// Allow transition from multiple states
{ name: 'reset', from: ['locked', 'unlocked', 'broken'], to: 'locked' }

License

MIT

About

JSSM: A lightweight JavaScript finite state machine library. Define states, transitions, and actions with a clean API. Chain transitions, handle multiple source states, and customize behavior with minimal overhead.

Topics

Resources

Stars

Watchers

Forks