Created
July 18, 2020 05:21
-
-
Save MarceloAlves/871c4b0892b8f7db32d7c1c6875c9380 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
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