st8
Version:
Tiny state machine
96 lines (63 loc) • 1.64 kB
Markdown
> Tiny state machine for structural describing behavior of components.
```
$ npm install st8
```
```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'
```
```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();
}
});
```
Create a new state machine based on the `states` object. Optionally pass a `context` for callbacks.
Get current state.
Transition to a new state, invoking necessary callbacks.
<!--
Subscribe to state changes.
Returns _Subscribable_ for interop with _Observables_ etc.
-->
<p align="center">🕉<p>