* Enforce a single application instance. If a second instance is launched, * any protocol URL in the command line is forwarded to the existing instance. * * @param mb - The menubar instance to show when a second instance is detected. * @param protocol - The custom protocol string to extract from
(mb: Menubar, protocol: string)
| 62 | * @param protocol - The custom protocol string to extract from command line args. |
| 63 | */ |
| 64 | function preventSecondInstance(mb: Menubar, protocol: string): void { |
| 65 | const gotTheLock = app.requestSingleInstanceLock(); |
| 66 | |
| 67 | if (!gotTheLock) { |
| 68 | logWarn('main:gotTheLock', 'Second instance detected, quitting'); |
| 69 | app.quit(); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | app.on('second-instance', (_event, commandLine) => { |
| 74 | logInfo( |
| 75 | 'main:second-instance', |
| 76 | 'Second instance was launched. Extracting command to forward', |
| 77 | ); |
| 78 | |
| 79 | const url = commandLine.find((arg) => arg.startsWith(`${protocol}://`)); |
| 80 | |
| 81 | if (url) { |
| 82 | handleProtocolURL(mb, url, protocol); |
| 83 | } |
| 84 | }); |
| 85 | } |
no test coverage detected