kristi
v1.2.4
Published
Simple FSM engine
Downloads
5
Readme
Kristi
Kristi is an asynchronous finite state machine engine. It allows you to describe a program (or part of it) using an automata-based approach.
In addition, this is my first experiment with literate programming (with help of a literate-programming-lib).
Kristi is inspired by Machina.js library.
Usage
Import
Users of npm can use npm install kristi
.
In other case, use gulp build-min
to get minified UMD-compatible build.
FSM Construction
import { Automaton, EVENTS } from 'kristi';
let fsm = new Automaton({
'login-screen-is-shown': {
transitions: {
'user-authenticated' : 'todo-screen-is-shown',
'password-recovery-requested' : 'password-recovery-screen-is-shown',
},
coming() {
let fsm = this; // Automaton instance is set as `this` in `coming` and `leaving`;
// AJAX requests for screen template, etc...
return new Promise((resolve) => {
$('#btn-recover-passw').click(() => {
fsm.processEvent('password-recovery-requested');
});
resolve();
});
},
leaving() {
// Some clean-up
}
},
'todo-screen-is-shown' : {
...
},
...
});
fsm.startWith('login-screen-is-shown');
The "best practice" is to move transition functions out of fsm-schema definition:
let fsm = new Automaton({
'login-screen-is-shown': {
transitions: {
'user-authenticated' : 'todo-screen-is-shown',
'password-recovery-requested' : 'password-recovery-screen-is-show',
},
coming: showLoginScreen,
leaving: hideLoginScreen
}
...
});
Events
Kristi provides simple on/off
interface, so you can subscribe to some events from fsm instance.
fsm.on(EVENTS.TRANSITION, ({from, to}) => {...});
fsm.on(EVENTS.PROCESSING, ({state, event}) => {...});
TODO: add full Event API description.
API
The full API description could be found here.