Last active
February 8, 2021 05:00
-
-
Save tomByrer/293787d6f2afe9d4a79cdba56591a78c 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 DRY contrll | |
/* copy/paste below object into bottom of EVENTS tab | |
{ | |
"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: 'setVolume', | |
target: 'heard' | |
}, | |
}, | |
}, | |
muted: { | |
entry: ()=> mute(), | |
on: { | |
MUTE_TOGGLE: 'heard', | |
VOL_CHANGE: { | |
actions: 'setVolume', | |
target: 'heard' | |
}, | |
}, | |
exit: context=> unmute(context.volume), | |
}, | |
} | |
}, | |
{ | |
actions: { | |
setVolume: assign((context, event) => { | |
console.log('assign volume: '+ event.value) | |
return { | |
volume: event.value | |
} | |
}), | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment