mirror of
https://github.com/oliverschwendener/ueli.git
synced 2024-10-04 01:57:45 +03:00
Merge e227333737
into 8d183c60f6
This commit is contained in:
commit
5045393379
@ -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();
|
||||
});
|
||||
});
|
||||
|
@ -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");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user