Created
March 18, 2021 22:06
-
-
Save tivac/bedd527b21d72f582d6b9a85f9fb4589 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) => { | |
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