Last active
May 21, 2020 21:53
-
-
Save Tymek/7021b7d1e72d11f6d7812a120955272d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const flipPowerSwitch = assign({ | |
powerSwitch: (context, event) => ( | |
context.powerSwitch === 'ON' | |
? 'OFF' | |
: 'ON' | |
) | |
}) | |
const isSwitchOn = (context, event) => | |
context.powerSwitch === 'ON' | |
const isSwitchOff = (context, event) => | |
context.powerSwitch === 'OFF' | |
const FLIP_POWER_OFF = { | |
target: 'poweredDown', | |
actions: 'flipPowerSwitch', | |
cond: 'isSwitchOn', | |
} | |
const fetchMachine = Machine({ | |
id: 'power', | |
initial: 'poweredDown', | |
context: { | |
powerSwitch: 'OFF' | |
}, | |
states: { | |
poweredDown: { | |
on: { | |
FLIP_POWER_ON: { | |
target: 'poweredUp', | |
actions: 'flipPowerSwitch', | |
cond: 'isSwitchOff', | |
}, | |
FLIP_POWER_OFF, | |
}, | |
}, | |
poweredUp: { | |
on: { | |
FLIP_POWER_OFF, | |
GCODE_M81: 'poweredDown', | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
flipPowerSwitch | |
}, | |
guards: { | |
isSwitchOn, | |
isSwitchOff, | |
} | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment