Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dy/st8
Tiny state machine
https://github.com/dy/st8
state state-machine state-management
Last synced: 4 days ago
JSON representation
Tiny state machine
- Host: GitHub
- URL: https://github.com/dy/st8
- Owner: dy
- License: mit
- Created: 2014-08-21T12:13:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-02-19T19:08:16.000Z (almost 3 years ago)
- Last Synced: 2024-12-27T19:34:04.702Z (14 days ago)
- Topics: state, state-machine, state-management
- Language: JavaScript
- Homepage:
- Size: 126 KB
- Stars: 6
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# st8 [![test](https://github.com/dy/st8/actions/workflows/test.yml/badge.svg)](https://github.com/dy/st8/actions/workflows/test.yml) [![npm](https://img.shields.io/npm/v/st8)](http://npmjs.org/st8)
> Tiny state machine for structural describing behavior of components.
# Usage
```
$ npm install st8
```## Standalone state machine
```js
import State from 'st8';var state = new State({
// enter, exit
b: () => () => {},// enter shortcut, forwards to state d
c: () => 'd',// shorter cut, redirects to state a
d: 'a',// any other state
_: 'a'
});//enter state 'a', invoke corresponding callbacks
state.set('a');//get current state
state.get(); // 'a'
```## Define stateful object property
```js
import State from 'st8'var state = new State({
a() {
// onenter
this === target //true
},b() {
// onenter
this === target //true
return () => {
// onexit
}
}}, target);
Object.defineProperty(target, property, {
set: function (value) {
return state.set(value);
},
get: function () {
return state.get();
}
});
```# API
### let state = new State(states [, context])
Create a new state machine based on the `states` object. Optionally pass a `context` for callbacks.
### state.get()
Get current state.
### state.set(value)
Transition to a new state, invoking necessary callbacks.
🕉