2017-06-02 13:19:50 +00:00
|
|
|
import { StateMachine, StateMachineEvent, create } from 'javascript-state-machine';
|
|
|
|
|
|
|
|
|
|
interface StateMachineTest extends StateMachine {
|
2020-05-15 02:20:31 +00:00
|
|
|
warn?: StateMachineEvent;
|
|
|
|
|
panic?: StateMachineEvent;
|
|
|
|
|
calm?: StateMachineEvent;
|
|
|
|
|
clear?: StateMachineEvent;
|
2017-06-02 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fsm: StateMachineTest = StateMachine.create({
|
2020-05-15 02:20:31 +00:00
|
|
|
initial: 'green',
|
|
|
|
|
events: [
|
|
|
|
|
{ name: 'warn', from: 'green', to: 'yellow' },
|
|
|
|
|
{ name: 'panic', from: 'yellow', to: 'red' },
|
|
|
|
|
{ name: 'calm', from: 'red', to: 'yellow' },
|
|
|
|
|
{ name: 'clear', from: 'yellow', to: 'green' }
|
|
|
|
|
],
|
|
|
|
|
callbacks: {
|
|
|
|
|
onpanic(event?, from?, to?, msg?) { alert('panic! ' + msg); },
|
|
|
|
|
onclear(event?, from?, to?, msg?) { alert('thanks to ' + msg); },
|
|
|
|
|
ongreen(event?, from?, to?) { document.body.className = 'green'; },
|
|
|
|
|
onyellow(event?, from?, to?) { document.body.className = 'yellow'; },
|
|
|
|
|
onred(event?, from?, to?) { document.body.className = 'red'; },
|
|
|
|
|
}
|
2017-06-02 13:19:50 +00:00
|
|
|
});
|
|
|
|
|
|
2020-05-15 02:20:31 +00:00
|
|
|
// fsm.warn(); // transition from green to yellow
|
|
|
|
|
// fsm.panic("ERROR ALERT"); // transition from yellow to red
|
|
|
|
|
// fsm.calm(); // transition from red to yellow
|
|
|
|
|
// fsm.clear("All clear"); // transition from yellow to green
|
2017-06-02 13:19:50 +00:00
|
|
|
|
|
|
|
|
const transitions = fsm.transitions();
|