xstate vs microstates - Finite State Machines example
const { Machine } = require('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')
.value;
const { create } = require('microstates');
class LightMicrostate {
constructor() {
this.light = String;
}
timer() {
let { light } = this;
switch (light.state) {
case 'green': return light.set('yellow');
case 'yellow': return light.set('red');
case 'red': return light.set('green');
}
return this;
}
}
let lightMicrostate = create(LightMicrostate, { light: 'green' });
lightMicrostate.timer().light.state
no comments