Last active
July 2, 2021 02:30
-
-
Save sugasaki/562f33e45348a55ffc3d334225f38140 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
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