From e8c6abe86ffe03c2b91fc74e292def16d6b58be8 Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Fri, 18 Mar 2022 16:53:57 +0100 Subject: [PATCH] fix(compass): Catch all errors during theme update --- packages/compass/src/main/menu.ts | 48 ++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/compass/src/main/menu.ts b/packages/compass/src/main/menu.ts index 3cbbcf2c4ca..76baece9bca 100644 --- a/packages/compass/src/main/menu.ts +++ b/packages/compass/src/main/menu.ts @@ -1,14 +1,5 @@ -// based off of https://github.com/atom/atom/blob/master/src/browser/application-menu.coffee -// use js2.coffee to convert it to JS -import { - BrowserWindow, - Menu, - app, - dialog, - shell, - nativeTheme, - MenuItemConstructorOptions, -} from 'electron'; +import type { MenuItemConstructorOptions } from 'electron'; +import { BrowserWindow, Menu, app, dialog, shell, nativeTheme } from 'electron'; import { ipcMain } from 'hadron-ipc'; import fs from 'fs'; import path from 'path'; @@ -31,21 +22,32 @@ class ThemeState { function updateTheme({ theme }: ThemeState) { - if (theme === THEMES.OS_THEME) { - if (nativeTheme.shouldUseDarkColors) { + try { + if (theme === THEMES.OS_THEME) { + if (nativeTheme.shouldUseDarkColors) { + ipcMain.broadcast('app:darkreader-enable'); + } else { + ipcMain.broadcast('app:darkreader-disable'); + } + return; + } + + if (theme === THEMES.DARK) { ipcMain.broadcast('app:darkreader-enable'); - } else { - ipcMain.broadcast('app:darkreader-disable'); + return; } - return; - } - - if (theme === THEMES.DARK) { - ipcMain.broadcast('app:darkreader-enable'); - return; + + ipcMain.broadcast('app:darkreader-disable'); + } catch (e) { + // TODO: Seems like sometimes we are broadcasting theme update too fast + // causing ipc.send to fail with "Render frame was disposed before + // WebFrameMain could be accessed" error. + // + // This is a workaround that just swallows the issue and allows app not to + // break while trying to switch the theme. We should inspect the + // implementation and do a better fix in the scope of https://jira.mongodb.org/browse/COMPASS-5606 + debug('Failed to broadcast theme change', e); } - - ipcMain.broadcast('app:darkreader-disable'); } function separator(): MenuItemConstructorOptions {