]> Repos - mime-chat/commitdiff
refactor frontend ipc
authorriqo <hernandeze2@xavier.edu>
Sat, 19 Jun 2021 20:22:17 +0000 (15:22 -0500)
committerriqo <hernandeze2@xavier.edu>
Sat, 19 Jun 2021 20:22:17 +0000 (15:22 -0500)
13 files changed:
src/account.ts
src/composables/useIpcMain.ts
src/ipc/handlers.ts
src/ipc/listeners.ts
src/main.ts
src/render/App.vue
src/render/composables/useIpcRend.ts
src/render/composables/useProfile.ts
src/render/ipc.ts
src/render/listeners.ts [new file with mode: 0644]
src/render/main.ts
src/store.ts
src/types.ts

index 9f867f64ec716465753b61c5580dd0534135bec9..581da0e36b0c6b3933b79ac323ad3faf95bc67ba 100644 (file)
@@ -1,8 +1,9 @@
 
 import { postAuth, postLogin, postLogout } from "@/api/account";
 import { endSession, launchSession } from "@/session";
-import { getToken, clearToken, setToken } from "./store";
+import { getToken,  setToken, setProfile, getProfile, clearStore } from "./store";
 import { parseAuthRes } from "./auth";
+import { ipcEmit } from "@/composables/useEmitter";
 
 export const accountAuth = async (): Promise<Error | AuthState> => {
 
@@ -18,6 +19,7 @@ export const accountAuth = async (): Promise<Error | AuthState> => {
             const parsed = parseAuthRes(res);
 
             setToken(parsed.token)
+            setProfile(parsed.profile);
 
             return {
                 profile: parsed.profile,
@@ -26,7 +28,7 @@ export const accountAuth = async (): Promise<Error | AuthState> => {
 
         } catch(e) {
             console.log('[ACCOUNT]', e);
-            clearToken();
+            clearStore();
             throw(new Error('Failed to authenticate.'));
 
         }
@@ -45,6 +47,7 @@ export const accountLogin: IpcHandlerCallback<AccountCredentials, Profile> = asy
 
          // save jwt token and profile
          setToken(parsed.token);
+         setProfile(parsed.profile);
 
          // launch session
          launchSession(parsed.token);
@@ -53,6 +56,7 @@ export const accountLogin: IpcHandlerCallback<AccountCredentials, Profile> = asy
          return parsed.profile;
 
      } catch(e) {
+         clearStore();
          throw e;
      }
 }
@@ -65,7 +69,7 @@ export const accountLogout = async (): Promise<Error | void> => {
         await postLogout();
 
         // remove key and crimataId
-        clearToken();
+        clearStore();
 
         // kill crimata platform session
         endSession();
@@ -79,4 +83,13 @@ export const accountLogout = async (): Promise<Error | void> => {
 
 }
 
+export const updateAppState = (): void => {
+
+    const profile = getProfile();
+
+    ipcEmit("set-profile", profile);
+
+    // ipcEmit('messages') etc
+
+}
 
index 57a2cb3ac23fb05e609f756bc3b8c0c502f159d0..ebfc4bcb0ef5491a30dddf1bbae593bfd41acfac 100644 (file)
@@ -1,22 +1,21 @@
 
 import { ipcMain, IpcMainInvokeEvent, IpcMainEvent } from "electron";
 
-export class IpcHandler<InputType, ReturnType> implements IIpcHandler<InputType, ReturnType> {
+export class IpcHandler<InputParam, ReturnType> implements IIpcHandler<InputParam, ReturnType> {
 
   readonly channel: string;
 
-  readonly _handlerCallback: IpcHandlerCallback<InputType, ReturnType>;
+  readonly _handlerCallback: IpcHandlerCallback<InputParam, ReturnType>;
 
   constructor(options: {
       channel: string;
-      handlerCallback: IpcHandlerCallback<InputType, ReturnType>;
+      handlerCallback: IpcHandlerCallback<InputParam, ReturnType>;
   }) {
     this.channel = options.channel;
     this._handlerCallback = options.handlerCallback;
   }
 
   handle() {
-      console.log(`[IPC] INIT: ${this.channel}`);
       this.remove();
       ipcMain.handle(this.channel, this._onInvoke);
   }
@@ -49,22 +48,21 @@ export class IpcHandler<InputType, ReturnType> implements IIpcHandler<InputType,
 }
 
 
-export class IpcListener<InputType> implements IIpcListener<InputType> {
+export class IpcListener<InputParam> implements IIpcListener<InputParam> {
 
   readonly channel: string;
 
-  readonly _listenerCallback: IpcListenerCallback<InputType>;
+  readonly _listenerCallback: IpcListenerCallback<InputParam>;
 
   constructor(options: {
       channel: string;
-      listenerCallback: IpcListenerCallback<InputType>;
+      listenerCallback: IpcListenerCallback<InputParam>;
   }) {
     this.channel = options.channel;
     this._listenerCallback = options.listenerCallback;
   }
 
   listen() {
-      console.log(`[IPC] Init: ${this.channel}`);
       this.remove();
       ipcMain.on(this.channel, this._onPost);
   }
@@ -82,7 +80,3 @@ export class IpcListener<InputType> implements IIpcListener<InputType> {
   }
 
 }
-
-
-
-
index bc44fc276987fdf9d22915b70e80aacd6dc6ee45..5529662b9c8ed8c5279ddecb1285328e347bc52d 100644 (file)
@@ -1,10 +1,11 @@
 
 "use strict";
 
-import { accountLogin, accountLogout } from "@/account";
+import { accountLogin, accountLogout, accountProfile } from "@/account";
 import { IpcHandler } from "@/composables/useIpcMain";
 import { flush } from "@/audio";
 
+
 const LOGIN_CHANNEL = "invoke-account-login";
 const LOGOUT_CHANNEL = "invoke-account-logout";
 const GET_AUDIO_CHANNEL = "invoke-audio-flush";
index f88887a8e0437f2163310cf5ad718c6b517d5b33..8ef5d451a9098e8f75e1c3b8df0b565606890cdb 100644 (file)
@@ -2,9 +2,11 @@
 import { IpcListener } from "@/composables/useIpcMain"
 import { sendMessage } from '@/session';
 import { collect } from "@/audio";
+import { updateAppState } from "@/account";
 
 const CLIENT_MESSAGE_CHANNEL = "post-session-send"
 const GET_AUDIO_CHANNEL = "post-audio-collect";
+const APP_MOUNT_CHANNEL = "post-app-mount";
 
 export const messageListener = new IpcListener<Message>({
     channel: CLIENT_MESSAGE_CHANNEL,
@@ -15,3 +17,8 @@ export const audioChunkListener = new IpcListener<ArrayBuffer>({
     channel: GET_AUDIO_CHANNEL,
     listenerCallback: collect
 });
+
+export const appMountListener = new IpcListener<null>({
+    channel: APP_MOUNT_CHANNEL,
+    listenerCallback: updateAppState
+});
index 40dd4daea6abb79f0c625a98d355870d16d09e62..a360835a692426a0352df7ac43114372b958858c 100644 (file)
@@ -10,9 +10,8 @@
  */
 
 import initIpcMain from "@/ipc/index";
-import  { accountAuth } from "./account";
+import  { accountAuth, updateAppState } from "./account";
 import { launchSession } from "./session";
-import { ipcEmit } from "./composables/useEmitter";
 import createWindow from "./window";
 
 let authState: AuthState | null;
@@ -31,12 +30,10 @@ export default async function main() {
         console.log('AUTH:', e);
         authState = null;
     } finally {
-        let profile = null;
         if (authState) {
             launchSession(authState.token as string);
-            profile = authState.profile;
         }
-        ipcEmit("set-profile", profile);
+        updateAppState();
     }
 
 }
index d53788fa49241c0277718f65a48234f1b0389b69..a7645d1a52d601bb8c30144ddb801789567388ff 100644 (file)
 
 <script lang="ts">
 
-import { defineComponent, onMounted, onUnmounted, Ref, ref } from "vue";
+import { defineComponent, onMounted } from "vue";
 import { IpcRendererEvent } from "electron";
 
 import Splash from "@/render/components/splash.vue";
 import Messenger from "@/render/components/messenger.vue";
 import Login from "@/render/components/login.vue";
 import Header from "@/render/components/header.vue";
+
 import useProfile from "@/render/composables/useProfile";
+import { postAppMount } from "@/render/ipc";
 
 export default defineComponent({
 
@@ -43,25 +45,9 @@ export default defineComponent({
 
   setup() {
 
-    const authComplete = ref(false);
-
-    const { setProfile, clearProfile, profile } = useProfile();
-
-    onMounted(async () => {
-      console.log("[APP]:mounted.");
-
-      /* listen for auth related messages */
-      window.ipcRenderer.on("set-profile", (_event: IpcRendererEvent, payload: any) => {
-        console.log('progfile', payload.message)
-        payload.message ? setProfile(payload.message) : clearProfile();
-        authComplete.value = true;
-      });
-
-    });
+    const { profile, authComplete } = useProfile();
 
-    onUnmounted(() => {
-      window.ipcRenderer.removeAllListeners("set-profile");
-    });
+    onMounted(() => postAppMount());
 
     return {
         authComplete,
index 0d067d36954fa32522754182de80ef08e95904f1..c1e5e330b9afe084a087b5a6eeebee67d6317beb 100644 (file)
@@ -1,6 +1,39 @@
 
-export default function useIpc () {
-    
+import { IpcRendererEvent } from "electron";
+
+export class IpcRendererListener<InputParam> implements IIpcListener<InputParam> {
+
+  readonly channel: string;
+
+  readonly _listenerCallback: IpcListenerCallback<InputParam>;
+
+  constructor(options: {
+      channel: string;
+      listenerCallback: IpcListenerCallback<InputParam>;
+  }) {
+    this.channel = options.channel;
+    this._listenerCallback = options.listenerCallback;
+  }
+
+  listen() {
+      this.remove();
+      window.ipcRenderer.on(this.channel, this._onPost);
+  }
+
+  remove() {
+      window.ipcRenderer.removeAllListeners(this.channel);
+  }
+
+  private _onPost = (_e: IpcRendererEvent, payload: IpcEmitPayload<InputParam>): void => {
+      console.log(`[IPC] Post: ${this.channel}`);
+      this._listenerCallback(payload.message);
+  }
+
+}
+
+
+export default function useIpcRenderer () {
+
     const invoke = async (endpoint: string, payload: any) => {
         try {
             const res = await window.ipcRenderer.invoke(endpoint, payload);
@@ -16,6 +49,6 @@ export default function useIpc () {
 
     return {
         invoke,
-        post
+        post,
     }
 }
index 0ee3b6db6dc7a9a08cf1dd45b8b33e2787b1f45c..0f3c39efb1649406bbc607ea8980f968f3c1f4e5 100644 (file)
@@ -2,20 +2,30 @@ import { ref } from "vue";
 
 const profile = ref();
 
+const authComplete = ref(false);
+
 const useProfile = () => {
 
-    const setProfile = (payload: Profile) => {
-        profile.value = payload;
+    const setProfile: IpcListenerCallback<Profile | null> = (payload) => {
+        payload ? profile.value = payload : clearProfile();
+        showRender();
     }
 
     const clearProfile = () => {
         profile.value = null;
     }
 
+    const showRender = () => {
+        authComplete.value = true;
+    }
+
+
     return {
         setProfile,
         clearProfile,
-        profile
+        profile,
+        showRender,
+        authComplete,
     }
 
 }
index e3c37752c06055e62cb4dd79433aa2c191cd646b..bc68049dc40920ff64b31047998529d07ea1e104 100644 (file)
@@ -1,5 +1,6 @@
 
 import useIpc from "@/render/composables/useIpcRend";
+import * as rendererListeners from "./listeners";
 
 const { post, invoke } = useIpc();
 
@@ -47,3 +48,33 @@ export const invokeSession = async (cid: string): Promise<Profile | Error> => (
 export const postMessage = (payload: Message): void => (
     post('post-session-send', payload)
 );
+
+export const postAppMount = (): void => (
+    post('post-app-mount', null)
+);
+
+
+/**
+ *
+ * Ipc Renderer Listeners
+ *
+ */
+
+let ipcListeners: IPCListeners = {};
+
+export const initIpcRendererListeners = () => {
+    for (const [key, listener] of Object.entries(rendererListeners)) {
+      if (!(key in ipcListeners)) {
+          ipcListeners[key] = listener;
+          listener.listen();
+      }
+    }
+};
+
+export const removeListeners = () => {
+    for (const [key, listener] of Object.entries(rendererListeners)) {
+      listener.remove();
+    }
+    ipcListeners = {};
+};
+
diff --git a/src/render/listeners.ts b/src/render/listeners.ts
new file mode 100644 (file)
index 0000000..76f4832
--- /dev/null
@@ -0,0 +1,32 @@
+
+import { IpcRendererListener } from "./composables/useIpcRend"
+import useProfile from "./composables/useProfile";
+
+const { setProfile } = useProfile();
+
+const SET_PROFILE_CHANNEL = "set-profile";
+
+const INIT_MESSAGES_CHANNEL = "init-messages";
+const ADD_MESSAGE_CHANNEL = "add-message";
+const UPDATE_MESSAGE_CHANNEL = "update-message";
+
+export const setProfileListener = new IpcRendererListener({
+    channel: SET_PROFILE_CHANNEL,
+    listenerCallback: setProfile
+});
+
+export const initMessagesListener = new IpcRendererListener({
+    channel: INIT_MESSAGES_CHANNEL,
+    listenerCallback: () => {}
+});
+
+
+export const addMessagesListener = new IpcRendererListener({
+    channel: ADD_MESSAGE_CHANNEL,
+    listenerCallback: () => {}
+});
+
+export const updateMessagesListener = new IpcRendererListener({
+    channel: UPDATE_MESSAGE_CHANNEL,
+    listenerCallback: () => {}
+});
index 50a594aa79f6551c722d89c14e33f92f674009e0..537588980fa552d3165111fd81874d7b57481962 100644 (file)
@@ -6,11 +6,16 @@ import App from "./App.vue";
 import mitt from "mitt";
 import { createApp } from "vue";
 
+import { initIpcRendererListeners } from "./ipc"
+
+
+// Handle ipcMain events.
+initIpcRendererListeners();
 
 // Handle events.
 const emitter = mitt();
 
-const app = createApp(App)
+const app = createApp(App);
 
-app.provide("mitt", emitter)
+app.provide("mitt", emitter);
 app.mount("#app");
index 88b9c17e8d02e48f7801f25473764cfd96973025..e532625a7f246bd105a312933b4e5ba04dc7f21b 100644 (file)
@@ -1,9 +1,10 @@
 const Store = require('electron-store');
 
 const schema = {
-       key: {
+       token: {
                type: 'string',
        },
+    profile: {}
 };
 
 const store = new Store({
@@ -17,3 +18,14 @@ export const clearToken = (): void => (store.delete("token"));
 
 export const setToken = (token: string): void => (store.set('token', token));
 
+export const setProfile = (profile: Profile): Profile => (store.set('profile', profile));
+
+export const getProfile = (): Profile => (store.get('profile'));
+
+export const clearProfile = (): void => (store.delete('profile'));
+
+export const clearStore = (): void => {
+    clearToken();
+    clearProfile();
+}
+
index 876bf388240186f33b932ea0c2794a0b35793883..01dd4b56309719b5e09665af0a54d0c57ed0cf90 100644 (file)
@@ -46,8 +46,8 @@ interface IpcHandlerCallback<I, O> {
     (payload: I | null): Promise<Error | O>;
 }
 
-interface IpcListenerCallback<I> {
-    (payload: I | null): void;
+interface IpcListenerCallback<T> {
+    (payload: T | null): void;
 }
 
 interface IIpcHandler<I, O> {
@@ -67,6 +67,10 @@ interface IPCHandlers {
 }
 
 interface IPCListeners {
-    [listener: string]: IIpcListener<any>;
+    [listener: string]: IIpcListener<any> | null;
+}
+
+interface IpcEmitPayload<T> {
+    message: T | null;
 }