Last active
February 8, 2021 04:40
-
-
Save tomByrer/f6aabba3ef4581972b20e4a3b0b18587 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// volume contrll | |
/* | |
{ | |
"type": "VOL_CHANGE", | |
"value": 0.9 | |
} | |
*/ | |
function mute(){ | |
console.log('🔇 muted') | |
} | |
function unmute(vol){ | |
console.log('👂 unmuted, vol: '+ vol) | |
} | |
const volumeMachine = Machine({ | |
id: 'volume', | |
initial: 'heard', | |
context: { | |
volume: 0.5 | |
}, | |
states: { | |
heard: { // idle | |
entry: (context)=>{ | |
console.log('vol: '+ context.volume) | |
}, | |
on: { | |
MUTE_TOGGLE: 'muted', | |
VOL_CHANGE: { | |
actions: assign((context, event) => { | |
console.log('assign volume: '+ event.volume) | |
return { | |
volume: event.value | |
} | |
}), | |
target: 'heard' | |
}, | |
}, | |
}, | |
muted: { | |
entry: ()=> mute(), | |
on: { | |
MUTE_TOGGLE: 'heard', | |
VOL_CHANGE: { | |
actions: assign((context, event) => { | |
console.log('assign volume: '+ event.volume) | |
return { | |
volume: event.value | |
} | |
}), | |
target: 'heard' | |
}, | |
}, | |
exit: context=> unmute(context.volume), | |
}, | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment