davidkpiano / xstate
- воскресенье, 27 августа 2017 г. в 03:14:14
Stateless JS Finite State Machines and Statecharts
Simple, stateless JavaScript finite state machines and statecharts.
Read the slides (video coming soon!) or check out these resources for learning about the importance of finite state machines and statecharts in user interfaces:
The JSON-based notation used here to declaratively represent finite state machines and statecharts can be copy-pasted here: https://codepen.io/davidkpiano/pen/ayWKJO/ which will generate interactive state transition diagrams.
npm install xstate --save
import { Machine } from 'xstate';
import { Machine } from 'xstate';
const lightMachine = Machine({
key: 'light',
initial: 'green',
states: {
green: {
on: {
TIMER: 'yellow',
}
},
yellow: {
on: {
TIMER: 'red',
}
},
red: {
on: {
TIMER: 'green',
}
}
}
});
const currentState = 'green';
const nextState = lightMachine
.transition(currentState, 'TIMER')
.toString(); // toString() only works for non-parallel machines
// => 'yellow'
More examples coming soon!