Skip to content

Instantly share code, notes, and snippets.

@danielkcz
Created March 14, 2020 17:05
Show Gist options
  • Save danielkcz/c71fecfd3e9749dedc5292d7209a8ba2 to your computer and use it in GitHub Desktop.
Save danielkcz/c71fecfd3e9749dedc5292d7209a8ba2 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const audioStreamMachine = Machine({
id: 'audioStream',
initial: 'idle',
context: {
stream: null,
streamError: null,
},
states: {
idle: {
on: {
CLAIM_STREAM: 'claiming',
},
},
claiming: {
invoke: {
id: 'streamClaim',
src: () => getAudioStream(),
onDone: {
target: 'streaming',
actions: assign({ stream: (_, ev) => ev.data }),
},
onError: {
target: 'failed',
actions: assign({ streamError: (_, ev) => ev.data }),
},
},
on: {
STOP_STREAM: 'idle', // <-- I need to wait for promise resolve
/*
stream.getTracks().forEach(function(track) {
track.stop()
})
*/
},
},
streaming: {
on: {
STOP_STREAM: 'idle',
/*
stream.getTracks().forEach(function(track) {
track.stop()
})
*/
},
},
failed: {},
},
})
function getAudioStream() {
return navigator.mediaDevices.getUserMedia({
audio: true,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment