Created
June 19, 2021 01:19
-
-
Save kdgerona/d05712c0784ca5753c49985fafa62318 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 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: 'load_messages', | |
}, | |
FAILED: { | |
target: 'error', | |
}, | |
}, | |
}, | |
load_messages: { | |
entry: ['logLoadMessages'], | |
invoke: [ | |
{ | |
id: 'load-messages', | |
src: 'loadMessages', | |
}, | |
], | |
on: { | |
SUCCESS: { | |
actions: ['assignMessages'], | |
target: 'conversation', | |
}, | |
FAILED: { | |
actions: ['sendErrorToParent'], | |
target: 'conversation', | |
}, | |
}, | |
}, | |
conversation: { | |
initial: 'disconnected', | |
invoke: [ | |
{ | |
id: 'connect-to-chat-endpoint', | |
src: 'connectToChatEndpoint', | |
}, | |
], | |
states: { | |
disconnected: { | |
entry: ['logWaitingForConnection'], | |
on: { | |
CHAT_SOCKET_CONNECTED: { | |
actions: ['logEvent'], | |
target: 'connected', | |
}, | |
}, | |
}, | |
connected: { | |
entry: ['logReady'], | |
on: { | |
CHAT_SOCKET_DISCONNECTED: { | |
actions: ['logEvent'], | |
target: 'disconnected', | |
}, | |
SEND_MESSAGE: { | |
actions: ['logEvent', 'sendEventToChatService'], | |
}, | |
MESSAGE: { | |
actions: ['logEvent', 'upsertToMessages'], | |
}, | |
}, | |
}, | |
}, | |
}, | |
error: { | |
id: 'error', | |
on: { | |
'':[ | |
{ | |
cond: 'hasReachedMaxAttempts', | |
target: 'timeout', | |
}, | |
{ | |
target: 'get_config', | |
actions: ['incrementAttempts'], | |
}, | |
] | |
} | |
}, | |
timeout: { | |
id: 'timeout', | |
type: 'final', | |
}, | |
done: { | |
id: 'done', | |
type: 'final', | |
}, | |
}, | |
} | |
Machine(config, implementations) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment