Skip to content

Instantly share code, notes, and snippets.

@sergiokopplin
Last active July 28, 2020 17:54
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 sergiokopplin/144ef910946b105e8bc333e271638780 to your computer and use it in GitHub Desktop.
Save sergiokopplin/144ef910946b105e8bc333e271638780 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const importerJobMachine = Machine(
{
id: 'importerJob',
initial: 'idle',
context: {
data: {
status: {},
command: {},
report: {},
},
error: {},
retries: 0,
maxRetries: 5,
shouldRetry: true,
},
states: {
idle: {
on: {
start: 'command',
},
},
command: {
initial: 'running',
states: {
running: {
invoke: {
id: 'sendJob',
src: 'sendJob',
onDone: {
target: 'success',
actions: 'setData',
},
onError: {
target: 'failure',
actions: 'setError',
},
},
},
success: {
on: {
done: '#searching',
},
},
failure: {
type: 'final',
},
},
},
searching: {
id: 'searching',
initial: 'running',
states: {
running: {
invoke: {
id: 'searchJob',
src: 'searchJob',
onDone: {
target: 'success',
actions: 'setData',
},
onError: {
target: 'failure',
actions: 'setError',
},
},
},
success: {
on: {
'': [
{
target: 'failure',
cond: 'canNotRetry',
},
{
target: 'running',
cond: 'canRetry',
},
{
target: 'success',
cond: 'doneRetry',
},
],
retry: {
target: 'running',
actions: 'incrementRetries',
},
success: '#report',
},
},
failure: {
type: 'final',
},
},
},
report: {
type: 'final',
id: 'report',
},
},
},
{
actions: {
setData: () => ({}),
setError: () => ({}),
incrementRetries: assign({
retries: ({ retries }) => retries + 1,
}),
toggleRetries: assign({ shouldRetry: ({ shouldRetry }) => !shouldRetry }),
},
guards: {
canNotRetry: ({ retries, maxRetries }) => retries === maxRetries,
canRetry: () => {
console.log('business rule here');
},
doneRetry: () => {
console.log('business rule here');
},
},
services: {
sendJob: () => ({}),
searchJob: () => ({}),
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment