Skip to content

Instantly share code, notes, and snippets.

@nrgapple
Created February 12, 2020 17:31
Show Gist options
  • Save nrgapple/3dd08325bda5a40b58fe107dbad9aabe to your computer and use it in GitHub Desktop.
Save nrgapple/3dd08325bda5a40b58fe107dbad9aabe to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const chatMachine = Machine({
id: 'chat-app',
context: {
},
initial: 'init',
states: {
init: {
initial: 'wait',
states: {
wait: {
on: {
DEPENDENCIES_LOADED: {
target: 'fetchMessages',
}
}
},
fetchMessages: {
initial: 'loadMessages',
states: {
loadMessages: {
invoke: {
src: 'loadMessages',
onDone: 'getUnreadMessages',
},
},
getUnreadMessages: {
entry: ['getUnreadMessages'],
on: {
SUCCESS: {
target: 'finish'
}
}
},
finish: {
type: 'final',
}
},
onDone: 'finish',
},
finish: {
type: 'final',
}
},
onDone: 'subscribe',
},
subscribe: {
type: 'parallel',
states: {
subToChat: {
initial: 'start',
states: {
start: {
entry: ['subToChat'],
on: {
SUB_CHAT_SUCCESS: 'success',
},
},
success: { type: 'final' },
}
},
subToTyping: {
initial: 'start',
states: {
start: {
entry: ['subToTyping'],
on: {
SUB_TYPING_SUCCESS: 'success',
},
},
success: { type: 'final' },
}
},
subToRead: {
initial: 'start',
states: {
start: {
entry: ['subToRead'],
on: {
SUB_READ_SUCCESS: 'success',
},
},
success: { type: 'final' },
}
},
},
onDone: 'ready'
},
ready:
{
type: 'parallel',
states: {
user: {
id: 'user',
initial: 'idle',
states: {
idle: {
on: {
USER_TYPED: {
target: 'typing',
actions: ['sendTyping'],
},
USER_SENT: {
target: 'sendMessage',
}
}
},
sendMessage: {
entry: ['sendMessage'],
on: {
USER_SENT: {
target: 'idle',
actions: ['clearInput'],
}
}
},
typing: {
entry: ['resetTypingTimout'],
on: {
USER_SENT: {
target: 'sendMessage',
actions: ['stopTyping'],
},
USER_TYPED: {
target: 'typing',
},
USER_STOPPED: {
target: 'idle',
actions: ['stopTyping'],
},
},
},
},
},
recipientTyping: {
initial: 'idle',
states: {
idle: {
on: {
REC_TYPED: {
target: 'typing',
actions: ['scrollToTheBottom'],
}
}
},
typing: {
on: {
REC_STOPPED: {
target: 'idle',
}
}
},
}
},
recipientMessages: {
initial: 'idle',
states: {
idle: {
on: {
REC_INCOMING_MSG: {
target: 'incomingMessage',
}
}
},
incomingMessage: {
entry: ['sendRead'],
on: {
REC_UPDATED: {
target: 'idle',
}
}
},
}
},
read: {
id: 'read',
initial: 'idle',
states: {
idle: {
on: {
REC_READ: {
target: 'updatingLastRead',
}
}
},
updatingLastRead: {
entry: ['updateLastRead'],
on: {
READ_UPDATE_SUCCESS: {
target: 'idle',
}
}
},
}
}
}
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment