refactor ipcEmit event
This commit is contained in:
parent
2c510765c0
commit
95e3dab1c2
6 changed files with 14 additions and 21 deletions
|
|
@ -6,10 +6,10 @@ class BackgroundMitt extends EventEmitter { }
|
||||||
|
|
||||||
export const backgroundMitt = new BackgroundMitt();
|
export const backgroundMitt = new BackgroundMitt();
|
||||||
|
|
||||||
export const ipcEmit = (channel: string, payload: any) => {
|
export const ipcEmit = <T>(channel: string, payload: T) => {
|
||||||
backgroundMitt.emit('ipc-renderer', {
|
backgroundMitt.emit('ipc-renderer', {
|
||||||
channel: channel,
|
channel,
|
||||||
message: payload
|
payload
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ export class IpcRendererListener<InputParam> implements IIpcListener<InputParam>
|
||||||
window.ipcRenderer.removeAllListeners(this.channel);
|
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}`);
|
console.log(`[IPC] Post: ${this.channel}`);
|
||||||
this._listenerCallback(payload.message);
|
this._listenerCallback(payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
import { ref, Ref } from "vue";
|
import { ref, Ref } from "vue";
|
||||||
import useScroll from "@/render/composables/useScroll";
|
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) => {
|
export const setMessages: IpcListenerCallback<Array<Message>> = (payload) => {
|
||||||
messages.value = payload as Array<Message>;
|
messages.value = payload as Array<Message>;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
// shared
|
// shared
|
||||||
import { ref } from "vue";
|
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) => {
|
export const setProfile: IpcListenerCallback<Profile | null> = (payload) => {
|
||||||
payload ? profile.value = payload : clearProfile();
|
payload ? profile.value = payload : clearProfile();
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,8 @@ interface IPCListeners {
|
||||||
[listener: string]: IIpcListener<any> | null;
|
[listener: string]: IIpcListener<any> | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IpcEmitPayload<T> {
|
interface IpcRendererEvent<T> {
|
||||||
message: T | null;
|
channel: string;
|
||||||
|
payload: T | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,6 @@ import fs from 'fs';
|
||||||
import { config } from "@/config";
|
import { config } from "@/config";
|
||||||
const { autoUpdater } = require('electron-updater');
|
const { autoUpdater } = require('electron-updater');
|
||||||
|
|
||||||
interface IpcRendererPayload {
|
|
||||||
channel: string;
|
|
||||||
message: Message | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
let win: BrowserWindow | null;
|
let win: BrowserWindow | null;
|
||||||
let winState: WindowState;
|
let winState: WindowState;
|
||||||
|
|
||||||
|
|
@ -49,12 +44,9 @@ const onNavBar = (_event: any, action: string): void => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Util function to render message on ipc-renderer event.
|
// Util function to render message on ipc-renderer event.
|
||||||
const renderMessage = (payload: IpcRendererPayload): void => {
|
const postToWindow = <T>(event: IpcRendererEvent<T>): void => {
|
||||||
console.log('sending to browser window', payload);
|
|
||||||
if (win) {
|
if (win) {
|
||||||
win.webContents.send(payload.channel, {
|
win.webContents.send(event.channel, event.payload);
|
||||||
message: payload.message
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +81,7 @@ const onWindowMount = (): void => {
|
||||||
|
|
||||||
// Gateway for messages to the frontend.
|
// Gateway for messages to the frontend.
|
||||||
backgroundMitt.removeAllListeners("ipc-renderer")
|
backgroundMitt.removeAllListeners("ipc-renderer")
|
||||||
backgroundMitt.on("ipc-renderer", renderMessage);
|
backgroundMitt.on("ipc-renderer", postToWindow);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue