mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract app lifecycle dependency builder
This commit is contained in:
79
src/core/services/app-lifecycle-deps-runtime-service.test.ts
Normal file
79
src/core/services/app-lifecycle-deps-runtime-service.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createAppLifecycleDepsRuntimeService } from "./app-lifecycle-deps-runtime-service";
|
||||
|
||||
test("createAppLifecycleDepsRuntimeService maps app methods and platform", async () => {
|
||||
const events: Record<string, (...args: unknown[]) => void> = {};
|
||||
let lockRequested = 0;
|
||||
let quitCalled = 0;
|
||||
const deps = createAppLifecycleDepsRuntimeService({
|
||||
app: {
|
||||
requestSingleInstanceLock: () => {
|
||||
lockRequested += 1;
|
||||
return true;
|
||||
},
|
||||
quit: () => {
|
||||
quitCalled += 1;
|
||||
},
|
||||
on: (event, listener) => {
|
||||
events[event] = listener;
|
||||
},
|
||||
whenReady: async () => {},
|
||||
},
|
||||
platform: "darwin",
|
||||
shouldStartApp: () => true,
|
||||
parseArgs: () => ({
|
||||
start: false,
|
||||
stop: false,
|
||||
toggle: false,
|
||||
toggleVisibleOverlay: false,
|
||||
toggleInvisibleOverlay: false,
|
||||
settings: false,
|
||||
show: false,
|
||||
hide: false,
|
||||
showVisibleOverlay: false,
|
||||
hideVisibleOverlay: false,
|
||||
showInvisibleOverlay: false,
|
||||
hideInvisibleOverlay: false,
|
||||
copySubtitle: false,
|
||||
copySubtitleMultiple: false,
|
||||
mineSentence: false,
|
||||
mineSentenceMultiple: false,
|
||||
updateLastCardFromClipboard: false,
|
||||
toggleSecondarySub: false,
|
||||
triggerFieldGrouping: false,
|
||||
triggerSubsync: false,
|
||||
markAudioCard: false,
|
||||
openRuntimeOptions: false,
|
||||
texthooker: false,
|
||||
help: false,
|
||||
autoStartOverlay: false,
|
||||
generateConfig: false,
|
||||
backupOverwrite: false,
|
||||
verbose: false,
|
||||
}),
|
||||
handleCliCommand: () => {},
|
||||
printHelp: () => {},
|
||||
logNoRunningInstance: () => {},
|
||||
onReady: async () => {},
|
||||
onWillQuitCleanup: () => {},
|
||||
shouldRestoreWindowsOnActivate: () => false,
|
||||
restoreWindowsOnActivate: () => {},
|
||||
});
|
||||
|
||||
assert.equal(deps.requestSingleInstanceLock(), true);
|
||||
deps.quitApp();
|
||||
assert.equal(lockRequested, 1);
|
||||
assert.equal(quitCalled, 1);
|
||||
assert.equal(deps.isDarwinPlatform(), true);
|
||||
|
||||
let callbackRan = false;
|
||||
deps.whenReady(async () => {
|
||||
callbackRan = true;
|
||||
});
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal(callbackRan, true);
|
||||
|
||||
deps.onActivate(() => {});
|
||||
assert.equal(typeof events["activate"], "function");
|
||||
});
|
||||
57
src/core/services/app-lifecycle-deps-runtime-service.ts
Normal file
57
src/core/services/app-lifecycle-deps-runtime-service.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { CliArgs, CliCommandSource } from "../../cli/args";
|
||||
import { AppLifecycleServiceDeps } from "./app-lifecycle-service";
|
||||
|
||||
interface AppLike {
|
||||
requestSingleInstanceLock: () => boolean;
|
||||
quit: () => void;
|
||||
on: (...args: any[]) => unknown;
|
||||
whenReady: () => Promise<void>;
|
||||
}
|
||||
|
||||
export interface AppLifecycleDepsRuntimeOptions {
|
||||
app: AppLike;
|
||||
platform: NodeJS.Platform;
|
||||
shouldStartApp: (args: CliArgs) => boolean;
|
||||
parseArgs: (argv: string[]) => CliArgs;
|
||||
handleCliCommand: (args: CliArgs, source: CliCommandSource) => void;
|
||||
printHelp: () => void;
|
||||
logNoRunningInstance: () => void;
|
||||
onReady: () => Promise<void>;
|
||||
onWillQuitCleanup: () => void;
|
||||
shouldRestoreWindowsOnActivate: () => boolean;
|
||||
restoreWindowsOnActivate: () => void;
|
||||
}
|
||||
|
||||
export function createAppLifecycleDepsRuntimeService(
|
||||
options: AppLifecycleDepsRuntimeOptions,
|
||||
): AppLifecycleServiceDeps {
|
||||
return {
|
||||
shouldStartApp: options.shouldStartApp,
|
||||
parseArgs: options.parseArgs,
|
||||
requestSingleInstanceLock: () => options.app.requestSingleInstanceLock(),
|
||||
quitApp: () => options.app.quit(),
|
||||
onSecondInstance: (handler) => {
|
||||
options.app.on("second-instance", handler as (...args: unknown[]) => void);
|
||||
},
|
||||
handleCliCommand: options.handleCliCommand,
|
||||
printHelp: options.printHelp,
|
||||
logNoRunningInstance: options.logNoRunningInstance,
|
||||
whenReady: (handler) => {
|
||||
options.app.whenReady().then(handler);
|
||||
},
|
||||
onWindowAllClosed: (handler) => {
|
||||
options.app.on("window-all-closed", handler as (...args: unknown[]) => void);
|
||||
},
|
||||
onWillQuit: (handler) => {
|
||||
options.app.on("will-quit", handler as (...args: unknown[]) => void);
|
||||
},
|
||||
onActivate: (handler) => {
|
||||
options.app.on("activate", handler as (...args: unknown[]) => void);
|
||||
},
|
||||
isDarwinPlatform: () => options.platform === "darwin",
|
||||
onReady: options.onReady,
|
||||
onWillQuitCleanup: options.onWillQuitCleanup,
|
||||
shouldRestoreWindowsOnActivate: options.shouldRestoreWindowsOnActivate,
|
||||
restoreWindowsOnActivate: options.restoreWindowsOnActivate,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user