refactor frontend ipc

This commit is contained in:
riqo 2021-06-19 15:22:17 -05:00
commit 56196bcbcf
13 changed files with 177 additions and 52 deletions

View 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,
}
}