Skip to content

Instantly share code, notes, and snippets.

@rjdestigter
Last active March 5, 2020 13:41
Show Gist options
  • Save rjdestigter/84a55cfc3704ce2d52670fa80ebed9cd to your computer and use it in GitHub Desktop.
Save rjdestigter/84a55cfc3704ce2d52670fa80ebed9cd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const context = {
questions: ["Q1", "Q2", "Q3"],
current: -1,
answers: new Map()
}
const machine = Machine(
{
id: "quiz",
initial: "quizzing",
context,
states: {
quizzing: {
after: { 15000: "done" },
initial: "questioning",
states: {
questioning: {
after: { 5000: "timeout" },
on: {
'': {
target: "#quiz.done",
cond: "noMoreQuestions"
},
ANSWER: {
target: "questioning",
internal: false,
actions: "increment"
}
}
},
timeout: {
entry: ["increment"],
after: { 2500: "questioning" }
}
}
},
done: {}
}
},
{
actions: {
increment: assign({
current: ctx => ctx.current + 1,
answers: (ctx, e) => {
ctx.answers.set(ctx.current, "answer") // e.answer
return ctx.answers
}
})
},
guards: {
noMoreQuestions: ctx => ctx.current === ctx.questions.length - 1
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment