Skip to content

Instantly share code, notes, and snippets.

@MichalBryxi
Last active November 13, 2020 07:29
Show Gist options
  • Save MichalBryxi/eea1ced4d06f3a38df86e0bd53ccfb70 to your computer and use it in GitHub Desktop.
Save MichalBryxi/eea1ced4d06f3a38df86e0bd53ccfb70 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine(
{
initial: 'idle',
context: {
winning: 'heads',
selected: 'tails'
},
states: {
idle: {
on: {
SELECT: [
{ target: 'playing' }
]
},
},
playing: {
// It might seem that entry level action fires first
entry: ['flipCoin'],
on: {
'': [
// And *after* that the guard is checked.
// But this is not how it works.
{ target: 'score', cond: 'isScore' },
{ target: 'nope' }
]
}
},
nope: {
type: 'final'
},
score: {
type: 'final'
},
}
}, {
guards: {
isScore(context) {
console.log('guard isScore');
return context.selected === context.winning;
},
},
actions: {
flipCoin(context) {
context.selected = 'heads';
console.log('action flipCoin');
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment