next-state
v1.0.0
Published
Simple event-driven state machine
Downloads
9
Readme
next-state
Simple event-driven state machine.
Support
Install
npm i next-state
Usage
Let's imagine you have 3 states:
INTERMEDIATE -> ACTIVE <-> INACTIVE
You should define 3 transitions:
const { createState, createMachine } = require('next-state');
const INTERMEDIATE = 'INTERMEDIATE';
const ACTIVE = 'ACTIVE';
const INACTIVE = 'INACTIVE';
const transitions = {
[INTERMEDIATE]: createState(ACTIVE),
[ACTIVE]: createState(INACTIVE),
[INACTIVE]: createState(ACTIVE)
};
const machine = createMachine(transitions, INTERMEDIATE);
machine.on(ACTIVE, () => console.log('button pressed on'));
machine.on(INACTIVE, () => console.log('button pressed off'));
function FriendlyButton() {
return <Button onClick={ () => {
const nextstate = machine.state === ACTIVE ? INACTIVE : ACTIVE;
machine.next(nextstate);
}; }> { machine.state } </Button>
}
See also nodertc/dtls for more complex example.
License
MIT, 2019 © Dmitriy Tsvettsikh