From 7cbfbfc5f11a8e61e6f0b54702401b72e2ad3aea Mon Sep 17 00:00:00 2001 From: Kersch <> Date: Sat, 30 May 2020 17:21:29 -0700 Subject: [PATCH 1/2] fix: singleInstance w/ electron 4+ --- skeleton/app.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/skeleton/app.js b/skeleton/app.js index 44cb7ece..ada20e6f 100644 --- a/skeleton/app.js +++ b/skeleton/app.js @@ -128,16 +128,28 @@ export default class App { applySingleInstance() { if ('singleInstance' in this.settings && this.settings.singleInstance) { this.l.verbose('setting single instance mode'); - const isSecondInstance = app.makeSingleInstance(() => { - // Someone tried to run a second instance, we should focus our window. - if (this.window) { - if (this.window.isMinimized()) { - this.window.restore(); + if (semver.lt(process.versions.electron, '4.0')) { //https://www.electronjs.org/docs/all#appmakesingleinstance + const isSecondInstance = app.makeSingleInstance(() => { + // Someone tried to run a second instance, we should focus our window. + if (this.window) { + if (this.window.isMinimized()) { + this.window.restore(); + } + this.window.focus(); } - this.window.focus(); - } - }); - + }); + } else { + app.requestSingleInstanceLock() + const isSecondInstance = app.on(() => { + // Someone tried to run a second instance, we should focus our window. + if (this.window) { + if (this.window.isMinimized()) { + this.window.restore(); + } + this.window.focus(); + } + }); + } if (isSecondInstance) { this.l.warn('current instance was terminated because another instance is running'); app.quit(); From 135b46797f4324d7a5b24a5c62841a7b0fcdbd89 Mon Sep 17 00:00:00 2001 From: Kersch <> Date: Sat, 30 May 2020 17:58:40 -0700 Subject: [PATCH 2/2] fix second-instance function for app.on --- skeleton/app.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/skeleton/app.js b/skeleton/app.js index ada20e6f..5a522955 100644 --- a/skeleton/app.js +++ b/skeleton/app.js @@ -128,7 +128,7 @@ export default class App { applySingleInstance() { if ('singleInstance' in this.settings && this.settings.singleInstance) { this.l.verbose('setting single instance mode'); - if (semver.lt(process.versions.electron, '4.0')) { //https://www.electronjs.org/docs/all#appmakesingleinstance + if (semver.lt(process.versions.electron, '4.0.0')) { //https://www.electronjs.org/docs/all#appmakesingleinstance const isSecondInstance = app.makeSingleInstance(() => { // Someone tried to run a second instance, we should focus our window. if (this.window) { @@ -140,7 +140,7 @@ export default class App { }); } else { app.requestSingleInstanceLock() - const isSecondInstance = app.on(() => { + const isSecondInstance = app.on('second-instance', () => { // Someone tried to run a second instance, we should focus our window. if (this.window) { if (this.window.isMinimized()) { @@ -148,12 +148,10 @@ export default class App { } this.window.focus(); } + this.l.warn('current instance was terminated because another instance is running'); + app.quit(); }); } - if (isSecondInstance) { - this.l.warn('current instance was terminated because another instance is running'); - app.quit(); - } } }