refactor ipc Main

This commit is contained in:
riqo 2021-06-19 11:21:47 -05:00
commit a22b3c68e5
13 changed files with 139 additions and 114 deletions

View 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);
}
}

View 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())
});