mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract app lifecycle orchestration service
This commit is contained in:
72
src/core/services/app-lifecycle-service.ts
Normal file
72
src/core/services/app-lifecycle-service.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { CliArgs, CliCommandSource } from "../../cli/args";
|
||||
|
||||
export interface AppLifecycleServiceDeps {
|
||||
shouldStartApp: (args: CliArgs) => boolean;
|
||||
parseArgs: (argv: string[]) => CliArgs;
|
||||
requestSingleInstanceLock: () => boolean;
|
||||
quitApp: () => void;
|
||||
onSecondInstance: (handler: (_event: unknown, argv: string[]) => void) => void;
|
||||
handleCliCommand: (args: CliArgs, source: CliCommandSource) => void;
|
||||
printHelp: () => void;
|
||||
logNoRunningInstance: () => void;
|
||||
whenReady: (handler: () => Promise<void>) => void;
|
||||
onWindowAllClosed: (handler: () => void) => void;
|
||||
onWillQuit: (handler: () => void) => void;
|
||||
onActivate: (handler: () => void) => void;
|
||||
isDarwinPlatform: () => boolean;
|
||||
onReady: () => Promise<void>;
|
||||
onWillQuitCleanup: () => void;
|
||||
shouldRestoreWindowsOnActivate: () => boolean;
|
||||
restoreWindowsOnActivate: () => void;
|
||||
}
|
||||
|
||||
export function startAppLifecycleService(
|
||||
initialArgs: CliArgs,
|
||||
deps: AppLifecycleServiceDeps,
|
||||
): void {
|
||||
const gotTheLock = deps.requestSingleInstanceLock();
|
||||
if (!gotTheLock) {
|
||||
deps.quitApp();
|
||||
return;
|
||||
}
|
||||
|
||||
deps.onSecondInstance((_event, argv) => {
|
||||
deps.handleCliCommand(deps.parseArgs(argv), "second-instance");
|
||||
});
|
||||
|
||||
if (initialArgs.help && !deps.shouldStartApp(initialArgs)) {
|
||||
deps.printHelp();
|
||||
deps.quitApp();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!deps.shouldStartApp(initialArgs)) {
|
||||
if (initialArgs.stop && !initialArgs.start) {
|
||||
deps.quitApp();
|
||||
} else {
|
||||
deps.logNoRunningInstance();
|
||||
deps.quitApp();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
deps.whenReady(async () => {
|
||||
await deps.onReady();
|
||||
});
|
||||
|
||||
deps.onWindowAllClosed(() => {
|
||||
if (!deps.isDarwinPlatform()) {
|
||||
deps.quitApp();
|
||||
}
|
||||
});
|
||||
|
||||
deps.onWillQuit(() => {
|
||||
deps.onWillQuitCleanup();
|
||||
});
|
||||
|
||||
deps.onActivate(() => {
|
||||
if (deps.shouldRestoreWindowsOnActivate()) {
|
||||
deps.restoreWindowsOnActivate();
|
||||
}
|
||||
});
|
||||
}
|
||||
73
src/main.ts
73
src/main.ts
@@ -127,6 +127,7 @@ import {
|
||||
triggerFieldGroupingService,
|
||||
updateLastCardFromClipboardService,
|
||||
} from "./core/services/mining-runtime-service";
|
||||
import { startAppLifecycleService } from "./core/services/app-lifecycle-service";
|
||||
import { showDesktopNotification } from "./core/utils/notification";
|
||||
import { openYomitanSettingsWindow } from "./core/services/yomitan-settings-service";
|
||||
import { tokenizeSubtitleService } from "./core/services/tokenizer-service";
|
||||
@@ -420,26 +421,33 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
||||
app.quit();
|
||||
});
|
||||
} else {
|
||||
const gotTheLock = app.requestSingleInstanceLock();
|
||||
|
||||
if (!gotTheLock) {
|
||||
app.quit();
|
||||
} else {
|
||||
app.on("second-instance", (_event, argv) => {
|
||||
handleCliCommand(parseArgs(argv), "second-instance");
|
||||
});
|
||||
if (initialArgs.help && !shouldStartApp(initialArgs)) {
|
||||
printHelp(DEFAULT_TEXTHOOKER_PORT);
|
||||
app.quit();
|
||||
} else if (!shouldStartApp(initialArgs)) {
|
||||
if (initialArgs.stop && !initialArgs.start) {
|
||||
app.quit();
|
||||
} else {
|
||||
startAppLifecycleService(initialArgs, {
|
||||
shouldStartApp: (args) => shouldStartApp(args),
|
||||
parseArgs: (argv) => parseArgs(argv),
|
||||
requestSingleInstanceLock: () => app.requestSingleInstanceLock(),
|
||||
quitApp: () => app.quit(),
|
||||
onSecondInstance: (handler) => {
|
||||
app.on("second-instance", handler);
|
||||
},
|
||||
handleCliCommand: (args, source) => handleCliCommand(args, source),
|
||||
printHelp: () => printHelp(DEFAULT_TEXTHOOKER_PORT),
|
||||
logNoRunningInstance: () => {
|
||||
console.error("No running instance. Use --start to launch the app.");
|
||||
app.quit();
|
||||
}
|
||||
} else {
|
||||
app.whenReady().then(async () => {
|
||||
},
|
||||
whenReady: (handler) => {
|
||||
app.whenReady().then(handler);
|
||||
},
|
||||
onWindowAllClosed: (handler) => {
|
||||
app.on("window-all-closed", handler);
|
||||
},
|
||||
onWillQuit: (handler) => {
|
||||
app.on("will-quit", handler);
|
||||
},
|
||||
onActivate: (handler) => {
|
||||
app.on("activate", handler);
|
||||
},
|
||||
isDarwinPlatform: () => process.platform === "darwin",
|
||||
onReady: async () => {
|
||||
loadSubtitlePosition();
|
||||
keybindings = resolveKeybindings(getResolvedConfig(), DEFAULT_KEYBINDINGS);
|
||||
|
||||
@@ -509,17 +517,9 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
||||
"Overlay runtime deferred: waiting for explicit overlay command.",
|
||||
);
|
||||
}
|
||||
|
||||
handleInitialArgs();
|
||||
});
|
||||
|
||||
app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
|
||||
app.on("will-quit", () => {
|
||||
},
|
||||
onWillQuitCleanup: () => {
|
||||
globalShortcut.unregisterAll();
|
||||
subtitleWsService.stop();
|
||||
texthookerService.stop();
|
||||
@@ -544,21 +544,16 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
||||
if (ankiIntegration) {
|
||||
ankiIntegration.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
app.on("activate", () => {
|
||||
if (
|
||||
overlayRuntimeInitialized &&
|
||||
BrowserWindow.getAllWindows().length === 0
|
||||
) {
|
||||
},
|
||||
shouldRestoreWindowsOnActivate: () =>
|
||||
overlayRuntimeInitialized && BrowserWindow.getAllWindows().length === 0,
|
||||
restoreWindowsOnActivate: () => {
|
||||
createMainWindow();
|
||||
createInvisibleWindow();
|
||||
updateVisibleOverlayVisibility();
|
||||
updateInvisibleOverlayVisibility();
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleCliCommand(
|
||||
|
||||
Reference in New Issue
Block a user