Skip to content

Instantly share code, notes, and snippets.

@theonetheycallneo
Last active October 1, 2020 00:44
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 theonetheycallneo/ea5d81067efebe36785b43ebc2652b67 to your computer and use it in GitHub Desktop.
Save theonetheycallneo/ea5d81067efebe36785b43ebc2652b67 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 sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
const fetchAll = () => {
console.log('fetchAll')
return new Promise(async resolve => {
await sleep(1000)
await sleep(50)
resolve({
isAnyFaults: false,
isAllCmdOff: true,
isEStopStatus: false,
wasEStopTriggered: true,
isWoFPumpOn: true,
})
})
}
const fetchMachine = Machine({
id: 'pexp',
initial: 'PoweredOn',
context: {
properties: {
isAnyFaults: false,
isAllCmdOff: false,
isEStopStatus: false,
wasEStopTriggered: false,
isWoFPumpOff: false
},
retries: 0
},
states: {
PoweredOn: {
on: {
isActive : 'fetching'
}
},
fetching: {
invoke: {
id: 'fetchAll',
src: fetchAll,
onDone: [
{
actions: ['setProps'],
target: 'TS1',
},
],
onError: {
target: 'OC'
}
}
},
TS1: {
invoke: {
id: 'TS1',
src: () => Promise.resolve(),
onDone: [
{
target: 'ES',
cond: (_ctx, event) => _ctx.properties.isEStopStatus
},
{
target: 'ESR',
cond: (_ctx, event) => !_ctx.properties.isEStopStatus && _ctx.properties.wasEStopTriggered
},
{
target: 'M',
cond: (_ctx, event) => !_ctx.properties.isEStopStatus && !_ctx.properties.wasEStopTriggered
}
],
onError: {
target: 'OC'
}
}
},
OC: {
on: {
isConnected: {
target: 'fetching'
}
}
},
ES: {
entry: ['logContext'],
invoke: {
src: 'fetchAll',
onDone: [
{
cond: (ctx, event) => !ctx.properties.eStopStatus,
target: 'TS1'
}, {
target: 'ES'
}
]
}
},
ESR: {
entry: ['logContext'],
exit: ['cleanupEStop'],
invoke: {
src: 'disableInputs',
onDone: [
{
cond: (ctx, event) => ctx.properties.isAllCmdOff,
target: 'TS1'
}, {
target: 'ESR'
}
]
}
},
M: {
entry: ['logContext'],
invoke: {
src: 'fetchAll',
onDone: [
{
cond: (ctx, event) => !ctx.properties.isAnyFaults && ctx.properties.isWoFPumpOn,
target: 'WoF'
}, {
target: 'M'
}
]
}
},
WoF: {
entry: ['logContext'],
invoke: {
src: 'fetchAll',
onDone: [
{
cond: (ctx, event) => !ctx.properties.isAnyFaults && ctx.properties.isWoFPumpOn,
target: 'WoF'
}, {
target: 'TS1'
}
]
}
}
}
}, {
actions: {
cleanupEStop: assign({
properties: (context, event) => {
return {
...context.properties,
wasEStopTriggered: false
}
}}),
logContext: (context, event) => {
console.log('log', context, event)
},
setProps: assign({
properties: (context, event) => {
console.log('event', event.data)
return event.data
}})
},
services: {
fetchAll: (context, event) => fetchAll()
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment