Skip to content

Commit

Permalink
[core] Fix listener leak error on electron
Browse files Browse the repository at this point in the history
`electron-main-menu-factory` was registering a lot of listeners.
This commit refactors the listeners into one, reducing the count and avoiding the error.

Fix #2208.

Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Jul 26, 2018
1 parent 678b1d9 commit 4e38f9b
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ import { PreferenceService, KeybindingRegistry, Keybinding, KeyCode, Key } from
export class ElectronMainMenuFactory {

protected _menu: Electron.Menu;
protected _toggledCommands: Set<string> = new Set();

constructor(
@inject(CommandRegistry) protected readonly commandRegistry: CommandRegistry,
@inject(PreferenceService) protected readonly preferencesService: PreferenceService,
@inject(MenuModelRegistry) protected readonly menuProvider: MenuModelRegistry,
@inject(KeybindingRegistry) protected readonly keybindingRegistry: KeybindingRegistry
) { }
) {
this.preferencesService.onPreferenceChanged(() => {
for (const item of this._toggledCommands) {
this._menu.getMenuItemById(item).checked = this.commandRegistry.isToggled(item);
electron.remote.getCurrentWindow().setMenu(this._menu);
}
});
}

createMenuBar(): Electron.Menu {
const menuModel = this.menuProvider.getMenu(MAIN_MENU_BAR);
Expand All @@ -53,7 +61,6 @@ export class ElectronMainMenuFactory {
}

protected fillMenuTemplate(items: Electron.MenuItemConstructorOptions[], menuModel: CompositeMenuNode): Electron.MenuItemConstructorOptions[] {
const toggledCommands: string[] = [];
for (const menu of menuModel.children) {
if (menu instanceof CompositeMenuNode) {
if (menu.label) {
Expand Down Expand Up @@ -99,16 +106,10 @@ export class ElectronMainMenuFactory {
accelerator
});
if (this.commandRegistry.getToggledHandler(commandId)) {
toggledCommands.push(commandId);
this._toggledCommands.add(commandId);
}
}
}
this.preferencesService.onPreferenceChanged(() => {
for (const item of toggledCommands) {
this._menu.getMenuItemById(item).checked = this.commandRegistry.isToggled(item);
electron.remote.getCurrentWindow().setMenu(this._menu);
}
});
return items;
}

Expand Down

0 comments on commit 4e38f9b

Please sign in to comment.