Skip to content

Instantly share code, notes, and snippets.

@sonntam
Last active March 30, 2020 11:58
Show Gist options
  • Save sonntam/b93e00325b3d7fe66a06c0829161eeda to your computer and use it in GitHub Desktop.
Save sonntam/b93e00325b3d7fe66a06c0829161eeda 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 maxValueReached = (context, event) => {
return context.counter >= 10;
};
const incrementCounter = (context, event) => {
context.counter += 1;
}
const resetCounter = (context, event) => {
context.counter = 0;
}
const fetchMachine = Machine({
context: {
counter: 0
},
initial: 'count',
states: {
count: {
on: {
'': { target: 'reset', cond: 'maxValueReached' }
},
after: {
200: { target: 'count', actions: 'incrementCounter' }
}
},
reset: {
entry: 'resetCounter',
on: {
'': { target: 'count', }
}
}
}
}
,
{
guards: { maxValueReached },
actions: { incrementCounter, resetCounter }
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment