From 19a1da6ff3adcaaa858b92c4ebb73585e91c5bd9 Mon Sep 17 00:00:00 2001 From: Pavlo Buidenkov Date: Sun, 29 May 2022 11:35:36 +0200 Subject: [PATCH] more ipc invoke, less globals --- app/configs/i18next.config.client.ts | 3 +- app/containers/DeskreenStepper.tsx | 37 +++---------------- .../PeerConnection/handleSelfDestroy.ts | 3 +- app/main/IpcEvents.enum.ts | 8 ++++ app/main/ipcMainHandlers.ts | 21 ++++++++--- app/menu.ts | 11 +++--- 6 files changed, 40 insertions(+), 43 deletions(-) create mode 100644 app/main/IpcEvents.enum.ts diff --git a/app/configs/i18next.config.client.ts b/app/configs/i18next.config.client.ts index 1bf9ad9..86e2666 100644 --- a/app/configs/i18next.config.client.ts +++ b/app/configs/i18next.config.client.ts @@ -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 => { const res = new Map(); @@ -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, diff --git a/app/containers/DeskreenStepper.tsx b/app/containers/DeskreenStepper.tsx index 68eb8eb..bddcb2f 100644 --- a/app/containers/DeskreenStepper.tsx +++ b/app/containers/DeskreenStepper.tsx @@ -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, () => ({ diff --git a/app/features/PeerConnection/handleSelfDestroy.ts b/app/features/PeerConnection/handleSelfDestroy.ts index 1c73c1a..d3d823a 100644 --- a/app/features/PeerConnection/handleSelfDestroy.ts +++ b/app/features/PeerConnection/handleSelfDestroy.ts @@ -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); } diff --git a/app/main/IpcEvents.enum.ts b/app/main/IpcEvents.enum.ts new file mode 100644 index 0000000..e8d970b --- /dev/null +++ b/app/main/IpcEvents.enum.ts @@ -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', +} diff --git a/app/main/ipcMainHandlers.ts b/app/main/ipcMainHandlers.ts index 80dd019..170eba8 100644 --- a/app/main/ipcMainHandlers.ts +++ b/app/main/ipcMainHandlers.ts @@ -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; + }); } diff --git a/app/menu.ts b/app/menu.ts index b269202..34ba28a 100644 --- a/app/menu.ts +++ b/app/menu.ts @@ -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]; }