]> Repos - mime-chat/commitdiff
remove user-message & agen-message event listeners
authorriqo <hernandeze2@xavier.edu>
Wed, 24 Feb 2021 17:42:05 +0000 (11:42 -0600)
committerriqo <hernandeze2@xavier.edu>
Wed, 24 Feb 2021 17:42:05 +0000 (11:42 -0600)
src/background/session.ts
src/components/messageView/index.vue
src/components/messageView/viewDetails/transitions.ts
src/components/taInput/inputController.ts
src/components/taInput/textDetails/textVisualizer.ts
src/components/textBubble/bubbleDetails/computed.ts
src/modules/auth.ts
src/modules/ipc.ts
src/preload.ts
src/views/login.vue
src/views/register.vue

index 452695e031990f26ce7590a841ec3dce6f51f26f..ccf36e31afa759754827ca910031bc0a497f5d19 100644 (file)
@@ -24,7 +24,7 @@ interface TextMessage {
 }
 
 const ip = 'ws://127.0.0.1';
-const port = 8760;
+const port = 8081;
 const reconnectTimeout = 3000; //ms
 let socket: WebSocket;
 let success = false;
@@ -66,46 +66,19 @@ const receiveMessage = (message: string): void => {
     }
 
     const parsed: RenderMessage = JSON.parse(message);
-    if (parsed.modifier === "sf") {
-        backgroundMitt.emit('message-res', parsed);
-    } else if (parsed.modifier === "ai") {
-        backgroundMitt.emit('ipc-renderer', {
-            endpoint: 'agent-message',
-            message: parsed
-        });
-    }
+    backgroundMitt.emit('ipc-renderer', {
+        endpoint: 'render-message',
+        message: parsed
+    });
 
 };
 
-const asyncSend = (_event?, payload: TextMessage | Buffer): Promise<RenderMessage> => {
-    return new Promise((resolve, reject) => {
-
-        backgroundMitt.once('message-res', (res: RenderMessage) => {
-            if (res.content) {
-                resolve(res);
-            } else {
-                reject('ERROR: NO MESSAGE');
-            }
-
-        });
-
-        if (payload instanceof Buffer) {
-            socket.send(payload);
-        } else if (payload) {
-            socket.send(JSON.stringify(payload));
-        }
-    });
-
+const sendMessage = (_event, payload: TextMessage): void => {
+    socket.send(JSON.stringify(payload));
 }
 
-export const sendAudio = async (content: Buffer) => {
-
-    const res = await asyncSend(null, content);
-
-    backgroundMitt.emit('ipc-renderer', {
-        endpoint: 'agent-message',
-        message: res
-    });
+export const sendAudio = (content: Buffer) => {
+    socket.send(content);
 }
 
 // Run every time we want to connect to backend.
@@ -127,9 +100,9 @@ export const initSession = () => {
     ipcMain.removeHandler('auth-session'); // avoid setting duplicate handlers
     ipcMain.handle('auth-session', authSession);
 
-    // handle renderer send-message event
-    ipcMain.removeHandler('message'); // avoid setting duplicate handlers
-    ipcMain.handle('message', asyncSend);
+    // handle user message event
+    ipcMain.removeAllListeners('message');
+    ipcMain.on('message', sendMessage);
 
     // Connect to the backend.
     socket.on('open', () => {
index e1a35d6310099187cc4ead68df9cb77a38162614..75f1b01a9d72111cc1dcc55702899e5429ad0f77 100644 (file)
@@ -24,7 +24,6 @@
 <script lang="ts">
 import TextBubble from "@/components/textBubble/index.vue";
 import { Message } from "@/types/message/index";
-import { useIpc } from "@/modules/ipc";
 import { Queue } from "@/modules/queue";
 import useTransitions from "./viewDetails/transitions";
 import useScrollView from "@/modules/scrollView";
@@ -40,8 +39,6 @@ export default defineComponent({
     const renderArr: Message[] = reactive([]);
     const renderQ = new Queue();
 
-    const { invoke } = useIpc("message");
-
     // Each method will be called on their respective lifecycle event.
     const { beforeEnter, enter, afterEnter, rendering } = useTransitions();
     const { scrollView } = useScrollView({
@@ -64,20 +61,11 @@ export default defineComponent({
     });
 
     onMounted(() => {
-      // Call render function on event "agent-message."
-      window.ipcRenderer.on("agent-message", (event, payload) => {
+      window.ipcRenderer.on("render-message", (event, payload) => {
         const m = payload.message;
         if (m.content) render(m);
       });
 
-      emitter.on("user-message", async (message) => {
-        try {
-          const res = await invoke(message);
-          render(res);
-        } catch (e) {
-          console.log("No response. Do not render.");
-        }
-      });
     });
 
     onUnmounted(() => {
index 493725ebf5246d0582317f55bf97f422b53c52c1..bd3ce6dc658eacab9e0bb9cf9dec70dcce7785d1 100644 (file)
@@ -1,10 +1,10 @@
 /**
  * Do stuff before, on, and after message enters the view.
- * Rendering Process consists of the execution of the above 
- * mentioned callbacks followed by the shift or stack animation. 
- * The animation called depends on the height of the message view 
+ * Rendering Process consists of the execution of the above
+ * mentioned callbacks followed by the shift or stack animation.
+ * The animation called depends on the height of the message view
  * relative to the window height. Additionally, the animation will
- * always be called on the after enter callback, after all the neccesary 
+ * always be called on the after enter callback, after all the neccesary
  * calculations have been completed. While all of this is hapenning,
  * the rendering state is updated throughout the process.
  */
index 20bf438610ed7d23d45be17c9f9874e650ba6ff9..1f79bb21ddf5d325e5f5924f2b5aa771969ed4a6 100644 (file)
@@ -20,7 +20,7 @@ export default function useInputController() {
     const paths = ref([]);
     const audioBubbleWidth = ref(10);
     const { emitter } = useMitt();
-    const { post } = useIpc('update-recorder');
+    const { post } = useIpc();
 
     // send message on ENTER
     const enterCallback = (input: string) => {
@@ -29,27 +29,32 @@ export default function useInputController() {
             audio: 0,
             text: input
         };
-        emitter.emit("user-message", message);
+
+        // trigger send animation
+        emitter.emit("animate-send");
+
+        // send message to backend
+        post('message', message);
     }
 
-    const { 
-        visualizeStreamAsPaths, 
-        expandBubble 
+    const {
+        visualizeStreamAsPaths,
+        expandBubble
     } = useAudioVisualizer(visualizeStream);
 
     const { keyDownHandler, input } = useKeyDownHandler(enterCallback);
 
-    const { 
-        textInputXoffset, 
+    const {
+        textInputXoffset,
         textInputYoffset,
-        renderTextInput 
+        renderTextInput
     } = useTextVisualizer(input);
 
     // stops stream recording on space key up
     function keyupHandler(e: any) {
         if (e.key === " " && visualizeStream.value) {
 
-            post(false);
+            post('update-recorder', false);
             visualizeStream.value = false;
             paths.value = []
             audioBubbleWidth.value = 10;
@@ -79,7 +84,7 @@ export default function useInputController() {
         }
         if (input === " " && visualizeStream.value === false) {
             visualizeStream.value = true;
-            post(true);
+            post('update-recorder', true);
             expandBubble(audioBubbleWidth);
             visualizeStreamAsPaths(stream, paths, audioBubbleWidth);
             return;
index e41da04b3a71ea7351af0e2da6903ad3b728bf5b..c0c919b2efdb247126879b09d10b2c035913d740 100644 (file)
@@ -35,7 +35,7 @@ export default function useTextVisualizer(input: Ref<string>) {
     }
 
     onMounted(() => {
-        emitter.on("user-message", () => {
+        emitter.on("animate-send", () => {
             const inputEl = document.getElementsByClassName("inputContainer");
             const foreignEl = inputEl[0] as HTMLElement;
             animateSend(foreignEl, inputHeight.value, input);
index a2e9fa2b5e7f0d9610f66178b66ee9fa59bde60d..fb660c4072dbc53f09d5839a1457cd02412c272c 100644 (file)
@@ -40,7 +40,7 @@ export default function useComputedBubble(m: string, id: string) {
     const calculateXoffset = async () => {
         switch (m) {
             case "sf":
-                x.value = 800 - messageWidth.value - selfMessageMarginRight;
+                x.value = winW - messageWidth.value - selfMessageMarginRight;
                 break;
             default:
                 x.value = paddingLeft;
index 709a262bc72a622347263dd7d9fdfb43059225cf..05bcbbe8263f8f87ddaad59fe164c570f9e986d3 100644 (file)
@@ -16,10 +16,9 @@ const AUTH_KEY = 'crimata_token';
 const token = window.localStorage.getItem(AUTH_KEY);
 
 const authToken = async () => {
-   const { data, invoke  } = useIpc('auth-session');
+   const { invoke  } = useIpc();
     try {
-        await invoke(token);
-        state.accessToken = data.value;
+        state.accessToken = await invoke('auth-session', token);
     }
     catch(e) {
         state.error = e;
index 53c4288f771a54489bb078f4d2b98e29b9854c84..f333d6a457384712919d0cf7601470fa202e0391 100644 (file)
@@ -1,28 +1,15 @@
-import { ref, computed } from 'vue'
 
-export const useIpc = (endpoint: string) => {
-    const data = ref();
-    const error = ref();
-
-    const errorMessage = computed(() => {
-        console.log('ERROR', error.value);
-        if (error.value) {
-            return error.value.message
-        }
-    })
-
-    const invoke = async (payload: any) => {
-        error.value = undefined;
+export const useIpc = () => {
+    const invoke = async (endpoint: string, payload: any) => {
         try {
             const res = await window.ipcRenderer.invoke(endpoint, payload);
-            return data.value = res;
+            return res;
         } catch (e) {
-            error.value = e;
             throw e;
         }
     }
 
-    const post = (payload: any) => {
+    const post = (endpoint: string, payload: any) => {
         window.postMessage({
             endpoint: endpoint,
             content: payload
@@ -30,9 +17,6 @@ export const useIpc = (endpoint: string) => {
     };
 
     return {
-        data,
-        error,
-        errorMessage,
         invoke,
         post
     }
index 72760c0d7709e7986ba1add63e4aac16d5bb4e22..bbc0c9a09efec6c9d61aaadde3d109cc0945c68d 100644 (file)
@@ -25,6 +25,10 @@ process.once("loaded", () => {
       ipcRenderer.send("update-recorder", message.content);
     }
 
+    if (message.endpoint === "message") {
+      ipcRenderer.send("message", message.content);
+    }
+
   });
 
 });
index 9e886d78d61686cf86c8c789fd576652f2c97347..e098c016ae5253e2d293c29129a3502d95cb4621 100644 (file)
@@ -13,7 +13,7 @@
         <input class="input" type="text" v-model="email" />
         <div class="inputModifier">Email</div>
       </div>
-      
+
 
       <!-- password -->
       <div class="inputBox">
@@ -53,7 +53,7 @@ export default defineComponent({
     const password = ref("");
     const rememberMe = ref(true);
 
-    const {  data,  invoke  } = useIpc('auth-session');
+    const { invoke } = useIpc();
 
     const submit = async () => {
         const payload = {
@@ -62,8 +62,8 @@ export default defineComponent({
           password: password.value
         };
         try {
-            await invoke(payload);
-            setToken(data.value);
+            const res = await invoke('auth-session', payload);
+            setToken(res);
             router.push({ name: "home" });
         } catch(e){
             console.log('Error loging in.')
index 777f7b4800262c82604772abb5e7c637da97d59c..53193ec47e0418fdef8e638a1110b1ac7e9d31f5 100644 (file)
@@ -49,7 +49,7 @@ export default defineComponent({
     const password = ref("");
     const password2 = ref("");
 
-    const { invoke, data,} = useIpc("auth-session");
+    const { invoke } = useIpc();
     const { setToken } = useAuth();
     const router = useRouter();
 
@@ -81,8 +81,8 @@ export default defineComponent({
 
       if (validate()) {
           try {
-              await invoke(payload);
-              setToken(data.value);
+              const res = await invoke('auth-session', payload);
+              setToken(res);
               router.push({ name: "home" });
           } catch(e) {
               console.log('Failed to register.')