Skip to content

Instantly share code, notes, and snippets.

@sugasaki
Last active July 2, 2021 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugasaki/562f33e45348a55ffc3d334225f38140 to your computer and use it in GitHub Desktop.
Save sugasaki/562f33e45348a55ffc3d334225f38140 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) => {
// check if player won
return context.points > 99;
},
didPlayerLose: (context, event) => {
// 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