Skip to content

Instantly share code, notes, and snippets.

@kdgerona
Last active June 19, 2021 05:36
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/6bfc7264201d00b885cafa17192943e9 to your computer and use it in GitHub Desktop.
Save kdgerona/6bfc7264201d00b885cafa17192943e9 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 = {
services: {
getConfig: () => (send) => {
const success_event= {
type: 'SUCCESS',
payload: {
application_config: {
max_attempts: 5,
atomic_apps: [
{
component_name: 'ConversationGrid',
component_type: 'menu',
config: {
workflow_type: 'CONVERSATION_LIST',
},
},
{
component_name: 'AgentChat',
component_type: 'agent_chat',
config: {
workflow_type: 'AGENT_CHAT',
},
},
{
component_name: 'MemberDetails',
component_type: 'member_details',
config: {
workflow_type: 'MEMBER_DETAILS',
},
},
{
component_name: 'ReservationGrid',
component_type: 'reservation_grid',
config: {
workflow_type: 'RESERVATION_GRID',
},
},
],
},
application_data: {
attempts: 0,
},
},
};
setTimeout(() => {
send(success_event);
}, 1000);
},
},
actions: {
incrementAttempts: assign({
application_data: ({ application_data }) => {
const { attempts } = application_data;
return {
...application_data,
attempts: attempts + 1,
};
},
}),
assignConfigs: assign({
application_config: ({ application_config }, { payload }) => {
return {
...application_config,
...payload,
};
},
}),
setCurrentSelected: assign({
application_data: ({ application_data }, { payload }) => {
return {
...application_data,
currently_selected_id: payload,
};
},
}),
},
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: 'ready',
},
FAILED: {
target: 'error',
},
},
},
ready: {
on: {
SET_CURRENT_SELECTED: {
actions: ['setCurrentSelected'],
},
},
},
error: {
on: {
'': [
{
cond: 'hasReachedMaxAttempts',
target: 'timeout',
},
{
actions: ['incrementAttempts'],
target: 'get_config',
},
]
},
},
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