Skip to content

Instantly share code, notes, and snippets.

@benatwork
Last active October 22, 2019 14:57
Show Gist options
  • Save benatwork/790df6fef111494921812505545b8cde to your computer and use it in GitHub Desktop.
Save benatwork/790df6fef111494921812505545b8cde to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
//----------------------------------------
const findingDevice = {
initial: 'findingDevice',
states: {
/* BASE STATES */
foundDevice: {},
/* ACTION STATES */
findingDevice: {
on: {
BT_EXISTING_CONNECTION: 'foundDevice',
BT_PREVIOUSLY_STORED_DEVICE_ID: 'foundDevice',
BT_NO_EXISTING_CONNECTION: 'scanning'
}
},
scanning: {
on: {
BT_SCANNING_WARNING: 'scanningWarning',
BT_SCANNING_TIMEOUT: 'scanningTimeout',
BT_DEVICE_FOUND: 'foundDevice'
}
},
scanningWarning: {
on: {
BT_RESCANNING: 'scanning'
}
},
scanningTimeout: {
on: {
BT_RESCANNING: 'scanning'
}
}
}
}
const connectingToDevice = {
initial: 'connectingToDevice',
states: {
/* BASE STATES */
connected: {},
connectingError: {},
connectingTimeout: {},
bondingError: {},
bondingTimeout: {},
/* ACTION STATES */
connectingToDevice: {
on: {
BT_CONNECTED: 'connected',
BT_ANDROID_CONNECTED: 'bonding',
BT_CONNECTED_ERROR: 'connectingError',
BT_CONNECTED_TIMEOUT: 'connectingTimeout'
}
},
bonding: {
on: {
BT_BONDING_ERROR: 'bondingError',
BT_BONDING_TIMEOUT: 'bondingTimeout',
BT_CONNECTED: 'connected'
}
}
}
}
const inittingDevice = {
initial: 'inittingDevice',
states: {
/* BASE STATES */
deviceReady: {},
readTimeout: {},
/* ACTION STATES */
inittingDevice: {
on: {
BT_READING_SERVICES: 'readingServices',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
},
readingServices: {
on: {
BT_READING_DEVICE_STATE: 'readingDeviceState',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
},
readingDeviceState: {
on: {
BT_READING_BATTERY: 'readingBattery',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
},
readingBattery: {
on: {
BT_READING_FIRMWARE: 'readingFirmware',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
},
readingFirmware: {
on: {
BT_CREATING_NOTIFICATIONS: 'creatingNotifications',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
},
creatingNotifications: {
on: {
BT_CREATING_SUBSCRIPTIONS: 'creatingSubscriptions',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
},
creatingSubscriptions: {
on: {
DEVICE_READY: 'deviceReady',
BT_DEVICE_READ_ERROR: 'readTimeout'
}
}
}
}
const cartridgeValidations = {
initial: 'authorizingLocalCartridge',
states: {
/* BASE STATES */
localAuthFailure: {},
remoteAuthFailure: {},
dosingProfileValidationFailure: {},
/* ACTION STATES */
authorizingLocalCartridge: {
on: {
// FAILURE STATES
EVNT_INVLD_LOCAL_AUTH_FAILURE: 'localAuthFailure',
EVNT_INVLD_CARTRIDGE_EMPTY: 'localAuthFailure',
EVNT_INLVD_CARTRIDGE_REMOVED: 'localAuthFailure',
EVNT_INVLD_CARTRIDGE_REMOVED_IN_SESSION: 'localAuthFailure',
EVNT_INVLD_READ_ERROR: 'localAuthFailure',
// SUCCESS STATE
SYS_STATE_REMOTE_CARTRIDGE_AUTH: 'authorizingRemoteCartridge'
}
},
authorizingRemoteCartridge: {
on: {
RMT_INVLD_GENERAL_ERROR: 'remoteAuthFailure',
RMT_INVLD_NO_DOSING_PROFILE: 'remoteAuthFailure',
RMT_VALIDATING_DOSING_PROFILE: 'validatingDosingProfile'
}
},
validatingDosingProfile: {
on: {
CARTRIDGE_READY: 'cartridgeReady',
DOSING_PROFILE_VALIDATION_ERROR: 'dosingProfileValidationFailure'
}
},
cartridgeReady: {
},
}
}
const sessions = {
initial: 'sessionReady',
states: {
/* BASE STATES */
sessionError: {},
sessionReady: {
on: {
SYS_STATE_SESSION_START: 'startingSession',
}
},
/* ACTION STATES */
startingSession: {
on: {
SYS_STATE_SESSION_IN_PROGRESS: 'sessionInProgress',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
},
sessionInProgress: {
on: {
SYS_STATE_SESSION_END: 'sessionReady',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
}
}
}
const cartridgeAndSessions = {
initial: 'waitingForCartridge',
states: {
/* BASE STATES */
sessionsReady: {
on: {
SYS_STATE_READY: 'sessionsReady'
},
...sessions
},
/* ACTION STATES */
waitingForCartridge: {
on: {
SYS_STATE_READY: 'sessionsReady'
},
},
}
}
const cartridgeHandler = {
initial: 'validateCartridge',
states: {
validateCartridge: {
on: {
CARTRIDGE_VALID: 'validCartridge',
CARTRIDGE_INVALID: 'invalidCartridge'
}
// ...cartridgeValidations
},
invalidCartridge: {},
validCartridge: {}
}
}
const deviceEcoSystem = Machine({
id: 'deviceEcoSystem',
initial: 'disconnected',
states: {
/* BASE STATES */
disconnected: {
on: {
FIND_AND_CONNECT: 'connecting.findingDevice'
}
},
charging: {},
connecting: {
on: {
CONNECTING_ERROR: 'disconnected',
INITTING_ERROR: 'disconnected',
DEVICE_CONNECTED: 'connected.waitingForCartridge'
},
states: {
findingDevice: {
on: {
DEVICE_FOUND: 'connectingToDevice',
DEVICE_NOT_FOUND: 'deviceNotFound'
},
...findingDevice
},
connectingToDevice: {
on: {
CONNECTED_ERROR: 'connectingError',
CONNECTED: 'inittingDevice'
},
...connectingToDevice
},
inittingDevice: {
on: {
DEVICE_INITTED: 'deviceInitted',
ERROR_INITTING: 'deviceInitError'
},
...inittingDevice
},
deviceInitted: {},
deviceInitError: {},
deviceNotFound: {},
connectingError: {},
}
},
connected: {
on: {
BT_DISCONNECTED: 'disconnected',
CHARGING: 'charging',
},
states: {
waitingForCartridge: {
on: {
CARTRIDGE_CONNECTED: 'validatingCartridge',
},
},
validatingCartridge: {
on: {
CARTRIDGE_VALID: 'deviceReady',
CARTRIDGE_INVALID: 'invalidCartridge'
},
...cartridgeHandler
},
invalidCartridge: {},
deviceReady: {
on: {
START_SESSION: 'dosing',
CARTRIDGE_DISCONNECT: 'waitingForCartridge'
}
},
dosing: {
on: {
SESSION_END: 'deviceReady',
SESSION_ERROR: 'sessionError'
},
...sessions
},
sessionError: {
on: {
CONTINUE: 'deviceReady'
}
}
}
},
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment