Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
type IOpenExternal,
} from '../shared/events';
import { logInfo, logWarn } from '../shared/logger';
import { isLinux, isWindows } from '../shared/platform';

import { handleMainEvent, onMainEvent, sendRendererEvent } from './events';
import { onFirstRunMaybe } from './first-run';
Expand Down Expand Up @@ -78,6 +77,8 @@ let shouldUseAlternateIdleIcon = false;
let shouldUseUnreadActiveIcon = true;

app.whenReady().then(async () => {
preventSecondInstance();

await onFirstRunMaybe();

appUpdater.start();
Expand Down Expand Up @@ -116,31 +117,6 @@ app.whenReady().then(async () => {
});
});

/** Prevent second instances */
if (isWindows() || isLinux()) {
const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
logWarn('main:gotTheLock', 'Second instance detected, quitting');
app.quit(); // Quit the second instance
return;
}

app.on('second-instance', (_event, commandLine, _workingDirectory) => {
logInfo(
'main:second-instance',
'Second instance was launched. extracting command to forward',
);

// Get the URL from the command line arguments
const url = commandLine.find((arg) => arg.startsWith(`${protocol}://`));

if (url) {
handleURL(url);
}
});
}

/**
* Gitify custom IPC events - no response expected
*/
Expand Down Expand Up @@ -280,3 +256,30 @@ function setActiveIcon() {
function setErrorIcon() {
mb.tray.setImage(TrayIcons.error);
}

/**
* Prevent second instances
*/
function preventSecondInstance() {
const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
logWarn('main:gotTheLock', 'Second instance detected, quitting');
app.quit(); // Quit the second instance
return;
}

app.on('second-instance', (_event, commandLine, _workingDirectory) => {
logInfo(
'main:second-instance',
'Second instance was launched. extracting command to forward',
);

// Get the URL from the command line arguments
const url = commandLine.find((arg) => arg.startsWith(`${protocol}://`));

if (url) {
handleURL(url);
}
});
}