Skip to content

Instantly share code, notes, and snippets.

@danielsdesk
Created January 15, 2020 06:52
Show Gist options
  • Save danielsdesk/a29884b39827aaf8228fb1185a308f83 to your computer and use it in GitHub Desktop.
Save danielsdesk/a29884b39827aaf8228fb1185a308f83 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const increment = assign({
count: ctx => ctx.count + 1
});
const decrement = assign({
count: ctx => ctx.count - 1
});
const MIN = 0;
const MAX = 10;
const countMachine = Machine({
initial: 'min',
context: { count: 0 },
states: {
min: {
on: {
'': {
target: 'mid',
cond: ctx => ctx.count > MIN
},
// disallow decrementing
DEC: undefined
}
},
mid: {
on: {
'': [
{
target: 'min',
cond: ctx => ctx.count === MIN
},
{
target: 'max',
cond: ctx => ctx.count === MAX
},
]
}
},
max: {
on: {
'': {
target: 'mid',
cond: ctx => ctx.count < MAX
},
// disallow incrementing
INC: undefined
}
}
},
on: {
INC: { actions: increment },
DEC: { actions: decrement }
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment