From f71cda65efcd76935c03332ec1080f8084f1269f Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 20 Mar 2016 03:54:25 -0700 Subject: [PATCH] Prevent multiple instances of the app from running at the same time Fix #167 --- main/index.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/main/index.js b/main/index.js index d95109ed..7ae290f5 100644 --- a/main/index.js +++ b/main/index.js @@ -9,7 +9,32 @@ var registerProtocolHandler = require('./register-handlers') var shortcuts = require('./shortcuts') var windows = require('./windows') -var argv = process.argv.slice(config.IS_PRODUCTION ? 1 : 2) +// Prevent multiple instances of the app from running at the same time. New instances +// signal this instance and exit. +var shouldQuit = app.makeSingleInstance(function (newArgv) { + newArgv = sliceArgv(newArgv) + + if (app.ipcReady) { + windows.main.send('log', 'Second app instance attempted to open but was prevented') + + newArgv.forEach(function (torrentId) { + windows.main.send('dispatch', 'onOpen', torrentId) + }) + + if (windows.main.isMinimized()) { + windows.main.restore() + } + windows.main.focus() + } else { + argv.push(...newArgv) + } +}) + +if (shouldQuit) { + app.quit() +} + +var argv = sliceArgv(process.argv) app.on('open-file', onOpen) app.on('open-url', onOpen) @@ -62,3 +87,7 @@ function onOpen (e, torrentId) { argv.push(torrentId) } } + +function sliceArgv (argv) { + return argv.slice(config.IS_PRODUCTION ? 1 : 2) +}