Skip to content

Instantly share code, notes, and snippets.

@jonathanstiansen
Last active February 5, 2021 23:13
Show Gist options
  • Save jonathanstiansen/e85113fb69f41bb210cbcd9a4cc7d124 to your computer and use it in GitHub Desktop.
Save jonathanstiansen/e85113fb69f41bb210cbcd9a4cc7d124 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// import { Camera as RNCamera } from 'expo-camera'
const recordVideo = (cameraRef, options) =>
cameraRef.recordAsync(options)
// VideoScreen is where the actual picture taking happens
const videoLimit = 11
const Video = Machine({
id: 'video',
initial: 'init',
context: {
newVideo: undefined,
initVideo: 'something',
videoToRemove: undefined,
videoOptions: {
// quality: RNCamera ? RNCamera.Constant.VideoQuality['4:3'] : undefined,
mute: true,
maxDuration: videoLimit,
}
},
states: {
init: {
on: {
always: [
{ target: 'savedVideo', cond: 'doesPlayerHaveVideo' },
{ target: 'noVideo', cond: 'playerHasNoVideo' }
],
}
},
unsavedVideo: {
on: {
START_RECORDING: {target: "recording", actions: ['']}
},
},
recordingFailed: {
on: {always: 'unsavedVideo'}
},
recording: {
invoke: {
id: 'recordVideo',
src: (context, event) => recordVideo(context.options),
onDone: {
target: 'savedVideo',
actions: assign({ uri: (context, event) => event.uri })
},
onError: {
target: 'recordingFailed',
actions: assign({ error: (context, event) => event.data })
}
}
},
savedVideo: {
type: 'final',
on: {
START_RECORDING:
{ target: 'unsavedVideo', actions: ['removePreviousVideo']
}
}
},
noVideo: {
on: {START_RECORDING: 'recording'}
},
}
},
{
actions:{
removePreviousVideo: assign({
retries: (context, event) => {
context.newVideo = context.initVideo
context.initVideo = undefined
}
})
},
guards: {
doesPlayerHaveVideo: (context, event) => {
return context.initVideo;
},
playerHasNoVideo: (context, event) => {
// check if player lost
return !context.initVideo;
}
},
services: {
saveVideo: (context, event) => saveVideo()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment