2019-06-16 00:41:43 +02:00
|
|
|
import { ipcMain } from 'electron';
|
2019-10-02 23:01:18 +02:00
|
|
|
import IpcMainEvent = Electron.IpcMainEvent;
|
2019-06-16 00:41:43 +02:00
|
|
|
|
2021-01-06 02:35:46 +01:00
|
|
|
export function registerHandler(channel: IpcChannel, controller: IpcController, handler: string): void {
|
|
|
|
ipcMain.on(channel, (event: IpcMainEvent, payload: IpcPayload) => {
|
2020-07-25 02:02:37 +02:00
|
|
|
((controller.get() as unknown) as { [x: string]: IpcHandler })
|
|
|
|
[handler](payload.data)
|
|
|
|
.then((result: unknown) => {
|
2021-01-06 02:35:46 +01:00
|
|
|
const response: IpcResponse = {
|
2020-07-25 02:02:37 +02:00
|
|
|
id: payload.id,
|
|
|
|
success: true,
|
|
|
|
data: result,
|
|
|
|
};
|
|
|
|
event.reply(channel, response);
|
|
|
|
})
|
|
|
|
.catch((reason: Error) => {
|
2021-01-06 02:35:46 +01:00
|
|
|
const response: IpcResponse = {
|
2020-07-25 02:02:37 +02:00
|
|
|
id: payload.id,
|
|
|
|
success: false,
|
|
|
|
error: reason.message,
|
|
|
|
};
|
|
|
|
event.reply(channel, response);
|
|
|
|
});
|
|
|
|
});
|
2020-02-08 23:26:57 +01:00
|
|
|
}
|