From 00dca3ba9b547e5813a16010f2815c2ba4db7b5e Mon Sep 17 00:00:00 2001 From: Ishan Karmakar Date: Mon, 1 Jun 2026 14:50:12 -0500 Subject: [PATCH] feat: Use single instance lock to avoid multiple instances of Vynte Connect --- src/main.ts | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index 49dd365..6934f6c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,10 +10,29 @@ if (started) { app.quit(); } -// This method will be called when Electron has finished -// initialization and is ready to create browser windows. -// Some APIs can only be used after this event occurs. -app.on('ready', createWindow); +const singleInstanceLock = app.requestSingleInstanceLock() + +if (!singleInstanceLock) app.quit(); +else { + app.on('second-instance', () => { + if (window) { + if (window.isMinimized()) window.restore(); + window.focus(); + } + }); + app.on('before-quit', disconnect); +} + +app.whenReady().then(() => { + createWindow(); + createTray(); + refreshStatus(); + pollTimer = setInterval(refreshStatus, POLL_INTERVAL_MS); +}); + +app.on('before-quit', () => { + if (pollTimer) clearInterval(pollTimer); +}); // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits @@ -24,8 +43,6 @@ app.on('window-all-closed', () => { } }); -app.on('before-quit', disconnect); - app.on('activate', () => { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. @@ -209,17 +226,6 @@ function createTray() { tray.on('click', showWindow); } -app.whenReady().then(() => { - createWindow(); - createTray(); - refreshStatus(); - pollTimer = setInterval(refreshStatus, POLL_INTERVAL_MS); -}); - -app.on('before-quit', () => { - if (pollTimer) clearInterval(pollTimer); -}); - ipcMain.handle('status:get', () => withAutoConnect(cachedStatus)); ipcMain.handle('status:refresh', refreshStatus); ipcMain.handle('vpn:connect', connect);