Skip to content

Instantly share code, notes, and snippets.

@tivac
Created March 18, 2021 22:06
Show Gist options
  • Save tivac/bedd527b21d72f582d6b9a85f9fb4589 to your computer and use it in GitHub Desktop.
Save tivac/bedd527b21d72f582d6b9a85f9fb4589 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const gameMachine = Machine(
{
id: 'game',
initial: 'playing',
context: {
points: 0
},
states: {
playing: {
// Eventless transition
// Will transition to either 'win' or 'lose' immediately upon
// entering 'playing' state or receiving AWARD_POINTS event
// if the condition is met.
always: [
{ target: 'win', cond: 'didPlayerWin' },
{ target: 'lose', cond: 'didPlayerLose' }
],
on: {
// Self-transition
AWARD_POINTS: {
actions: assign({
points: 100
})
}
}
},
win: { type: 'final' },
lose: { type: 'final' }
}
},
{
guards: {
didPlayerWin: (context, event) => {
console.log(context.points);
// check if player won
return context.points > 99;
},
didPlayerLose: (context, event) => {
console.log(context.points);
// check if player lost
return context.points < 0;
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment