Last active
June 18, 2021 23:57
-
-
Save kdgerona/ae47f91dad2ea63fb2cb40f352fc086c 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 context = { | |
application_config: { | |
max_attempts: 5, | |
}, | |
application_data: { | |
attempts: 0, | |
}, | |
}; | |
const config = { | |
initial: 'get_config', | |
context, | |
on: { | |
COMPONENT_UNMOUNT: { | |
target: 'done', | |
}, | |
}, | |
states: { | |
get_config: { | |
invoke: [ | |
{ | |
id: 'get-config', | |
src: 'getConfig', | |
}, | |
], | |
on: { | |
SUCCESS: { | |
actions: ['assignConfigs'], | |
target: 'loading_member_reservation', | |
}, | |
FAILED: { | |
target: 'error', | |
}, | |
}, | |
}, | |
loading_member_reservation: { | |
invoke: [ | |
{ | |
id: 'load-member-reservation', | |
src: 'loadMemberReservation', | |
}, | |
], | |
on: { | |
SUCCESS: { | |
actions: ['assignMemberReservation'], | |
target: 'done', | |
}, | |
FAILED: { | |
actions: ['sendErrorToParent'], | |
target: 'error', | |
}, | |
}, | |
}, | |
error: { | |
on: { | |
'': [ | |
{ | |
cond: 'hasReachedMaxAttempts', | |
target: 'timeout', | |
}, | |
{ | |
target: 'get_config', | |
actions: ['incrementAttempts'], | |
}, | |
] | |
} | |
}, | |
timeout: { | |
type: 'final', | |
}, | |
done: { | |
type: 'final', | |
}, | |
}, | |
} | |
const implementations = { | |
actions: { | |
incrementAttempts: assign({ | |
application_data: ({ application_data }) => { | |
const { attempts } = application_data; | |
return { | |
...application_data, | |
attempts: attempts + 1, | |
}; | |
}, | |
}), | |
}, | |
guards: { | |
hasReachedMaxAttempts: ({ application_config, application_data }) => { | |
const { max_attempts } = application_config; | |
const { attempts } = application_data; | |
return attempts >= max_attempts; | |
}, | |
} | |
} | |
Machine(config, implementations); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment