1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Make ipc.boradcast not stopping at uncaught exceptions, improve typing, improve error logs (#3717)

* Improve typing, print better error message instead of stop at throwing at view.getType(); continue to send to subframe even if one throws

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>

* Remove console.log

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>

* Remove extra line

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
chh 2021-09-02 13:20:18 +03:00 committed by GitHub
parent 56c35da046
commit 366a2f99a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,8 +28,9 @@ import { toJS } from "../utils/toJS";
import logger from "../../main/logger"; import logger from "../../main/logger";
import { ClusterFrameInfo, clusterFrameMap } from "../cluster-frames"; import { ClusterFrameInfo, clusterFrameMap } from "../cluster-frames";
import type { Disposer } from "../utils"; import type { Disposer } from "../utils";
import type remote from "@electron/remote";
const remote = ipcMain ? null : require("@electron/remote"); const electronRemote = ipcMain ? null : require("@electron/remote");
const subFramesChannel = "ipc:get-sub-frames"; const subFramesChannel = "ipc:get-sub-frames";
@ -48,9 +49,9 @@ function getSubFrames(): ClusterFrameInfo[] {
} }
export function broadcastMessage(channel: string, ...args: any[]) { export function broadcastMessage(channel: string, ...args: any[]) {
const views = (webContents || remote?.webContents)?.getAllWebContents(); const views: undefined | ReturnType<typeof webContents.getAllWebContents> | ReturnType<typeof remote.webContents.getAllWebContents> = (webContents || electronRemote?.webContents)?.getAllWebContents();
if (!views) return; if (!views || !Array.isArray(views) || views.length === 0) return;
args = args.map(sanitizePayload); args = args.map(sanitizePayload);
ipcRenderer?.send(channel, ...args); ipcRenderer?.send(channel, ...args);
@ -63,15 +64,32 @@ export function broadcastMessage(channel: string, ...args: any[]) {
subFramesP subFramesP
.then(subFrames => { .then(subFrames => {
for (const view of views) { for (const view of views) {
try { let viewType = "unknown";
logger.silly(`[IPC]: broadcasting "${channel}" to ${view.getType()}=${view.id}`, { args });
view.send(channel, ...args);
for (const frameInfo of subFrames) { // There will be a uncaught exception if the view is destroyed.
view.sendToFrame([frameInfo.processId, frameInfo.frameId], channel, ...args); try {
} viewType = view.getType();
} catch {
// We can ignore the view destroyed exception as viewType is only used for logging.
}
// Send message to views.
try {
logger.silly(`[IPC]: broadcasting "${channel}" to ${viewType}=${view.id}`, { args });
view.send(channel, ...args);
} catch (error) { } catch (error) {
logger.error("[IPC]: failed to send IPC message", { error: String(error) }); logger.error(`[IPC]: failed to send IPC message "${channel}" to view "${viewType}=${view.id}"`, { error: String(error) });
}
// Send message to subFrames of views.
for (const frameInfo of subFrames) {
logger.silly(`[IPC]: broadcasting "${channel}" to subframe "frameInfo.processId"=${frameInfo.processId} "frameInfo.frameId"=${frameInfo.frameId}`, { args });
try {
view.sendToFrame([frameInfo.processId, frameInfo.frameId], channel, ...args);
} catch (error) {
logger.error(`[IPC]: failed to send IPC message "${channel}" to view "${viewType}=${view.id}"'s subframe "frameInfo.processId"=${frameInfo.processId} "frameInfo.frameId"=${frameInfo.frameId}`, { error: String(error) });
}
} }
} }
}); });