Skip to content

Instantly share code, notes, and snippets.

@kellyjandrews
Last active July 9, 2020 16:40
Show Gist options
  • Save kellyjandrews/f6ef30ef3a1fdc3f43054213c4806f4d to your computer and use it in GitHub Desktop.
Save kellyjandrews/f6ef30ef3a1fdc3f43054213c4806f4d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const stream = Machine(
{
id: 'stream',
initial: 'connected',
context: {
stream: null,
hasAudio: true,
hasVideo: true,
videoSrcObject: null,
audioLevel: {
movingAvg: null,
logLevel: null,
},
},
states: {
connected: {
id: 'connected',
invoke: {
id: 'monitorStreamEvents',
src: 'monitorStreamEvents',
},
on: {
AUDIO_LEVEL_UPDATED: {
actions: 'updateAudioLevel',
},
VIDEO_ELEMENT_CREATED: {
actions: 'assignStream',
},
TOGGLE_AUDIO_PUBLISH: {
actions: ['toggleAudioPublish', 'sendToggleAudioEvent'],
},
TOGGLE_VIDEO_PUBLISH: {
actions: ['toggleVideoPublish', 'sendToggleVideoEvent'],
},
TOGGLE_AUDIO: { actions: 'toggleAudio' },
TOGGLE_VIDEO: { actions: 'toggleVideo' },
},
},
},
},
{
actions: {
updateAudioLevel: assign({ audioLevel: (ctx, e) => e.audioLevel }),
assignStream: assign({
stream: (_, e) => e.stream,
videoSrcObject: (_, e) => e.stream,
}),
sendToggleAudioEvent: send((ctx, e) => ({
type: 'TOGGLE_AUDIO',
value: !ctx.hasAudio,
})),
sendToggleVideoEvent: send((ctx, e) => ({
type: 'TOGGLE_VIDEO',
value: !ctx.hasVideo,
})),
toggleAudioPublish: (ctx) => !ctx.hasAudio,
toggleVideoPublish: (ctx) => !ctx.hasVideo,
toggleAudio: assign({ hasAudio: (ctx, e) => e.value }),
toggleVideo: assign({ hasVideo: (ctx, e) => e.value }),
},
services: {
monitorStreamEvents: () => {} ,
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment