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/734e42c87dfc2bb2d34aae1283efbff5 to your computer and use it in GitHub Desktop.
Save kdgerona/734e42c87dfc2bb2d34aae1283efbff5 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 config = {
initial: 'get_config',
context,
on: {
COMPONENT_UNMOUNT: {
target: 'done',
},
},
states: {
get_config: {
invoke: [
{
id: 'get-config',
src: 'getConfig',
},
],
on: {
SUCCESS: {
actions: ['assignConfigs'],
target: 'identify_agent',
},
FAILED: {
target: 'error',
},
},
},
identify_agent: {
entry: ['logIdentifyingAgent'],
invoke: [
{
id: 'identify-agent',
src: 'identifyAgent',
},
],
on: {
SUCCESS: {
actions: ['assignAgent'],
target: 'load_messages',
},
FAILED: {
actions: ['sendErrorToParent'],
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',
},
},
}
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