https://github.com/jschomay/frpfsm
FRP state machine
https://github.com/jschomay/frpfsm
Last synced: 7 months ago
JSON representation
FRP state machine
- Host: GitHub
- URL: https://github.com/jschomay/frpfsm
- Owner: jschomay
- License: mit
- Created: 2015-06-06T20:08:51.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-01-25T20:24:18.000Z (over 9 years ago)
- Last Synced: 2024-09-29T18:45:39.873Z (9 months ago)
- Language: JavaScript
- Size: 76.2 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# frpfsm (FRP Finite State Machine)
A small and simple Functionally Reactive Programming (streams) based state machine with a nice API for defining and transitioning states. Built on top of kefir.js.
## Concept
Each state is a factory function that takes initial data and returns a stream that emits a single event when it wants to transition to a different state, specifying the transition name and exit data. frpfsm connects all the state streams together.
## Usage
Initializing your states and transitions:
```javascript
frpfsm.loadState({
name: "Preload",
fn: preloadState,
transitions:{
"loaded": "Start"
}
});frpfsm.loadState({
name: "Start",
fn: startState,
transitions:{
"readyToPlay": "Play"
"changeSettings": "Settings"
}
});// etc...
```
Define your states:
```javascript
startState = function(startingValue) {
// do other state stuff...// return stream that emmits a transition event when the play button is clicked
return Kefir.fromEvents(document.querySelector('#start'), 'click')
.map(function() {
return ["readyToPlay", startingValue];
});
};
```Start the machine:
```javascript
var debug = true;
frpfsm.start("Preload", startingValue, debug);
```See `example.js` for a complete example.