]> Repos - mime-chat/commitdiff
refactor ipc Main
authorriqo <hernandeze2@xavier.edu>
Sat, 19 Jun 2021 16:21:47 +0000 (11:21 -0500)
committerriqo <hernandeze2@xavier.edu>
Sat, 19 Jun 2021 16:25:32 +0000 (11:25 -0500)
13 files changed:
src/audio.ts
src/composables/useIpcMain.ts
src/composables/useWebsockets.ts
src/ipc/account.ts [deleted file]
src/ipc/audio.ts [deleted file]
src/ipc/handlers.ts [new file with mode: 0644]
src/ipc/index.ts
src/ipc/listeners.ts [new file with mode: 0644]
src/ipc/session.ts [deleted file]
src/main.ts
src/render/ipc.ts
src/session.ts
src/types.ts

index 844e79194ab41f6b10af1ef988136ffc0f6c24f2..7ae25628f9f5a797b8876b4529c54d842d1c1f7b 100644 (file)
@@ -6,12 +6,12 @@
 let buffer: ArrayBuffer[] = [];
 
 // place audio data in buffer
-export function collect (chunk: ArrayBuffer) {
-    buffer.push(chunk);
+export const collect: IpcListenerCallback<ArrayBuffer> = (chunk) => {
+    if (chunk) buffer.push(chunk);
 }
 
 // return audio and clear buffer
-export function flush () {
+export const flush = async () => {
     const bufferCopy = buffer;
     buffer = [];
     return bufferCopy;
index 0a187b7ebdd7fe8789b062d2bde03fcad5ba9aa4..57a2cb3ac23fb05e609f756bc3b8c0c502f159d0 100644 (file)
@@ -1,5 +1,5 @@
 
-import { ipcMain, IpcMainInvokeEvent } from "electron";
+import { ipcMain, IpcMainInvokeEvent, IpcMainEvent } from "electron";
 
 export class IpcHandler<InputType, ReturnType> implements IIpcHandler<InputType, ReturnType> {
 
@@ -16,7 +16,7 @@ export class IpcHandler<InputType, ReturnType> implements IIpcHandler<InputType,
   }
 
   handle() {
-      console.log(`[IPC] INIT:${this.channel}`);
+      console.log(`[IPC] INIT: ${this.channel}`);
       this.remove();
       ipcMain.handle(this.channel, this._onInvoke);
   }
@@ -49,6 +49,39 @@ export class IpcHandler<InputType, ReturnType> implements IIpcHandler<InputType,
 }
 
 
+export class IpcListener<InputType> implements IIpcListener<InputType> {
+
+  readonly channel: string;
+
+  readonly _listenerCallback: IpcListenerCallback<InputType>;
+
+  constructor(options: {
+      channel: string;
+      listenerCallback: IpcListenerCallback<InputType>;
+  }) {
+    this.channel = options.channel;
+    this._listenerCallback = options.listenerCallback;
+  }
+
+  listen() {
+      console.log(`[IPC] Init: ${this.channel}`);
+      this.remove();
+      ipcMain.on(this.channel, this._onPost);
+  }
+
+  remove() {
+      ipcMain.removeAllListeners(this.channel);
+  }
+
+  private _onPost = (_e: IpcMainEvent, payload?: string | null): void => {
+
+      console.log(`[IPC] Post: ${this.channel}`);
+
+      const params = payload ? JSON.parse(payload) : null;
+      this._listenerCallback(params);
+  }
+
+}
 
 
 
index cc272d6e00544870a4623bd3b5cef5a6b0b2fe44..d1cfc02b2ebd03135eb73ce339b937862a358912 100644 (file)
@@ -57,7 +57,7 @@ export default function useWebSockets(
         });
 
         socket.on("message", (event: WebSocket.MessageEvent) => {
-            console.log("message received", event.data);
+            console.log("message received", event);
             messageCallback(event.toString())
         });
 
diff --git a/src/ipc/account.ts b/src/ipc/account.ts
deleted file mode 100644 (file)
index b8ae1d3..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-
-"use strict";
-
-import { accountLogin, accountLogout } from "@/account";
-import { IpcHandler } from "@/composables/useIpcMain";
-
-const LOGIN_CHANNEL = "account-login";
-const LOGOUT_CHANNEL = "account-logout";
-
-const loginHandler =  new IpcHandler({
-    channel: LOGIN_CHANNEL,
-    handlerCallback: accountLogin
-});
-
-const logoutHandler =  new IpcHandler({
-    channel: LOGOUT_CHANNEL,
-    handlerCallback: accountLogout
-});
-
-const handlers = [loginHandler, logoutHandler];
-
-export default handlers;
-
diff --git a/src/ipc/audio.ts b/src/ipc/audio.ts
deleted file mode 100644 (file)
index d3c7685..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-
-"use strict";
-
-import { ipcMain, IpcMainInvokeEvent, IpcMainEvent } from "electron";
-import { collect, flush } from "@/composbales/audio";
-import { IpcHandler } from "@/composables/useIpcMain";
-
-const AUDIO_CHUNK_CHANNEL = 'audio-chunk';
-const GET_AUDIO_CHANNEL = 'get-audio';
-
-// handle the new audio data
-const onAudioChunk = (
-    _e: IpcMainEvent,
-    payload: ArrayBuffer
-) => {
-    console.log('[IPC]: audio-buffer');
-    collect(payload);
-}
-
-const getAudioHandler = new IpcHandler({
-    channel: GET_AUDIO_CHANNEL,
-    handlerCallback: flush
-});
-
-
-export default function useAudioListeners(): void {
-
-    ipcMain.removeAllListeners("audio-chunk");
-    ipcMain.on("audio-chunk", onAudioChunk);
-
-}
diff --git a/src/ipc/handlers.ts b/src/ipc/handlers.ts
new file mode 100644 (file)
index 0000000..bc44fc2
--- /dev/null
@@ -0,0 +1,27 @@
+
+"use strict";
+
+import { accountLogin, accountLogout } 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";
+
+export const loginHandler =  new IpcHandler({
+    channel: LOGIN_CHANNEL,
+    handlerCallback: accountLogin
+});
+
+export const logoutHandler =  new IpcHandler({
+    channel: LOGOUT_CHANNEL,
+    handlerCallback: accountLogout
+});
+
+export const getAudioHandler = new IpcHandler({
+    channel: GET_AUDIO_CHANNEL,
+    handlerCallback: flush
+});
+
+
index 017a9f2b78605a1355b1fbebfc131557134ad7d5..e6eb90a0a7694bcac7e69d1c1c1966c0c0dc0ba9 100644 (file)
@@ -1,36 +1,33 @@
 
 "use strict";
 
-import {IpcHandler} from "@/composables/useIpcMain";
-import accountHandlers from "./account";
-import useSessionListeners from "./session";
-// import useAudioListeners from "./audio";
-
-interface IPCHandlers {
-    [channel: string]: IpcHandler<any, any>;
-}
+import * as handlers from "./handlers";
+import * as listeners from "./listeners";
 
 const ipcHandlers: IPCHandlers = {};
-
-const _initHandlers = (handlers: Array<IpcHandler<any, any>>) => {
-    handlers.forEach((h: IpcHandler<any, any>) => {
-        if (!(h.channel in ipcHandlers)) {
-            ipcHandlers[h.channel] = h;
-            h.handle();
-        }
-    });
-}
-
-export default function useIpc(): void {
-
-    _initHandlers(accountHandlers);
-
-    // useAccountListeners();
-
-    useSessionListeners();
-
-    // useAudioListeners();
-
+const ipcListeners: IPCListeners = {};
+
+const _initHandlers = (): void => {
+    for (const [key, handler] of Object.entries(handlers)) {
+      if (!(key in ipcHandlers)) {
+          ipcHandlers[key] = handler;
+          handler.handle();
+      }
+    }
+};
+
+const _initListeners = (): void => {
+    for (const [key, listener] of Object.entries(listeners)) {
+      if (!(key in ipcListeners)) {
+          ipcListeners[key] = listener;
+          listener.listen();
+      }
+    }
+};
+
+export default function initIpcMain(): void {
+    _initHandlers();
+    _initListeners();
 }
 
 
diff --git a/src/ipc/listeners.ts b/src/ipc/listeners.ts
new file mode 100644 (file)
index 0000000..f88887a
--- /dev/null
@@ -0,0 +1,17 @@
+
+import { IpcListener } from "@/composables/useIpcMain"
+import { sendMessage } from '@/session';
+import { collect } from "@/audio";
+
+const CLIENT_MESSAGE_CHANNEL = "post-session-send"
+const GET_AUDIO_CHANNEL = "post-audio-collect";
+
+export const messageListener = new IpcListener<Message>({
+    channel: CLIENT_MESSAGE_CHANNEL,
+    listenerCallback: sendMessage
+});
+
+export const audioChunkListener = new IpcListener<ArrayBuffer>({
+    channel: GET_AUDIO_CHANNEL,
+    listenerCallback: collect
+});
diff --git a/src/ipc/session.ts b/src/ipc/session.ts
deleted file mode 100644 (file)
index 0d89eb7..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-
-"use strict";
-
-import { ipcMain, IpcMainEvent } from "electron";
-import { sendMessage } from '@/session';
-
-// Handle messages from window/client.
-function onSendMessage(_event: IpcMainEvent, payload: Message): void {
-    sendMessage(payload);
-}
-
-export default function useSessionListeners(): void {
-    ipcMain.removeAllListeners("client-message");
-    ipcMain.on("client-message", onSendMessage);
-}
index 9103cae552a3e411a034b4ea3f6e428cd99588ad..40dd4daea6abb79f0c625a98d355870d16d09e62 100644 (file)
@@ -9,7 +9,7 @@
  *
  */
 
-import useIpc from "@/ipc/index";
+import initIpcMain from "@/ipc/index";
 import  { accountAuth } from "./account";
 import { launchSession } from "./session";
 import { ipcEmit } from "./composables/useEmitter";
@@ -20,7 +20,7 @@ let authState: AuthState | null;
 export default async function main() {
 
     /* initiate controls for frontend to use when needed */
-    useIpc();
+    initIpcMain();
 
     /* launch browser window */
     await createWindow();
index 9a8e47819efea58c7493674ee3fae5a28f4f89d0..e3c37752c06055e62cb4dd79433aa2c191cd646b 100644 (file)
@@ -13,11 +13,11 @@ const { post, invoke } = useIpc();
 export const invokeLogin = async (
     payload: LoginPayload
 ): Promise<Profile | Error> => (
-    await invoke('account-login', JSON.stringify(payload))
+    await invoke('invoke-account-login', JSON.stringify(payload))
 );
 
 export const invokeLogout = async (): Promise<void> => (
-    await invoke("account-logout", null)
+    await invoke("invoke-account-logout", null)
 );
 
 /**
@@ -27,11 +27,11 @@ export const invokeLogout = async (): Promise<void> => (
  */
 
 export const postAudioChunk = (chunk: ArrayBuffer): void => (
-    post("audio-chunk", chunk)
+    post("post-audio-collect", chunk)
 );
 
 export const invokeReturnAudio = async (): Promise<ArrayBuffer[] | Error> => (
-    await invoke("get-audio", null)
+    await invoke("invoke-audio-flush", null)
 );
 
 /**
@@ -45,5 +45,5 @@ export const invokeSession = async (cid: string): Promise<Profile | Error> => (
 );
 
 export const postMessage = (payload: Message): void => (
-    post('client-message', payload)
+    post('post-session-send', payload)
 );
index 3e844c1d478efff1ef4d31f6d106bbef8c4d0d07..1b278ba6d681d84a1a1ba98414e5bcec87a3a17c 100644 (file)
@@ -13,16 +13,16 @@ const uiState: any | null = null;
 /* start and stop audio functionality */
 // const { initAudio, closeAudio } = useAudio();
 
-let isInitMessage = (message: any): boolean => {
+const isInitMessage = (message: any): boolean => {
     console.log(message)
     return true;
 };
 
-let deauthenticate = (): void => {
+const deauthenticate = (): void => {
     console.log('deauthenticating')
 };
 
-let isAddMessage = (message: any): boolean => {
+const isAddMessage = (message: any): boolean => {
     console.log(message)
     return true;
 };
@@ -67,7 +67,7 @@ const onConnectionStatusCallback = (alive: boolean) => {
 const { connect, send, close } = useWebsockets(onMessageCallback, onConnectionStatusCallback);
 
 /* send a message to the platform */
-export function sendMessage(message: Message) {
+export function sendMessage<Message>(message: Message): void {
 
     /* socket send */
     send(message);
index 4dfa705e5e5fe7de7c88fec9e4b41584187480d2..876bf388240186f33b932ea0c2794a0b35793883 100644 (file)
@@ -39,14 +39,34 @@ interface AccountCredentials {
     password: string;
 }
 
+/*
+ * Electron Ipc Main
+ */
 interface IpcHandlerCallback<I, O> {
     (payload: I | null): Promise<Error | O>;
 }
 
+interface IpcListenerCallback<I> {
+    (payload: I | null): void;
+}
+
 interface IIpcHandler<I, O> {
     handle(): void;
     remove(): void;
     readonly _handlerCallback: IpcHandlerCallback<I, O>;
 }
 
+interface IIpcListener<I> {
+    listen(): void;
+    remove(): void;
+    readonly _listenerCallback: IpcListenerCallback<I>;
+}
+
+interface IPCHandlers {
+    [handler: string]: IIpcHandler<any, any>;
+}
+
+interface IPCListeners {
+    [listener: string]: IIpcListener<any>;
+}