From 3c1672c38f9a1d83b4317ddd634da2e2fff16c72 Mon Sep 17 00:00:00 2001 From: Cafe137 Date: Tue, 7 Jun 2022 16:11:05 +0200 Subject: [PATCH] feat: make app singleton --- src/electron.ts | 14 ++++++++++++-- src/notify.ts | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/notify.ts diff --git a/src/electron.ts b/src/electron.ts index 8560204..d304bae 100644 --- a/src/electron.ts +++ b/src/electron.ts @@ -1,9 +1,9 @@ import { app, Menu, Tray } from 'electron' -import Notifier from 'node-notifier' import { openDashboardInBrowser, openInstallerInBrowser } from './browser' import { runDownloader } from './downloader' import { runLauncher } from './launcher' import { BeeManager } from './lifecycle' +import { createNotification } from './notify' import { getPath } from './path' import { getStatus } from './status' @@ -70,6 +70,16 @@ export function rebuildElectronTray() { } export function runElectronTray() { + const gotTheLock = app.requestSingleInstanceLock() + + if (!gotTheLock) { + app.quit() + } else { + app.on('second-instance', () => { + createNotification('Swarm is already running. Please close the previous instance first.') + }) + } + app.whenReady().then(() => { if (app.dock) { app.dock.setIcon(getPath('icon.png')) @@ -85,7 +95,7 @@ async function redownloadAssets(): Promise { BeeManager.stop() } await runDownloader(true) - Notifier.notify({ title: 'Swarm', message: 'New assets fetched successfully' }) + createNotification('New assets fetched successfully') if (getStatus().hasInitialTransaction) { runLauncher() diff --git a/src/notify.ts b/src/notify.ts new file mode 100644 index 0000000..69140e7 --- /dev/null +++ b/src/notify.ts @@ -0,0 +1,5 @@ +import Notifier from 'node-notifier' + +export function createNotification(message: string): void { + Notifier.notify({ title: 'Swarm', message }) +}