Skip to content

Instantly share code, notes, and snippets.

@bryanjswift
Last active October 25, 2019 18:24
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 bryanjswift/971b5812e51afd2d117775147cf2a505 to your computer and use it in GitHub Desktop.
Save bryanjswift/971b5812e51afd2d117775147cf2a505 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: {
type: 'final',
},
/* 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: {
type: 'final',
entry: send('CONNECTED'),
},
connectingError: {
type: 'final',
entry: send('CONNECTING_ERROR'),
},
connectingTimeout: {
type: 'final',
entry: send('CONNECTING_ERROR'),
},
bondingError: {
type: 'final',
entry: send('CONNECTING_ERROR'),
},
bondingTimeout: {
type: 'final',
entry: send('CONNECTING_ERROR'),
},
/* 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: {
type: 'final',
entry: send('DEVICE_INITTED'),
},
readTimeout: {
type: 'final',
entry: send('ERROR_INITTING'),
},
/* 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 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: 'validatingLocalCartridge',
states: {
/* BASE STATES */
localCartridgeError: {},
remoteCartridgeError: {},
cartridgeValid: {},
/* ACTION STATES */
validatingLocalCartridge: {
on: {
EVNT_INVLD_LOCAL_AUTH_FAILURE: 'localCartridgeError',
EVNT_INVLD_CARTRIDGE_EMPTY: 'localCartridgeError',
EVNT_INVLD_REMOTE_TIMEOUT: 'localCartridgeError',
EVNT_INLVD_CARTRIDGE_REMOVED: 'localCartridgeError',
EVNT_INVLD_READ_ERROR: 'localCartridgeError',
SYS_STATE_REMOTE_CARTRIDGE_AUTH: 'validatingRemoteCartridge',
},
},
validatingRemoteCartridge: {
on: {
RMT_INVLD_GENERAL_ERROR: 'remoteCartridgeError',
RMT_INVLD_NO_DOSING_PROFILE: 'remoteCartridgeError',
RMT_VALIDATING_DOSING_PROFILE: 'validatingDosingProfile',
},
},
validatingDosingProfile: {
on: {
SYS_STATE_READY: 'cartridgeValid',
},
},
},
};
const deviceEcoSystem = Machine({
id: 'deviceEcoSystem',
initial: 'disconnected',
states: {
/* BASE STATES */
disconnected: {
on: {
FIND_AND_CONNECT: 'connecting.findingDevice',
},
},
charging: {
on: {
SYS_STATE_CHARGE_COMPLETE: 'connected.waitingForCartridge',
SYS_STATE_WAITING_FOR_CARTRIDGE: 'connected.waitingForCartridge',
},
},
connecting: {
on: {
CONNECTING_ERROR: 'disconnected',
INITTING_ERROR: 'disconnected',
DEVICE_CONNECTED: 'connected.waitingForCartridge',
},
states: {
findingDevice: {
on: {
DEVICE_NOT_FOUND: 'deviceNotFound',
},
...findingDevice,
onDone: 'connectingToDevice',
},
connectingToDevice: {
on: {
CONNECTED: 'inittingDevice',
},
...connectingToDevice,
},
inittingDevice: {
on: {
DEVICE_INITTED: 'deviceInitted',
ERROR_INITTING: 'deviceInitError',
},
...inittingDevice,
},
deviceInitted: {
type: 'final',
entry: send('DEVICE_CONNECTED'),
},
deviceInitError: {
type: 'final',
entry: send('INITTING_ERROR'),
},
deviceNotFound: {
type: 'final',
entry: send('CONNECTING_ERROR'),
},
},
},
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