Skip to content

Instantly share code, notes, and snippets.

@rickysullivan
Created September 22, 2020 10:53
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 rickysullivan/06c42ee633fcdac3cacaf85ba5288cd4 to your computer and use it in GitHub Desktop.
Save rickysullivan/06c42ee633fcdac3cacaf85ba5288cd4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'match',
initial: 'idle',
context: {
serves: 0,
winner: null,
playerA: {
score: 0
},
playerB: {
score: 0
}
},
states: {
idle: {
on: {
START: 'start'
}
},
start: {
on: {
'': 'play'
}
},
play: {
on: {
'AWARD': [
{ target: 'awarding', cond: 'isStillPlaying' },
{ target: 'end', cond: 'isWinner' },
]
}
},
awarding: {
exit: ['awardPoint'],
'': 'play'
},
end: {
type: 'final'
}
}
},
{
actions: {
// action implementation
alertGreen: (context, event) => {
alert('Green!');
},
awardPoint: () => {},
updateServers: (context) => {
context.serves++
}
},
activities: {
/* ... */
},
guards: {
isWinner: (context) => {
return context.playerA.score >=21
},
isStillPlaying: (context) => {
return context.playerA.score < 21 || context.playerB.score < 21
},
},
services: {
/* ... */
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment