This commit is contained in:
Kelvin Wu 2024-09-27 09:32:20 +02:00 committed by GitHub
commit 5045393379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 34 deletions

View File

@ -1,32 +1,34 @@
import type { Dependencies } from "@Core/Dependencies";
import type { DependencyRegistry } from "@Core/DependencyRegistry";
import type { EventEmitter } from "@Core/EventEmitter";
import type { App } from "electron";
import { describe, expect, it, vi } from "vitest";
import { SingleInstanceLockModule } from "./SingleInstanceLockModule";
describe(SingleInstanceLockModule, () => {
it("should request single instance lock", () => {
const requestSingleInstanceLockMock = vi.fn().mockReturnValue(true);
const quickMock = vi.fn();
it("should register the event listener and event emitter", () => {
const on = vi.fn();
const app = <App>{
on: (event: string, callback: () => void) => on(event, callback),
};
SingleInstanceLockModule.bootstrap(<App>{
requestSingleInstanceLock: () => requestSingleInstanceLockMock(),
quit: () => quickMock(),
});
const emitEvent = vi.fn();
const eventEmitter = <EventEmitter>{
emitEvent: (event: string) => emitEvent(event),
};
expect(requestSingleInstanceLockMock).toHaveBeenCalledOnce();
expect(quickMock).not.toHaveBeenCalled();
});
const dependencyRegistry = <DependencyRegistry<Dependencies>>{
get: (key: keyof Dependencies) => {
const result = <Dependencies>{
App: app,
EventEmitter: eventEmitter,
};
it("should quit the application if another instance is already running", () => {
const requestSingleInstanceLockMock = vi.fn().mockReturnValue(false);
return result[key];
},
};
const quickMock = vi.fn();
SingleInstanceLockModule.bootstrap(<App>{
requestSingleInstanceLock: () => requestSingleInstanceLockMock(),
quit: () => quickMock(),
});
expect(requestSingleInstanceLockMock).toHaveBeenCalledOnce();
expect(quickMock).toHaveBeenCalledOnce();
SingleInstanceLockModule.bootstrap(dependencyRegistry);
expect(on).toHaveBeenCalled();
});
});

View File

@ -1,10 +1,13 @@
import type { App } from "electron";
import type { Dependencies } from "@Core/Dependencies";
import type { DependencyRegistry } from "@Core/DependencyRegistry";
export class SingleInstanceLockModule {
public static bootstrap(app: App) {
if (!app.requestSingleInstanceLock()) {
console.log("Quitting application. Reason: another instance is already running");
app.quit();
}
public static bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>) {
const eventEmitter = dependencyRegistry.get("EventEmitter");
const app = dependencyRegistry.get("App");
app.on("second-instance", () => {
eventEmitter.emitEvent("hotkeyPressed");
});
}
}

View File

@ -1,13 +1,13 @@
import * as Electron from "electron";
import mitt from "mitt";
import { platform } from "os";
import * as Core from "./Core";
import * as Extensions from "./Extensions";
(async () => {
const main = async () => {
await Electron.app.whenReady();
Core.SingleInstanceLockModule.bootstrap(Electron.app);
const mitt = (await import("mitt")).default;
const platform = (await import("os")).platform;
const Core = await import("./Core");
const Extensions = await import("./Extensions");
Core.DockModule.bootstrap(Electron.app);
const dependencyRegistry = Core.DependencyRegistryModule.bootstrap();
@ -37,6 +37,7 @@ import * as Extensions from "./Extensions";
Core.XmlParserModule.bootstrap(dependencyRegistry);
Core.EventEmitterModule.bootstrap(dependencyRegistry);
Core.EventSubscriberModule.bootstrap(dependencyRegistry);
Core.SingleInstanceLockModule.bootstrap(dependencyRegistry);
Core.BrowserWindowNotifierModule.bootstrap(dependencyRegistry);
Core.DateProviderModule.bootstrap(dependencyRegistry);
Core.LoggerModule.bootstrap(dependencyRegistry);
@ -77,4 +78,10 @@ import * as Extensions from "./Extensions";
await Core.BrowserWindowModule.bootstrap(dependencyRegistry);
Core.RescanOrchestratorModule.bootstrap(dependencyRegistry);
})();
};
if (Electron.app.requestSingleInstanceLock()) {
main();
} else {
Electron.app.exit();
}