Created
August 30, 2020 02:45
-
-
Save sergiokopplin/fbd49b798cc9eb74b53026c0c1e6fd33 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
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
const JOB_GUARDS = { | |
CAN_NOT_RETRY: 'canNotRetry', | |
SHOULD_RETRY: 'shouldRetry', | |
DONE_RETRY: 'doneRetry', | |
}; | |
const jobGuards = { | |
[JOB_GUARDS.CAN_NOT_RETRY]: ({ maxRetries, retries, status }) => | |
status.status === 'Error' || retries === maxRetries, | |
[JOB_GUARDS.SHOULD_RETRY]: ({ status }) => status.status === 'Running', | |
[JOB_GUARDS.DONE_RETRY]: ({ status }) => status.status === 'Success', | |
}; | |
const JOB_SERVICES = { | |
SEND_JOB: 'sendJob', | |
GET_STATUS: 'getStatus', | |
GET_REPORT: 'getReport', | |
}; | |
const servicesMapper = { | |
job: { | |
['transaction']: '/api/import-transaction', | |
['registration']: '/api/import-registration-number', | |
}, | |
status: '/api/import-job-status', | |
report: '/api/import-report', | |
}; | |
const jobServices = { | |
[JOB_SERVICES.SEND_JOB]: () => ({}), | |
[JOB_SERVICES.GET_STATUS]: () => ({}), | |
[JOB_SERVICES.GET_REPORT]: () => ({}), | |
}; | |
const JOB_ACTIONS = { | |
SET_COMMAND_DATA: 'setCommandData', | |
SET_STATUS_DATA: 'setStatusData', | |
SET_REPORT_DATA: 'setReportData', | |
SET_ERROR: 'setError', | |
HYDRATE_DATA: 'hydrateData', | |
INCREMENT_RETRIES: 'incrementRetries', | |
}; | |
const jobActions = { | |
[JOB_ACTIONS.SET_COMMAND_DATA]: assign({ | |
command: (_context, event) => event.data.data, | |
}), | |
[JOB_ACTIONS.SET_STATUS_DATA]: assign({ | |
status: (_context, event) => event.data.data, | |
}), | |
[JOB_ACTIONS.SET_REPORT_DATA]: assign({ | |
report: (_context, event) => event.data.data, | |
}), | |
[JOB_ACTIONS.SET_ERROR]: assign({ | |
error: (_context, event) => event.data, | |
}), | |
[JOB_ACTIONS.HYDRATE_DATA]: assign({ | |
params: (_context, event) => event.params, | |
}), | |
[JOB_ACTIONS.INCREMENT_RETRIES]: assign({ | |
retries: ({ retries }) => retries + 1, | |
}), | |
}; | |
const JOB_STATES = { | |
COMMAND: 'command', | |
STATUS: 'status', | |
REPORT: 'report', | |
IDLE: 'idle', | |
FAILURE: 'failure', | |
RUNNING: 'running', | |
SUCCESS: 'success', | |
}; | |
const JOB_TRANSITIONS = { | |
START: 'commandStart', | |
}; | |
const jobMachine = Machine({ | |
id: 'importerJob', | |
initial: JOB_STATES.IDLE, | |
context: { | |
params: {}, | |
command: {}, | |
status: {}, | |
report: {}, | |
error: {}, | |
retries: 0, | |
maxRetries: 50, | |
delay: 5000, | |
}, | |
states: { | |
[JOB_STATES.IDLE]: { | |
on: { | |
[JOB_TRANSITIONS.START]: { | |
target: JOB_STATES.COMMAND, | |
actions: JOB_ACTIONS.HYDRATE_DATA, | |
}, | |
}, | |
}, | |
[JOB_STATES.COMMAND]: { | |
id: JOB_STATES.COMMAND, | |
initial: JOB_STATES.RUNNING, | |
states: { | |
[JOB_STATES.RUNNING]: { | |
invoke: { | |
id: JOB_SERVICES.SEND_JOB, | |
src: JOB_SERVICES.SEND_JOB, | |
onDone: { | |
target: `#${JOB_STATES.STATUS}`, | |
actions: JOB_ACTIONS.SET_COMMAND_DATA, | |
}, | |
onError: { | |
target: `#${JOB_STATES.FAILURE}`, | |
actions: JOB_ACTIONS.SET_ERROR, | |
}, | |
}, | |
}, | |
}, | |
}, | |
[JOB_STATES.STATUS]: { | |
id: JOB_STATES.STATUS, | |
initial: JOB_STATES.RUNNING, | |
states: { | |
[JOB_STATES.RUNNING]: { | |
invoke: { | |
id: JOB_SERVICES.GET_STATUS, | |
src: JOB_SERVICES.GET_STATUS, | |
onDone: { | |
target: JOB_STATES.SUCCESS, | |
actions: JOB_ACTIONS.SET_STATUS_DATA, | |
}, | |
onError: { | |
target: `#${JOB_STATES.FAILURE}`, | |
actions: JOB_ACTIONS.SET_ERROR, | |
}, | |
}, | |
}, | |
[JOB_STATES.SUCCESS]: { | |
always: [ | |
{ | |
target: `#${JOB_STATES.FAILURE}`, | |
cond: JOB_GUARDS.CAN_NOT_RETRY, | |
}, | |
{ | |
target: `#${JOB_STATES.REPORT}`, | |
cond: JOB_GUARDS.DONE_RETRY, | |
}, | |
{ | |
target: JOB_STATES.RUNNING, | |
actions: JOB_ACTIONS.INCREMENT_RETRIES, | |
cond: JOB_GUARDS.SHOULD_RETRY, | |
}, | |
], | |
}, | |
}, | |
}, | |
[JOB_STATES.REPORT]: { | |
id: JOB_STATES.REPORT, | |
initial: JOB_STATES.RUNNING, | |
states: { | |
[JOB_STATES.RUNNING]: { | |
invoke: { | |
id: JOB_SERVICES.GET_REPORT, | |
src: JOB_SERVICES.GET_REPORT, | |
onDone: { | |
target: JOB_STATES.SUCCESS, | |
actions: JOB_ACTIONS.SET_REPORT_DATA, | |
}, | |
onError: { | |
target: `#${JOB_STATES.FAILURE}`, | |
actions: JOB_ACTIONS.SET_ERROR, | |
}, | |
}, | |
}, | |
[JOB_STATES.SUCCESS]: { | |
type: 'final', | |
}, | |
}, | |
}, | |
[JOB_STATES.FAILURE]: { | |
id: JOB_STATES.FAILURE, | |
type: 'final', | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment