Skip to content

Instantly share code, notes, and snippets.

@Tymek
Last active May 21, 2020 21:53
Show Gist options
  • Save Tymek/7021b7d1e72d11f6d7812a120955272d to your computer and use it in GitHub Desktop.
Save Tymek/7021b7d1e72d11f6d7812a120955272d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// 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