refactor ipcEmit event

This commit is contained in:
riqo 2021-06-20 14:01:48 -05:00
commit 95e3dab1c2
6 changed files with 14 additions and 21 deletions

View file

@ -6,10 +6,10 @@ class BackgroundMitt extends EventEmitter { }
export const backgroundMitt = new BackgroundMitt();
export const ipcEmit = (channel: string, payload: any) => {
export const ipcEmit = <T>(channel: string, payload: T) => {
backgroundMitt.emit('ipc-renderer', {
channel: channel,
message: payload
channel,
payload
});
};

View file

@ -24,9 +24,9 @@ export class IpcRendererListener<InputParam> implements IIpcListener<InputParam>
window.ipcRenderer.removeAllListeners(this.channel);
}
private _onPost = (_e: IpcRendererEvent, payload: IpcEmitPayload<InputParam>): void => {
private _onPost = (_e: IpcRendererEvent, payload: InputParam): void => {
console.log(`[IPC] Post: ${this.channel}`);
this._listenerCallback(payload.message);
this._listenerCallback(payload);
}
}

View file

@ -2,7 +2,7 @@
import { ref, Ref } from "vue";
import useScroll from "@/render/composables/useScroll";
export let messages: Ref<Array<Message>> = ref([]);
export const messages: Ref<Array<Message>> = ref([]);
export const setMessages: IpcListenerCallback<Array<Message>> = (payload) => {
messages.value = payload as Array<Message>;

View file

@ -1,9 +1,9 @@
// shared
import { ref } from "vue";
export let profile = ref();
export const profile = ref();
export let authComplete = ref(false);
export const authComplete = ref(false);
export const setProfile: IpcListenerCallback<Profile | null> = (payload) => {
payload ? profile.value = payload : clearProfile();

View file

@ -70,7 +70,8 @@ interface IPCListeners {
[listener: string]: IIpcListener<any> | null;
}
interface IpcEmitPayload<T> {
message: T | null;
interface IpcRendererEvent<T> {
channel: string;
payload: T | null;
}

View file

@ -9,11 +9,6 @@ import fs from 'fs';
import { config } from "@/config";
const { autoUpdater } = require('electron-updater');
interface IpcRendererPayload {
channel: string;
message: Message | null;
}
let win: BrowserWindow | null;
let winState: WindowState;
@ -49,12 +44,9 @@ const onNavBar = (_event: any, action: string): void => {
}
// Util function to render message on ipc-renderer event.
const renderMessage = (payload: IpcRendererPayload): void => {
console.log('sending to browser window', payload);
const postToWindow = <T>(event: IpcRendererEvent<T>): void => {
if (win) {
win.webContents.send(payload.channel, {
message: payload.message
});
win.webContents.send(event.channel, event.payload);
}
}
@ -89,7 +81,7 @@ const onWindowMount = (): void => {
// Gateway for messages to the frontend.
backgroundMitt.removeAllListeners("ipc-renderer")
backgroundMitt.on("ipc-renderer", renderMessage);
backgroundMitt.on("ipc-renderer", postToWindow);
}