comfey
Version:
Tiny micro mini data binding library inspired by react hook useState
37 lines (25 loc) • 1.09 kB
Markdown
Very light weight add-on to Comfey to power your states with Finite State Machine logic.
[](https://codesandbox.io/s/comfey-finite-state-machine-9cfpm)
[](https://comfey-fsm.netlify.app/) [GitHub](https://github.com/dejavu1987/comfey-rpg-finite-state-machine)
```js
import Comfey, { fsm, ComfeyDom } from 'comfey';
const DEBUG = true;
const locationFsmConfig = {
start: 'a',
a: { goToC: 'c' },
c: { goToX: 'x', goToA: 'a' },
};
const app = new Comfey(new ComfeyDom(document.getElementById('app'), DEBUG));
const location = fsm(app, 'location', locationFsmConfig, locationWatcher);
function locationWatcher(newLoc, oldLoc) {
console.log('Transitioning:', oldLoc + ' -> ' + newLoc);
}
console.log('Current Location:', location.getState()); // a
console.log('Possible Transitions:', location.getTransitions()); // ['goToC']
location.transition('goToC');
console.log('Current Location:', location.getState()); // c
console.log('Possible Transitions:', location.getTransitions()); // ['goToX', 'goToA']
```