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-11-18 22:38:51 +01:00
|
|
|
import { mainWindow } from '../../main';
|
|
|
|
import { isLoggedIn, login } from '../services/nhentai-crawler';
|
2019-06-16 00:41:43 +02:00
|
|
|
|
2019-11-18 22:38:51 +01:00
|
|
|
export const ipcServer: IIpcServer = {
|
2019-07-26 23:05:29 +02:00
|
|
|
answer: (channel: IpcChannels, handler: (data?: any) => Promise<any>): void => {
|
2019-10-02 23:01:18 +02:00
|
|
|
ipcMain.on(channel, (event: IpcMainEvent, payload: IIpcPayload) => {
|
2019-07-26 23:05:29 +02:00
|
|
|
handler(payload.data)
|
|
|
|
.then((result: any) => {
|
|
|
|
const response: IIpcResponse = {
|
2019-07-30 23:58:58 +02:00
|
|
|
id: payload.id,
|
2019-07-26 23:05:29 +02:00
|
|
|
success: true,
|
|
|
|
data: result,
|
|
|
|
};
|
|
|
|
event.reply(channel, response);
|
|
|
|
})
|
|
|
|
.catch((reason: any) => {
|
|
|
|
const response: IIpcResponse = {
|
2019-07-30 23:58:58 +02:00
|
|
|
id: payload.id,
|
2019-07-26 23:05:29 +02:00
|
|
|
success: false,
|
|
|
|
error: reason.toString(),
|
|
|
|
};
|
|
|
|
event.reply(channel, response);
|
|
|
|
});
|
2019-06-30 01:18:21 +02:00
|
|
|
});
|
2019-07-26 23:05:29 +02:00
|
|
|
},
|
2019-11-18 22:38:51 +01:00
|
|
|
send: (channel: IpcChannels, data: any): void => {
|
|
|
|
mainWindow.webContents.send(channel, data);
|
|
|
|
},
|
2019-07-26 23:05:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ipcServer.answer(IpcChannels.LOGIN, (credentials: ICredentials) => {
|
|
|
|
return login(credentials.name, credentials.password);
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcServer.answer(IpcChannels.LOGGED_IN, () => {
|
|
|
|
return isLoggedIn();
|
2019-06-30 01:18:21 +02:00
|
|
|
});
|