Skip to content

Instantly share code, notes, and snippets.

@LevelbossMike
Last active November 23, 2020 10:31
Show Gist options
  • Select an option

  • Save LevelbossMike/79fbce1a1f58a087a9533e2d012193eb to your computer and use it in GitHub Desktop.

Select an option

Save LevelbossMike/79fbce1a1f58a087a9533e2d012193eb 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)
function readQRCode() {};
function startVideoStream() {};
const fetchMachine = Machine({
id: 'qrImport',
initial: 'qr',
states: {
qr: {
on: {
TO_DB: {
target: 'db',
},
},
type: 'parallel',
states: {
persistence: {
initial: 'noData',
states: {
noData: {
on: {
CODE_FOUND: 'data'
}
},
data: {
entry: assign({
qrData: (_context, event) => event.data
}),
exit: assign({
qrData: () => null
}),
on: {
SELECT_CODE: 'noData',
RESET_CODE: 'noData',
PERSIST_DATA: {
actions: 'persistData'
}
}
}
}
},
qrReader: {
initial: 'selection',
states: {
selection: {
initial: 'noSelection',
on: {
ENABLE_CAMERA: {
target: 'camera',
actions: send('RESET_CODE')
}
},
states: {
noSelection: {
id: 'noSelection',
on: {
SELECT_CODE: {
target: 'selected',
actions: [
assign({
selectedQRCode: (_context, event) => event.code
})
]
}
}
},
selected: {
initial: 'codeNotRead',
on: {
RESET_CODE: 'noSelection'
},
states: {
codeNotRead: {
on: {
READ_CODE: 'readingCode'
}
},
readingCode: {
invoke: {
src: readQRCode,
onDone: 'codeRead',
onError: 'codeReadError'
}
},
codeRead: {
entry: [
send({type: 'CODE_FOUND'})
],
on: {
PERSIST_DATA: '#noSelection',
}
},
codeReadError: {
on: {
'': 'codeNotRead'
}
}
}
}
}
},
camera: {
initial: 'willInitializeVideo',
on: {
ENABLE_SELECTION: {
target: 'selection',
actions: [send('RESET_CODE')]
}
},
states: {
willInitializeVideo: {
on: {
INIT: 'initializingVideo'
}
},
initializingVideo: {
invoke: {
src: startVideoStream,
onDone: 'videoInitialized',
onError: 'videoInitializeError'
}
},
videoInitialized: {
initial: 'huntingForCode',
states: {
huntingForCode: {
entry: 'startHuntForCode',
exit: 'cancelHuntForCode',
on: {
CODE_FOUND: 'codeFound'
}
},
codeFound: {
entry: [
'snapshotVideo',
'highlightQROnSnapshot'
],
on: {
RESTART_CODE_HUNT: {
target: 'huntingForCode',
actions: [
send('RESET_CODE')
]
}
}
}
}
},
videoInitializeError: {
entry: 'handleVideoError'
}
}
}
}
}
}
},
db: {
on: {
TO_QR: {
target: 'qr'
},
CLEAR_DB: {
actions: ['clearDb']
}
}
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment