Skip to content

Instantly share code, notes, and snippets.

@ahamid
Created January 20, 2022 21:20
Show Gist options
  • Save ahamid/cb72b4656ce8f440b2f1704b4e33a99b to your computer and use it in GitHub Desktop.
Save ahamid/cb72b4656ce8f440b2f1704b4e33a99b 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 fetchMachine = Machine({
id: 'fetch',
initial: 'idle',
context: {
retries: 0
},
states: {
idle: {
on: {
FETCH: {
// actions: ['catchMe'],
target: 'loading'
},
'xstate.error': {
target: 'internal_error'
},
'error.execution': {
target: 'internal_error'
}
}
},
loading: {
entry: ['catchMe2'],
on: {
RESOLVE: 'success',
REJECT: 'failure',
'xstate.error': {
target: 'internal_error'
},
'error.execution': {
target: 'internal_error'
}
}
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: {
target: 'loading',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
},
internal_error: {
type: 'final'
}
}
}, {
actions: {
catchMe: () => {
throw new Error('event: catch me if you can xD')
},
catchMe2: () => {
throw new Error('entry: catch me if you can xD')
}
}
});
// const interpreter = interpret(fetchMachine);
// console.log('fetch action', interpreter.send('FETCH'));
// console.log('state', interpreter.state);
// setTimeout(() => {
// console.log('state', interpreter.state);
// }, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment