Skip to content

Instantly share code, notes, and snippets.

@apostopher
Created November 18, 2020 11:27
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 apostopher/86580f7b572a6102e329a85f245efac6 to your computer and use it in GitHub Desktop.
Save apostopher/86580f7b572a6102e329a85f245efac6 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 fetchMachine = Machine(
{
id: 'ble',
context: {
activeMeasurements: [],
measurements: {},
},
initial: 'idle',
on: {
CLEANUP: 'cleanup',
},
states: {
idle: {
on: { INIT: 'initializing' },
},
initializing: {
invoke: {
id: 'initializeBLE',
src: 'initializeBLE',
onDone: {
target: 'ready',
actions: 'saveManager',
},
onError: {
target: 'machineError',
actions: 'saveError',
},
},
},
ready: {
on: { SCAN: 'scanning' },
},
scanning: {
invoke: {
id: 'scanForDevices',
src: 'scanForDevices',
onDone: {
target: 'scanned',
actions: 'saveDevice',
},
onError: {
target: 'machineError',
actions: 'saveError',
},
},
},
scanned: {
on: {
CONNECT: 'connecting',
},
},
connecting: {
invoke: {
id: 'connectToNTester',
src: 'connectToNTester',
onDone: {
target: 'connected',
actions: 'saveDevice',
},
onError: {
target: 'machineError',
actions: 'saveError',
},
},
},
connected: {
id: 'connected',
initial: 'ready',
invoke: {
id: 'watchConnection',
src: 'watchConnection',
},
on: {
DISCONNECTED: [
{
target: 'ready',
cond: 'managerFound',
},
{
target: 'idle',
},
],
},
states: {
ready: {
on: {
START_MEASUREMENT: 'measuring',
},
},
measuring: {
invoke: {
id: 'observeMeasurements',
src: 'observeMeasurements',
},
on: {
UPDATE_MEASUREMENT: {
actions: 'updateMeasurement',
},
STOP_MEASUREMENT: 'measured',
MEASUREMENT_ERROR: '#ble.machineError',
},
},
measured: {
exit: 'cleanupMeasurements',
on: {
SAVE_MEASUREMENT: {
target: 'measuring',
actions: 'saveMeasurements',
},
RESTART: [
{
target: '#ble.ready',
cond: 'managerFound',
},
{
target: '#ble.idle',
},
],
},
},
},
},
machineError: {
on: {
RESTART: [
{
target: 'ready',
cond: 'managerFound',
},
{
target: 'idle',
},
],
},
},
cleanup: {
invoke: {
id: 'cleanup',
src: 'cleanup',
onDone: {
target: 'idle',
actions: 'resetContext',
},
onError: {
target: 'machineError',
actions: 'saveError',
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment