From: Andrew Gundersen Date: Tue, 29 Jun 2021 16:30:35 +0000 (-0500) Subject: working protocols X-Git-Tag: v2.0.1~11^2~2 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=a791f973dcc3ff4c511412c82b460919019be289;p=mime-chat working protocols --- diff --git a/src/account.ts b/src/account.ts index ac53b23..965502c 100644 --- a/src/account.ts +++ b/src/account.ts @@ -1,10 +1,9 @@ import { postAuth, postLogin, postLogout } from "@/api/account"; import { endSession, launchSession } from "@/session"; -import { getToken, setToken, setProfile, getProfile, clearStore } from "./store"; +import { getToken, setToken, setProfile, clearStore } from "./store"; import { parseAuthRes } from "./auth"; import { ipcEmit } from "@/composables/useEmitter"; -import { messages } from "@/messages"; export const accountAuth = async (): Promise => { @@ -84,13 +83,4 @@ export const accountLogout = async (): Promise => { } -export const updateAppState = (): void => { - - const profile = getProfile(); - - ipcEmit("set-profile", profile); - - ipcEmit("init-messages", messages) - -} diff --git a/src/ipc/listeners.ts b/src/ipc/listeners.ts index cb17f65..17af103 100644 --- a/src/ipc/listeners.ts +++ b/src/ipc/listeners.ts @@ -2,7 +2,7 @@ import { IpcListener } from "@/composables/useIpcMain" import { sendMessage } from '@/session'; import { collect } from "@/audio"; -import { updateAppState } from "@/account"; +import { updateAppState } from "@/session"; import { onNavBar } from "@/window"; const CLIENT_MESSAGE_CHANNEL = "post-session-send" diff --git a/src/main.ts b/src/main.ts index a360835..6df9f6c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,8 +10,8 @@ */ import initIpcMain from "@/ipc/index"; -import { accountAuth, updateAppState } from "./account"; -import { launchSession } from "./session"; +import { accountAuth } from "./account"; +import { launchSession, updateAppState } from "./session"; import createWindow from "./window"; let authState: AuthState | null; diff --git a/src/messages.ts b/src/messages.ts index fa2ab22..9a72b38 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -1,40 +1,67 @@ import { ipcEmit } from "@/composables/useEmitter"; -export let messages: Message[] = []; +export default class Messages { -export const setMessages = (init: {messages: Message[]}) => { - console.log("setMessages", init); - messages = init.messages; - ipcEmit("init-messages", messages); -} + messages: Message[]; -export const newMessage = (message: Message) => { - console.log("newMessage", message); - messages.push(message); - ipcEmit("add-message", message); -} + constructor(messages: Message[]) { + console.log(messages); + this.messages = messages; + this.emit(); + } -export const annotateMessage = (annotation: Annotation) => { - console.log("annotateMessage", annotation); + emit() { + ipcEmit("init-messages", this.messages); + } - for (let i in messages) { + update(update: Update) { - if (messages[i].uid == annotation.uid) { + if (update.name === "add") { + this.messages.push(update.data as Message); + ipcEmit("add-message", update.data); + } - if (annotation.data.hasOwnProperty("content")) { - const data = annotation.data as ContentData; - messages[i].content = data.content; - } + else if (update.name === "annotate") { + this._annotate(update.data as Annotation); + ipcEmit("update-message", update.data); + } - if (annotation.data.hasOwnProperty("context")) { - const data = annotation.data as ContextData; - messages[i].context = data.context; - } + else { + this._delete(update.data as string); + ipcEmit("delete-message", update.data); + } + + } + + _annotate(annotation: Annotation) { + + for (let i in this.messages) { - ipcEmit("update-message", annotation); - break; + if (this.messages[i].uid == annotation.uid) { + + if (annotation.name == "content") { + this.messages[i].content = annotation.data as string[]; + } + + if (annotation.name == "context") { + this.messages[i].context = annotation.data as string; + } + + ipcEmit("update-message", annotation); + + } } + } -} + _delete(uid: string) { + for (var i = 0; i < this.messages.length; i++) { + if (this.messages[i].uid === uid) { + this.messages.splice(i, 1); + break; + } + } + } + +} \ No newline at end of file diff --git a/src/render/listeners.ts b/src/render/listeners.ts index 90b3cc4..e6ada7f 100644 --- a/src/render/listeners.ts +++ b/src/render/listeners.ts @@ -4,7 +4,7 @@ import { IpcRendererListener } from "./composables/useIpcRend" * Shared state imports * */ import { setProfile } from "./shared/profile"; -import { setMessages, addMessage, updateMessage } from "./shared/messages"; +import { setMessages, addMessage, updateMessage, deleteMessage } from "./shared/messages"; import { setConnectionStatus } from "./shared/connectionStatus"; const SET_PROFILE_CHANNEL = "set-profile"; @@ -12,6 +12,7 @@ const SET_PROFILE_CHANNEL = "set-profile"; const INIT_MESSAGES_CHANNEL = "init-messages"; const ADD_MESSAGE_CHANNEL = "add-message"; const UPDATE_MESSAGE_CHANNEL = "update-message"; +const DELETE_MESSAGE_CHANNEL = "delete-message"; const CONNECTION_STATUS_CHANNEL = "set-connection-status"; export const setProfileListener = new IpcRendererListener({ @@ -35,6 +36,11 @@ export const updateMessagesListener = new IpcRendererListener({ listenerCallback: updateMessage }); +export const deleteMessagesListener = new IpcRendererListener({ + channel: DELETE_MESSAGE_CHANNEL, + listenerCallback: deleteMessage +}); + export const connectionStatusListener = new IpcRendererListener({ channel: CONNECTION_STATUS_CHANNEL, listenerCallback: setConnectionStatus diff --git a/src/render/shared/messages.ts b/src/render/shared/messages.ts index 79b8fe7..b24dca3 100644 --- a/src/render/shared/messages.ts +++ b/src/render/shared/messages.ts @@ -5,55 +5,46 @@ import useScroll from "@/render/composables/useScroll"; export const messages: Ref> = ref([]); export const setMessages: IpcListenerCallback> = (payload) => { - console.log("setMessages", payload); messages.value = payload as Array; } export const addMessage: IpcListenerCallback = (payload) => { - console.log("addMessage", payload); messages.value.push(payload as Message); } export const updateMessage: IpcListenerCallback = (payload) => { - console.log("updateMessage", payload); const annotation = payload as Annotation; - /* find the target message */ - let targetMessage = messages.value.filter((m: Message) => { - return m.uid === annotation.uid; - })[0]; + for (let i in messages.value) { - if (targetMessage) { + if (messages.value[i].uid == annotation.uid) { + + if (annotation.name == "content") { + messages.value[i].content = annotation.data as string[]; + } + + if (annotation.name == "context") { + messages.value[i].context = annotation.data as string; + } - /* update the content (add a new bubble) */ - if (annotation.data.hasOwnProperty("content")) { - const data = annotation.data as ContentData; - targetMessage.content = data.content; } - /* and/or update the context */ - if (annotation.data.hasOwnProperty("context")) { - const data = annotation.data as ContextData; - targetMessage.context = data.context; + } + +} + +export const deleteMessage: IpcListenerCallback = (payload) => { + const uid = payload as string; + + for (var i = 0; i < messages.value.length; i++) { + if (messages.value[i].uid === uid) { + messages.value.splice(i, 1); + break; } - } } -// messages.value = [ -// { -// content: ["Welcome Andrew"], -// context: "Crimata", -// modifier: "ai", -// uid: "b5dd3d1b-cef0-4057-aa0f-c72e5c4cd67d" -// }, -// { -// content: ["show contacts"], -// context: "false", -// modifier: "client", -// uid: "ee2819a3-1125-4881-9942-ca5b3d0ad502" -// }] // setTimeout(() => { diff --git a/src/session.ts b/src/session.ts index 885d940..6b40e43 100644 --- a/src/session.ts +++ b/src/session.ts @@ -3,52 +3,34 @@ // import useAudio from "@/audio"; -import { setMessages, newMessage, annotateMessage } from "./messages"; +import Messages from "./messages"; +import { getProfile } from "./store"; import useWebsockets from "./composables/useWebsockets"; import {config} from "@/config"; +import { ipcEmit } from "@/composables/useEmitter"; /* start and stop audio functionality */ // const { initAudio, closeAudio } = useAudio(); -const isInitMessage = (object: any): object is Message[] => { - return 'messages' in object; -}; +let messages: Messages; -const isNewMessage = (object: any): object is Message => { - return 'content' in object; -}; - -const isAnnotation = (object: any): object is Annotation => { - return 'uid' in object; -}; - -const deauthenticate = (): void => { - console.log('deauthenticating') -}; - -/** - * Controls for interfacing with the platform. - * Takes an onMessage callback which we define below. - */ const onMessageCallback = (payload: string) => { const message = JSON.parse(payload); if (message === "CLOSE_AUTH_FAIL") { - deauthenticate(); + console.log("deauthenticating"); } - else if (isInitMessage(message)) { - setMessages(message); + else if (message.header === "init") { + messages = new Messages(message.body); } - else if (isNewMessage(message)) { - newMessage(message); - } - - else if (isAnnotation(message)) { - annotateMessage(message); + else { + if (messages) { + messages.update(message.body); + } } } @@ -57,6 +39,17 @@ const connectionStatusCallback = (status: string) => { ipcEmit('set-connection-status', status); } + +export const updateAppState = (): void => { + const profile = getProfile(); + + ipcEmit("set-profile", profile); + + if (messages) + messages.emit(); + +} + const statusOptions = { openMessage: "Connected", pongMessage: "Nominal", diff --git a/src/types.ts b/src/types.ts index a0cbb70..bcadf73 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,9 @@ +interface Update { + name: string; + data: Message[] | Message | Annotation | string; +} + interface Message { modifier: string; content: string[]; @@ -6,16 +11,9 @@ interface Message { uid: string; } -interface ContentData { - content: string[]; -} - -interface ContextData { - context: string; -} - interface Annotation { - data: ContentData | ContextData; + name: string; + data: string | string[]; uid: string; }