README
State
Simplest possible state machine.
Install
npm i @cloudcmd/state --save
How to use?
const simpleState = require('@cloudcmd/state');
const states = {
'init': ['process'],
'process': ['error', 'ok'],
'error': [],
'ok': [],
};
const stateMachine = simpleState('init', states);
stateMachine.on('state-not-found', (name) => {
console.error('state not found:', name);
})
stateMachine.on('before-next-state', (name) => {
console.log(`before next state: ${name}`);
})
stateMachine.on('next-state', (name) => {
console.log(`next state: ${name}`);
})
stateMachine.on('after-next-state', (name) => {
console.log(`after next state: ${name}`);
})
stateMachine.on('before:next-state:init') => {
console.log('before some kind of init');
})
stateMachine.on('next-state:init') => {
console.log('some kind of init');
});
stateMachine.on('after:next-state:init') => {
console.log('after some kind of init');
})
const {setNext} = stateMachine;
const [status, processName] = setNext('process');
// returns
[true, 'process']
const [status] = setNext('hello');
// returns
[false];
setNext('error');
// returns
[true, 'error']
Related
zames - converts callback-based functions to Promises and apply currying to arguments.
currify - translate the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single or more arguments.
fullstore - functional variables.
License
MIT