Skip to content

Instantly share code, notes, and snippets.

@seeden
Created February 18, 2021 12:36
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 seeden/81edc2510c03e50ea19b88fa795a1aff to your computer and use it in GitHub Desktop.
Save seeden/81edc2510c03e50ea19b88fa795a1aff to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const questionStates = {
initial: 'ready',
context: {
userAnswer: undefined,
oponentAnswer: undefined,
},
states: {
ready: {
on: {
ANSWER: 'wait_for_oponent_answer',
OPONENT_ANSWER: 'wait_for_user_answer',
},
},
wait_for_user_answer: {
on: {
ANSWER: 'results',
},
},
wait_for_oponent_answer: {
on: {
OPONENT_ANSWER: 'results',
},
},
results: {
exit: 'resetQuestion',
},
},
actions: {
resetQuestion: () => ({
userAnswer: undefined,
oponentAnswer: undefined,
}),
},
};
const quizStates = {
initial: 'quiz',
context: {
answers: [],
currentQuestion: 0,
currentQuestionDisplay: 1,
questions: [],
totalCorrectAnswers: 0,
},
states: {
quiz: {
on: {
NEXT_QUESTION: {
actions: ['nextQuestion']
},
},
...questionStates,
},
results: {
exit: 'resetQuiz',
},
},
actions: {
resetQuiz: () => ({
currentQuestion: 0,
currentQuestionDisplay: 1,
questions: [],
totalCorrectAnswers: 0,
}),
nextQuestion: (context) => ({
...context,
currentQuestion: context.currentQuestion + 1,
}),
},
};
const fetchMachine = Machine({
id: 'duel',
initial: 'welcome',
states: {
welcome: {
on: {
SEARCH: 'searching_oponent',
},
},
searching_oponent: {
on: {
OPONENT_FIND: 'quiz',
},
},
quiz: {
...quizStates,
},
results: {
on: {
PLAY_AGAIN: 'welcome',
},
exit: 'resetGame',
},
failure: {
on: {
RETRY: 'quiz',
START_OVER: 'welcome',
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment