Created
January 15, 2020 06:52
-
-
Save danielsdesk/a29884b39827aaf8228fb1185a308f83 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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