Last active
July 28, 2020 17:54
-
-
Save sergiokopplin/144ef910946b105e8bc333e271638780 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 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