Skip to content

Commit 944bbc7

Browse files
authored
refactor: second instance handling (#2285)
* refactor: second instance handling Signed-off-by: Adam Setch <adam.setch@outlook.com> * refactor: second instance handling Signed-off-by: Adam Setch <adam.setch@outlook.com> --------- Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 8321c80 commit 944bbc7

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/main/index.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
type IOpenExternal,
1919
} from '../shared/events';
2020
import { logInfo, logWarn } from '../shared/logger';
21-
import { isLinux, isWindows } from '../shared/platform';
2221

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

8079
app.whenReady().then(async () => {
80+
preventSecondInstance();
81+
8182
await onFirstRunMaybe();
8283

8384
appUpdater.start();
@@ -116,31 +117,6 @@ app.whenReady().then(async () => {
116117
});
117118
});
118119

119-
/** Prevent second instances */
120-
if (isWindows() || isLinux()) {
121-
const gotTheLock = app.requestSingleInstanceLock();
122-
123-
if (!gotTheLock) {
124-
logWarn('main:gotTheLock', 'Second instance detected, quitting');
125-
app.quit(); // Quit the second instance
126-
return;
127-
}
128-
129-
app.on('second-instance', (_event, commandLine, _workingDirectory) => {
130-
logInfo(
131-
'main:second-instance',
132-
'Second instance was launched. extracting command to forward',
133-
);
134-
135-
// Get the URL from the command line arguments
136-
const url = commandLine.find((arg) => arg.startsWith(`${protocol}://`));
137-
138-
if (url) {
139-
handleURL(url);
140-
}
141-
});
142-
}
143-
144120
/**
145121
* Gitify custom IPC events - no response expected
146122
*/
@@ -280,3 +256,30 @@ function setActiveIcon() {
280256
function setErrorIcon() {
281257
mb.tray.setImage(TrayIcons.error);
282258
}
259+
260+
/**
261+
* Prevent second instances
262+
*/
263+
function preventSecondInstance() {
264+
const gotTheLock = app.requestSingleInstanceLock();
265+
266+
if (!gotTheLock) {
267+
logWarn('main:gotTheLock', 'Second instance detected, quitting');
268+
app.quit(); // Quit the second instance
269+
return;
270+
}
271+
272+
app.on('second-instance', (_event, commandLine, _workingDirectory) => {
273+
logInfo(
274+
'main:second-instance',
275+
'Second instance was launched. extracting command to forward',
276+
);
277+
278+
// Get the URL from the command line arguments
279+
const url = commandLine.find((arg) => arg.startsWith(`${protocol}://`));
280+
281+
if (url) {
282+
handleURL(url);
283+
}
284+
});
285+
}

0 commit comments

Comments
 (0)