Skip to content

Commit

Permalink
Ensure relaunch is only called once (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
andrewburgess and sindresorhus authored Feb 22, 2020
1 parent ad01c78 commit e7101a6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = (moduleObject, options) => {
const cwd = packageDirectory ? path.dirname(packageDirectory) : mainProcessDirectory;
const mainProcessPaths = getMainProcessPaths(moduleObject, cwd);
const watchPaths = options.watchRenderer ? cwd : [...mainProcessPaths];
let isRelaunching = false;

const watcher = chokidar.watch(watchPaths, {
cwd,
Expand All @@ -71,8 +72,14 @@ module.exports = (moduleObject, options) => {
}

if (mainProcessPaths.has(path.join(cwd, filePath))) {
electron.app.relaunch();
electron.app.exit(0);
// Prevent multiple instances of Electron from being started due to the change
// handler being called multiple times before the original instance exits.
if (!isRelaunching) {
electron.app.relaunch();
electron.app.exit(0);
}

isRelaunching = true;
} else {
for (const window_ of electron.BrowserWindow.getAllWindows()) {
window_.webContents.reloadIgnoringCache();
Expand Down

0 comments on commit e7101a6

Please sign in to comment.