Skip to content

Instantly share code, notes, and snippets.

@herrfugbaum
Last active April 25, 2020 07:41
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 herrfugbaum/2c837af6a8c1d2fcc71ed78bdaa38b3c to your computer and use it in GitHub Desktop.
Save herrfugbaum/2c837af6a8c1d2fcc71ed78bdaa38b3c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const replMachine = Machine({
id: 'replMachine',
initial: 'repl',
context: {
cursor: 0,
data: [1,2,3,4,5]
},
states: {
repl: {
on: {
ENTER_KEY: 'parsing'
}
},
parsing: {
on: {
SUCCESS: 'readMore',
ERROR: 'error'
}
},
readMore: {
on: {
SPACE_KEY: [
{
target: 'readMore',
cond: 'notOnLastPage',
actions: ['cursorIncrement']
},
{
target: 'repl',
actions: ['cursorReset']
}
],
readAll: {
target: 'repl',
actions: ['cursorReset']
}
}
},
error: {
on: {
'': {
target: 'repl',
actions: ['printError']
}
}
}
}
},
{
actions: {
cursorIncrement: assign({
cursor: context => context.cursor + 1,
}),
cursorReset : assign({
cursor: context => context.cursor = 0,
}),
printError: () => console.log('Some Error occured!')
},
guards: {
'notOnLastPage': (context,event) => context.cursor < context.data.length
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment