1
0
mirror of https://github.com/pavlobu/deskreen.git synced 2025-05-18 00:10:12 -07:00

more ipc invoke, less globals

This commit is contained in:
Pavlo Buidenkov 2022-05-29 11:35:36 +02:00
parent 70e2c47555
commit 19a1da6ff3
6 changed files with 40 additions and 43 deletions

View File

@ -18,6 +18,7 @@ import translationZH_CN from '../locales/zh_CN/translation.json';
import translationZH_TW from '../locales/zh_TW/translation.json';
import translationDA from '../locales/da/translation.json';
import translationDE from '../locales/de/translation.json';
import { IpcEvents } from '../main/IpcEvents.enum';
export const getLangFullNameToLangISOKeyMap = (): Map<string, string> => {
const res = new Map<string, string>();
@ -68,7 +69,7 @@ export const getShuffledArrayOfHello = (): string[] => {
};
async function initI18NextOptions() {
const appPath = await ipcRenderer.invoke('get-app-path');
const appPath = await ipcRenderer.invoke(IpcEvents.GetAppPath);
const i18nextOptions = {
interpolation: {
escapeValue: false,

View File

@ -39,6 +39,7 @@ import { getShuffledArrayOfHello } from '../configs/i18next.config.client';
import ToggleThemeBtnGroup from '../components/ToggleThemeBtnGroup';
import SharingSessionService from '../features/SharingSessionService';
import ConnectedDevicesService from '../features/ConnectedDevicesService';
import { IpcEvents } from '../main/IpcEvents.enum';
const sharingSessionService = remote.getGlobal(
'sharingSessionService'
@ -117,8 +118,8 @@ const DeskreenStepper = React.forwardRef((_props, ref) => {
}, []);
useEffect(() => {
ipcRenderer.invoke('create-waiting-for-connection-sharing-session');
ipcRenderer.on('set-pending-connection-device', (_, device) => {
ipcRenderer.invoke(IpcEvents.CreateWaitingForConnectionSharingSession);
ipcRenderer.on(IpcEvents.SetPendingConnectionDevice, (_, device) => {
setPendingConnectionDevice(device);
setIsAlertOpen(true);
});
@ -184,17 +185,7 @@ const DeskreenStepper = React.forwardRef((_props, ref) => {
setPendingConnectionDevice(null);
setIsUserAllowedConnection(false);
sharingSessionService
.createWaitingForConnectionSharingSession()
// eslint-disable-next-line promise/always-return
.then((waitingForConnectionSharingSession) => {
waitingForConnectionSharingSession.setOnDeviceConnectedCallback(
(device: Device) => {
connectedDevicesService.setPendingConnectionDevice(device);
}
);
})
.catch((e) => log.error(e));
ipcRenderer.invoke(IpcEvents.CreateWaitingForConnectionSharingSession);
}, []);
const handleResetWithSharingSessionRestart = useCallback(() => {
@ -202,24 +193,8 @@ const DeskreenStepper = React.forwardRef((_props, ref) => {
setPendingConnectionDevice(null);
setIsUserAllowedConnection(false);
const sharingSession =
sharingSessionService.waitingForConnectionSharingSession;
sharingSession?.disconnectByHostMachineUser();
sharingSession?.destroy();
sharingSessionService.sharingSessions.delete(sharingSession?.id as string);
sharingSessionService.waitingForConnectionSharingSession = null;
sharingSessionService
.createWaitingForConnectionSharingSession()
// eslint-disable-next-line promise/always-return
.then((waitingForConnectionSharingSession) => {
waitingForConnectionSharingSession.setOnDeviceConnectedCallback(
(device: Device) => {
connectedDevicesService.setPendingConnectionDevice(device);
}
);
})
.catch((e) => log.error(e));
ipcRenderer.invoke(IpcEvents.ResetWaitingForConnectionSharingSession);
ipcRenderer.invoke(IpcEvents.CreateWaitingForConnectionSharingSession);
}, []);
React.useImperativeHandle(ref, () => ({

View File

@ -1,4 +1,5 @@
import { ipcRenderer } from 'electron';
import { IpcEvents } from '../../main/IpcEvents.enum';
import SharingSessionStatusEnum from '../SharingSessionService/SharingSessionStatusEnum';
import NullSimplePeer from './NullSimplePeer';
import NullUser from './NullUser';
@ -29,5 +30,5 @@ export default function handleSelfDestroy(peerConnection: PeerConnection) {
peerConnection.isCallStarted = false;
peerConnection.socket.disconnect();
ipcRenderer.invoke('unmark-room-id-as-taken', peerConnection.roomID);
ipcRenderer.invoke(IpcEvents.UnmarkRoomIDAsTaken, peerConnection.roomID);
}

View File

@ -0,0 +1,8 @@
// eslint-disable-next-line import/prefer-default-export
export enum IpcEvents {
CreateWaitingForConnectionSharingSession = 'create-waiting-for-connection-sharing-session',
SetPendingConnectionDevice = 'set-pending-connection-device',
UnmarkRoomIDAsTaken = 'unmark-room-id-as-taken',
GetAppPath = 'get-app-path',
ResetWaitingForConnectionSharingSession = 'reset-waiting-for-connection-sharing-session',
}

View File

@ -7,6 +7,7 @@ import RoomIDService from '../server/RoomIDService';
import getDeskreenGlobal from '../utils/mainProcessHelpers/getDeskreenGlobal';
import signalingServer from '../server';
import Logger from '../utils/LoggerWithFilePrefix';
import { IpcEvents } from './IpcEvents.enum';
const log = new Logger(__filename);
const v4IPGetter = require('internal-ip').v4;
@ -82,18 +83,17 @@ export default function initIpcMainHandlers(
return '255.255.255.255';
});
ipcMain.handle('get-app-path', () => {
ipcMain.handle(IpcEvents.GetAppPath, () => {
const deskreenGlobal = getDeskreenGlobal();
return deskreenGlobal.appPath;
});
ipcMain.handle('unmark-room-id-as-taken', (_, roomID) => {
ipcMain.handle(IpcEvents.UnmarkRoomIDAsTaken, (_, roomID) => {
const deskreenGlobal = getDeskreenGlobal();
deskreenGlobal.roomIDService.unmarkRoomIDAsTaken(roomID);
});
ipcMain.handle('create-waiting-for-connection-sharing-session', () => {
console.log('handle create-waiting-for-connection-sharing-session');
ipcMain.handle(IpcEvents.CreateWaitingForConnectionSharingSession, () => {
getDeskreenGlobal()
.sharingSessionService.createWaitingForConnectionSharingSession()
// eslint-disable-next-line promise/always-return
@ -104,7 +104,7 @@ export default function initIpcMainHandlers(
device
);
mainWindow.webContents.send(
'set-pending-connection-device',
IpcEvents.SetPendingConnectionDevice,
device
);
}
@ -112,4 +112,15 @@ export default function initIpcMainHandlers(
})
.catch((e) => log.error(e));
});
ipcMain.handle(IpcEvents.ResetWaitingForConnectionSharingSession, () => {
const sharingSession = getDeskreenGlobal().sharingSessionService
.waitingForConnectionSharingSession;
sharingSession?.disconnectByHostMachineUser();
sharingSession?.destroy();
getDeskreenGlobal().sharingSessionService.sharingSessions.delete(
sharingSession?.id as string
);
getDeskreenGlobal().sharingSessionService.waitingForConnectionSharingSession = null;
});
}

View File

@ -215,11 +215,12 @@ export default class MenuBuilder {
],
};
const subMenuView =
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
? subMenuViewDev
: subMenuViewProd;
// const subMenuView =
// process.env.NODE_ENV === 'development' ||
// process.env.DEBUG_PROD === 'true'
// ? subMenuViewDev
// : subMenuViewProd;
const subMenuView = subMenuViewDev;
return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp];
}