refactor ipc Main
This commit is contained in:
parent
a6351af83c
commit
a22b3c68e5
13 changed files with 139 additions and 114 deletions
|
|
@ -6,12 +6,12 @@
|
|||
let buffer: ArrayBuffer[] = [];
|
||||
|
||||
// place audio data in buffer
|
||||
export function collect (chunk: ArrayBuffer) {
|
||||
buffer.push(chunk);
|
||||
export const collect: IpcListenerCallback<ArrayBuffer> = (chunk) => {
|
||||
if (chunk) buffer.push(chunk);
|
||||
}
|
||||
|
||||
// return audio and clear buffer
|
||||
export function flush () {
|
||||
export const flush = async () => {
|
||||
const bufferCopy = buffer;
|
||||
buffer = [];
|
||||
return bufferCopy;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
*
|
||||
*/
|
||||
|
||||
import useIpc from "@/ipc/index";
|
||||
import initIpcMain from "@/ipc/index";
|
||||
import { accountAuth } from "./account";
|
||||
import { launchSession } from "./session";
|
||||
import { ipcEmit } from "./composables/useEmitter";
|
||||
|
|
@ -20,7 +20,7 @@ let authState: AuthState | null;
|
|||
export default async function main() {
|
||||
|
||||
/* initiate controls for frontend to use when needed */
|
||||
useIpc();
|
||||
initIpcMain();
|
||||
|
||||
/* launch browser window */
|
||||
await createWindow();
|
||||
|
|
|
|||
|
|
@ -13,11 +13,11 @@ const { post, invoke } = useIpc();
|
|||
export const invokeLogin = async (
|
||||
payload: LoginPayload
|
||||
): Promise<Profile | Error> => (
|
||||
await invoke('account-login', JSON.stringify(payload))
|
||||
await invoke('invoke-account-login', JSON.stringify(payload))
|
||||
);
|
||||
|
||||
export const invokeLogout = async (): Promise<void> => (
|
||||
await invoke("account-logout", null)
|
||||
await invoke("invoke-account-logout", null)
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
@ -27,11 +27,11 @@ export const invokeLogout = async (): Promise<void> => (
|
|||
*/
|
||||
|
||||
export const postAudioChunk = (chunk: ArrayBuffer): void => (
|
||||
post("audio-chunk", chunk)
|
||||
post("post-audio-collect", chunk)
|
||||
);
|
||||
|
||||
export const invokeReturnAudio = async (): Promise<ArrayBuffer[] | Error> => (
|
||||
await invoke("get-audio", null)
|
||||
await invoke("invoke-audio-flush", null)
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
@ -45,5 +45,5 @@ export const invokeSession = async (cid: string): Promise<Profile | Error> => (
|
|||
);
|
||||
|
||||
export const postMessage = (payload: Message): void => (
|
||||
post('client-message', payload)
|
||||
post('post-session-send', payload)
|
||||
);
|
||||
|
|
|
|||
|
|
@ -13,16 +13,16 @@ const uiState: any | null = null;
|
|||
/* start and stop audio functionality */
|
||||
// const { initAudio, closeAudio } = useAudio();
|
||||
|
||||
let isInitMessage = (message: any): boolean => {
|
||||
const isInitMessage = (message: any): boolean => {
|
||||
console.log(message)
|
||||
return true;
|
||||
};
|
||||
|
||||
let deauthenticate = (): void => {
|
||||
const deauthenticate = (): void => {
|
||||
console.log('deauthenticating')
|
||||
};
|
||||
|
||||
let isAddMessage = (message: any): boolean => {
|
||||
const isAddMessage = (message: any): boolean => {
|
||||
console.log(message)
|
||||
return true;
|
||||
};
|
||||
|
|
@ -67,7 +67,7 @@ const onConnectionStatusCallback = (alive: boolean) => {
|
|||
const { connect, send, close } = useWebsockets(onMessageCallback, onConnectionStatusCallback);
|
||||
|
||||
/* send a message to the platform */
|
||||
export function sendMessage(message: Message) {
|
||||
export function sendMessage<Message>(message: Message): void {
|
||||
|
||||
/* socket send */
|
||||
send(message);
|
||||
|
|
|
|||
20
src/types.ts
20
src/types.ts
|
|
@ -39,14 +39,34 @@ interface AccountCredentials {
|
|||
password: string;
|
||||
}
|
||||
|
||||
/*
|
||||
* Electron Ipc Main
|
||||
*/
|
||||
interface IpcHandlerCallback<I, O> {
|
||||
(payload: I | null): Promise<Error | O>;
|
||||
}
|
||||
|
||||
interface IpcListenerCallback<I> {
|
||||
(payload: I | null): void;
|
||||
}
|
||||
|
||||
interface IIpcHandler<I, O> {
|
||||
handle(): void;
|
||||
remove(): void;
|
||||
readonly _handlerCallback: IpcHandlerCallback<I, O>;
|
||||
}
|
||||
|
||||
interface IIpcListener<I> {
|
||||
listen(): void;
|
||||
remove(): void;
|
||||
readonly _listenerCallback: IpcListenerCallback<I>;
|
||||
}
|
||||
|
||||
interface IPCHandlers {
|
||||
[handler: string]: IIpcHandler<any, any>;
|
||||
}
|
||||
|
||||
interface IPCListeners {
|
||||
[listener: string]: IIpcListener<any>;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue