Skip to content

Commit

Permalink
Fix error when window is already destroyed (#126)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dertieran and sindresorhus committed Dec 21, 2020
1 parent a888718 commit 31c6f54
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ const create = (win, options) => {
webContents(win).on('context-menu', handleContextMenu);

return () => {
if (win.isDestroyed()) {
return;
}

webContents(win).removeListener('context-menu', handleContextMenu);
};
};
Expand All @@ -324,8 +328,19 @@ module.exports = (options = {}) => {
}

const disposeMenu = create(win, options);

disposables.push(disposeMenu);
const removeDisposable = () => {
const index = disposables.indexOf(disposeMenu);
if (index !== -1) {
disposables.splice(index, 1);
}
};

win.once('closed', removeDisposable);

disposables.push(() => {
disposeMenu();
win.off('closed', removeDisposable);
});
};

Expand Down

0 comments on commit 31c6f54

Please sign in to comment.