From a49127f182bed4ad41bb5bf28b0dd52234fa9fa1 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 28 Jan 2021 16:10:49 -0500 Subject: [PATCH] Remove async getSubFrames and move sendToFrame loop - getSubFrames never needed to be async and was needlessly converting it to an array - instead of relying on a bunch of promises that will get executed sometime in the future (after the current function ends). Which is a strange and unexpected use of the JS runtime behaviour. This makes it more explicit. Signed-off-by: Sebastian Malton --- src/common/ipc.ts | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/common/ipc.ts b/src/common/ipc.ts index 369221815b..d19b67dd60 100644 --- a/src/common/ipc.ts +++ b/src/common/ipc.ts @@ -4,7 +4,7 @@ import { ipcMain, ipcRenderer, webContents, remote } from "electron"; import logger from "../main/logger"; -import { ClusterFrameInfo, clusterFrameMap } from "./cluster-frames"; +import { clusterFrameMap } from "./cluster-frames"; export function handleRequest(channel: string, listener: (...args: any[]) => any) { ipcMain.handle(channel, listener); @@ -14,38 +14,31 @@ export async function requestMain(channel: string, ...args: any[]) { return ipcRenderer.invoke(channel, ...args); } -async function getSubFrames(): Promise { - const subFrames: ClusterFrameInfo[] = []; - - clusterFrameMap.forEach(frameInfo => { - subFrames.push(frameInfo); - }); - - return subFrames; -} - export function broadcastMessage(channel: string, ...args: any[]) { const views = (webContents || remote?.webContents)?.getAllWebContents(); if (!views) return; - views.forEach(webContent => { - const type = webContent.getType(); - - logger.silly(`[IPC]: broadcasting "${channel}" to ${type}=${webContent.id}`, { args }); - webContent.send(channel, ...args); - getSubFrames().then((frames) => { - frames.map((frameInfo) => { - webContent.sendToFrame([frameInfo.processId, frameInfo.frameId], channel, ...args); - }); - }).catch((e) => e); - }); - if (ipcRenderer) { ipcRenderer.send(channel, ...args); } else { ipcMain.emit(channel, ...args); } + + for (const view of views) { + const type = view.getType(); + + logger.silly(`[IPC]: broadcasting "${channel}" to ${type}=${view.id}`, { args }); + view.send(channel, ...args); + + try { + for (const frameInfo of clusterFrameMap.values()) { + view.sendToFrame([frameInfo.processId, frameInfo.frameId], channel, ...args); + } + } catch (error) { + logger.error("[IPC]: failed to send IPC message", { error }); + } + } } export function subscribeToBroadcast(channel: string, listener: (...args: any[]) => any) {