Skip to content

Instantly share code, notes, and snippets.

@KyleAMathews
Created April 3, 2020 23:06
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 KyleAMathews/3a8bf931a247905b0d4c49f9b0928f99 to your computer and use it in GitHub Desktop.
Save KyleAMathews/3a8bf931a247905b0d4c49f9b0928f99 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 recipeMachine = Machine({
id: 'recipe',
initial: 'init',
context: {
currentStep: 0,
steps: [1,2,3]
},
states: {
init: {
on: {
CONTINUE: 'present plan'
}
},
"present plan": {
entry: ['incrementStep'],
on: {
APPLY: 'applying step',
}
},
"applying step": {
on: {
FINISH: [
{
target: 'present plan',
// The 'searchValid' guard implementation details are
// specified in the machine config
cond: 'hasNextStep'
},
{
target: 'done',
// The 'searchValid' guard implementation details are
// specified in the machine config
cond: 'atLastStep'
},
],
}
},
"done": {
type: 'final'
},
},
},{
actions: {
incrementStep: assign((context, event) => {
return {
currentStep: context.currentStep + 1
}
}),
},
guards: {
hasNextStep: (context, event) => {
return context.currentStep < context.steps.length
},
atLastStep: (context, event) => {
return context.currentStep === context.steps.length
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment