Skip to content

Instantly share code, notes, and snippets.

@tomByrer
Last active May 22, 2021 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomByrer/e06674b4cfc3123c58848cafb4a9bf68 to your computer and use it in GitHub Desktop.
Save tomByrer/e06674b4cfc3123c58848cafb4a9bf68 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const volumeMachine = Machine({
id: 'volume',
initial: 'init',
context: {
volume: 0.7,
previousVolume: 0.7,
isMuted: false,
},
states:{
init:{
entry: 'loadConfigDefaults',
//TODO: XState v5
// always:[
// { target: 'unmuted',
// },
// ],
on: {
'': [ // imediately invoke XState v4
{ target: 'unmuted',
},
],
},
},
unmuted:{
on:{
MUTE_TRIGGER:{
target: 'muted',
},
VOLUME_CHANGE:{
actions: 'volumeChange',
},
}
},
muted:{
entry: 'mute',
exit: 'unmute',
on:{
MUTE_TRIGGER:{
target: 'unmuted',
},
VOLUME_CHANGE:{
target: 'unmuted',
actions: 'volumeChange',
},
},
},
}
});
//## Actions
const loadConfigDefaults = (ctx)=>{
//TODO HTMLMediaElement.muted = ctx.isMuted
//TODO HTMLMediaElement.volume = ctx.volume
//TODO may have to assign values from an outside config, like cookie, profile, etc
}
const mute = assign((ctx, evt)=>{
//TODO HTMLMediaElement.muted = true
return {
isMuted: true,
previousVolume: ctx.volume,
volume: 0,
}
})
const unmute = assign((ctx, evt)=>{
//TODO HTMLMediaElement.muted = false
return {
isMuted: false,
volume: ctx.previousVolume,
}
})
const volumeChange = assign((ctx, evt)=>{
//TODO HTMLMediaElement.volume = evt.value
return {
volume: evt.value,
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment