Last active
November 13, 2020 07:29
-
-
Save MichalBryxi/eea1ced4d06f3a38df86e0bd53ccfb70 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
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