Skip to content

Instantly share code, notes, and snippets.

@danielmarquesr
Last active April 15, 2021 15:16
Show Gist options
  • Save danielmarquesr/eab907744327e662f85b314d1248abeb to your computer and use it in GitHub Desktop.
Save danielmarquesr/eab907744327e662f85b314d1248abeb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine({
id: 'root',
type: 'parallel',
context: {
left: null,
right: null,
},
states: {
bluetooth: {
initial: 'idle',
states: {
idle: {
on: {
SCAN: {
target: 'scanning',
},
},
},
scanning: {
on: {
FINISH_CONNECT: {
target: 'idle',
},
},
invoke: {
src: () => (callback) => {
callback({ type: 'CONNECT_LEFT', payload: { id: 'LEFT' } })
callback({ type: 'CONNECT_RIGHT', payload: { id: 'RIGHT' } })
callback('FINISH_CONNECT')
},
},
},
},
},
left: {
initial: 'idle',
states: {
idle: {
on: {
CONNECT_LEFT: {
target: 'connecting',
actions: assign({
left: (_, event) => event.payload,
}),
},
},
},
connecting: {
on: {
RESOLVE_LEFT: { target: 'connected' },
ERROR_LEFT: { target: 'connection_failure' },
},
},
connected: {
on: {
START: { target: 'running' },
DISCONNECT_LEFT: { target: 'idle' },
},
},
connection_failure: {
on: {
RESET_LEFT: {
target: 'idle',
},
},
invoke: {
src: (context) => (callback) => {
context.left = null
callback('SCAN')
callback('RESET_LEFT')
},
},
},
running: {
on: {
STOP: { target: 'connected' },
LOST_LEFT: { target: 'connection_failure' },
},
},
},
},
right: {
initial: 'idle',
states: {
idle: {
on: {
CONNECT_RIGHT: {
target: 'connecting',
actions: assign({
right: (_, event) => event.payload,
}),
},
},
},
connecting: {
on: {
RESOLVE_RIGHT: { target: 'connected' },
ERROR_RIGHT: { target: 'connection_failure' },
},
},
connected: {
on: {
START: { target: 'running' },
DISCONNECT_RIGHT: { target: 'idle' },
},
},
connection_failure: {
on: {
RESET_RIGHT: {
target: 'idle',
},
},
invoke: {
src: (context) => (callback) => {
context.right = null
callback('SCAN')
callback('RESET_RIGHT')
},
},
},
running: {
on: {
STOP: { target: 'connected' },
LOST_RIGHT: { target: 'connection_failure' },
},
},
},
},
},
on: {
START: { actions: send('START') },
STOP: { actions: send('STOP') },
DISCONNECT_LEFT: { actions: send('DISCONNECT_LEFT') },
DISCONNECT_RIGHT: { actions: send('DISCONNECT_RIGHT') },
SCAN: { actions: send('SCAN') },
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment