Skip to content

Instantly share code, notes, and snippets.

@allencoded
Last active February 21, 2021 20:47
Show Gist options
  • Save allencoded/74afe92764d8ef0d2e45be7f58c5c823 to your computer and use it in GitHub Desktop.
Save allencoded/74afe92764d8ef0d2e45be7f58c5c823 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 dynamicScreenMachine = Machine({
id: 'dynamicScreen',
initial: 'idle',
context: {
questions: []
},
states: {
idle: {
on: {
COMPUTE: 'loading'
}
},
loading: {
on: {
ADD_QUESTION: {
actions: ['addQuestion']
},
DISPLAY_NEXT_QUESTION: 'question',
DISPLAY_NEXT_SCREEN_BUTTON: 'complete'
}
},
question: {
initial: 'idle',
states: {
idle: {
on: {
RESPONSE: 'answered',
}
},
answered: {
on: {
TRANSITION: '#dynamicScreen.loading',
},
final: true,
}
}
},
complete: {
final: true,
},
}
}, {
actions: {
addQuestion: assign((context,event) => {
if (!Array.isArray(event.questions)) {
return { questions: context.questions };
}
return { questions: [...context.questions, ...event.questions] };
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment