Skip to content

Instantly share code, notes, and snippets.

@MarceloAlves
Created July 18, 2020 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarceloAlves/871c4b0892b8f7db32d7c1c6875c9380 to your computer and use it in GitHub Desktop.
Save MarceloAlves/871c4b0892b8f7db32d7c1c6875c9380 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const MOISTURE_LEVEL = {
WET: 1,
DRY: 0,
}
const soilMachine = Machine(
{
id: 'soil',
initial: 'startup',
context: {
notificationSent: false,
previousReading: null,
currentReading: null,
},
states: {
startup: {
on: {
SETUP: {
actions: ['setup', 'initialReading'],
target: 'idle',
},
},
},
idle: {
after: {
5000: 'running',
},
},
running: {
on: {
'': {
actions: 'checkPin',
target: 'sendingAlert',
},
},
},
sendingAlert: {
on: {
'': [
{
cond: 'shouldSendAlert',
actions: 'sendAlert',
target: 'idle',
},
{ target: 'idle' },
],
},
},
},
},
{
actions: {
setup: () => console.log('setup'),
initialReading: assign((ctx, evt) => ({
previousReading: 1,
currentReading: 1,
})),
checkPin: assign((ctx, evt) => {
console.log('Checking Pin')
return {
previousReading: ctx.currentReading,
currentReading: Math.round(Math.random()),
}
}),
sendAlert: () => {
console.log('FEED ME SEYMOUR!')
},
},
guards: {
isWet: (ctx, evt) => ctx.currentReading === MOISTURE_LEVEL.WET,
shouldSendAlert: (ctx, evt) => {
return ctx.previousReading !== ctx.currentReading && ctx.currentReading === MOISTURE_LEVEL.DRY
},
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment