Get started with javascript-state-machine CDN
MIT licensed
Library for creating, managing finite state machines.
Tags:- finite state machine
- state machine
- server
- client
Stable version
Copied!
How to start using javascript-state-machine CDN
// Import the state machine library
const StateMachine = require('state-machine');
// Define the states
const states = {
STATE_A: 'stateA',
STATE_B: 'stateB',
STATE_C: 'stateC'
};
// Define the transitions
const transitions = {
[states.STATE_A]: {
'eventA': states.STATE_B,
'eventB': states.STATE_C
},
[states.STATE_B]: {
'eventA': states.STATE_A,
'eventB': states.STATE_C
},
[states.STATE_C]: {
'eventA': states.STATE_A,
'eventB': states.STATE_B
}
};
// Define the initial state
const initialState = states.STATE_A;
// Create the state machine
const stateMachine = new StateMachine({
initial: initialState,
transitions
});
// Example usage
stateMachine.addListener('transition', (event, from, to) => {
console.log(`Transitioned from ${from} to ${to} with event ${event}`);
});
stateMachine.sendEvent('eventA'); // Transitions from STATE_A to STATE_B or STATE_C
stateMachine.sendEvent('eventB'); // Transitions from the current state to another state
Copied!
Copied!
Copied!
Copied!
Copied!
Copied!