Skip to content

Instantly share code, notes, and snippets.

@kdgerona
Last active June 18, 2021 23:57
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 kdgerona/a7c090b6bd1aa58a2757f3f165876813 to your computer and use it in GitHub Desktop.
Save kdgerona/a7c090b6bd1aa58a2757f3f165876813 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const context = {
application_config: {
max_attempts: 5,
},
application_data: {
attempts: 0,
},
};
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;
},
}
}
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_details',
},
FAILED: {
target: 'error',
},
},
},
loading_member_details: {
invoke: [
{
id: 'load-member-details',
src: 'loadMemberDetails',
},
],
on: {
SUCCESS: {
actions: ['assignMemberDetails'],
target: 'done',
},
FAILED: {
target: 'error',
},
},
},
error: {
on: {
'': [
{
cond: 'hasReachedMaxAttempts',
target: 'timeout',
},
{
target: 'get_config',
actions: ['incrementAttempts'],
},
]
}
},
timeout: {
type: 'final',
},
done: {
type: 'final',
},
},
}
Machine(config, implementations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment