Skip to content

Instantly share code, notes, and snippets.

@leosuncin
Last active March 22, 2020 21:03
Show Gist options
  • Save leosuncin/31e2ebbde16e2e2b86d638746cbb9e12 to your computer and use it in GitHub Desktop.
Save leosuncin/31e2ebbde16e2e2b86d638746cbb9e12 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 cameraMachine = Machine({
id: 'camera',
initial: 'requestPermission',
context: {
picture: null,
},
states: {
requestPermission: {
invoke: {
id: 'request-permission',
src: 'requestCameraPermission',
onDone: [
{ target: 'ready', cond: 'hasPermission' },
{ target: 'unauthorized' },
],
onError: 'unauthorized',
},
},
ready: {
type: 'parallel',
states: {
flashMode: {
initial: 'off',
states: {
auto: {},
off: {},
on: {},
torch: {},
},
},
type: {
initial: 'back',
states: {
back: {},
front: {},
},
},
photo: {
invoke: {
id: 'take-picture',
src: 'takePicture',
onDone: { actions: ['setPicture', 'sendPicture'] },
},
},
},
on: {
SWITCH_FLASH_MODE: [
{
in: '#camera.ready.flashMode.off',
target: '#camera.ready.flashMode.auto',
},
{
in: '#camera.ready.flashMode.auto',
target: '#camera.ready.flashMode.on',
},
{
in: '#camera.ready.flashMode.on',
target: '#camera.ready.flashMode.torch',
},
{
in: '#camera.ready.flashMode.torch',
target: '#camera.ready.flashMode.off',
},
],
SWITCH_TYPE: [
{ in: '#camera.ready.type.back', target: '#camera.ready.type.front' },
{ in: '#camera.ready.type.front', target: '#camera.ready.type.back' },
],
TAKE_PHOTO: '.photo',
},
},
unauthorized: {},
},
}, {
guards: {
hasPermission(context, event) {
return event.data && event.data.status === 'granted';
},
},
actions: {
setPicture: assign((context, event) => ({
picture: event.data,
})),
sendPicture: sendParent((context, event) => ({
type: 'SET_PHOTO',
data: event.data,
})),
},
services: {
requestCameraPermission: () => Promise.resolve({status: 'granted'}),
takePicture: () => Promise.resolve({
width: 200,
height: 200,
uri: 'https://via.placeholder.com/200',
}),
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment