Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 28, 2025. It is now read-only.
/fsm.jsPublic 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.

NotificationsYou 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

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

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

AMD

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

Basic Usage

// Create a new state machineconstturnstile=newJSSM({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 transitionsactions:{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 actionscontext:null});

Triggering Transitions

// Trigger transitionsturnstile.insertCoin(25);// Passes 25 as the coin valueturnstile.push();// Chain transitionsturnstile.insertCoin(25).push().insertCoin(25);

Configuration

constfsm=newJSSM({// Optional settings_settings:{log:true,// Enable console loggingstrict:false,// Don't throw errors for invalid transitionshistory:false// Don't track transition history},transitions:[// Your transitions here],actions:{// Your actions here}});

State Management

// Get current stateconstcurrentState=turnstile.current();// or turnstile._current// Get previous stateconstpreviousState=turnstile._previous;// Check if a transition is possibleif(turnstile.can('push')){console.log('Can push the turnstile');}// Check if a transition is not possibleif(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:

functioninsertCoin(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


[8]ページ先頭

©2009-2025 Movatter.jp