Last active
October 1, 2020 00:44
-
-
Save theonetheycallneo/ea5d81067efebe36785b43ebc2652b67 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 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