refactor ipc Main
This commit is contained in:
parent
a6351af83c
commit
a22b3c68e5
13 changed files with 139 additions and 114 deletions
|
|
@ -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;
|
||||
|
||||
|
|
@ -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);
|
||||
|
||||
}
|
||||
27
src/ipc/handlers.ts
Normal file
27
src/ipc/handlers.ts
Normal file
|
|
@ -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
|
||||
});
|
||||
|
||||
|
||||
|
|
@ -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 ipcListeners: IPCListeners = {};
|
||||
|
||||
const _initHandlers = (handlers: Array<IpcHandler<any, any>>) => {
|
||||
handlers.forEach((h: IpcHandler<any, any>) => {
|
||||
if (!(h.channel in ipcHandlers)) {
|
||||
ipcHandlers[h.channel] = h;
|
||||
h.handle();
|
||||
}
|
||||
});
|
||||
}
|
||||
const _initHandlers = (): void => {
|
||||
for (const [key, handler] of Object.entries(handlers)) {
|
||||
if (!(key in ipcHandlers)) {
|
||||
ipcHandlers[key] = handler;
|
||||
handler.handle();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default function useIpc(): void {
|
||||
|
||||
_initHandlers(accountHandlers);
|
||||
|
||||
// useAccountListeners();
|
||||
|
||||
useSessionListeners();
|
||||
|
||||
// useAudioListeners();
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
17
src/ipc/listeners.ts
Normal file
17
src/ipc/listeners.ts
Normal file
|
|
@ -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
|
||||
});
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in a new issue