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,
|
triggerFieldGroupingService,
|
||||||
updateLastCardFromClipboardService,
|
updateLastCardFromClipboardService,
|
||||||
} from "./core/services/mining-runtime-service";
|
} from "./core/services/mining-runtime-service";
|
||||||
|
import { startAppLifecycleService } from "./core/services/app-lifecycle-service";
|
||||||
import { showDesktopNotification } from "./core/utils/notification";
|
import { showDesktopNotification } from "./core/utils/notification";
|
||||||
import { openYomitanSettingsWindow } from "./core/services/yomitan-settings-service";
|
import { openYomitanSettingsWindow } from "./core/services/yomitan-settings-service";
|
||||||
import { tokenizeSubtitleService } from "./core/services/tokenizer-service";
|
import { tokenizeSubtitleService } from "./core/services/tokenizer-service";
|
||||||
@@ -420,26 +421,33 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
|||||||
app.quit();
|
app.quit();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const gotTheLock = app.requestSingleInstanceLock();
|
startAppLifecycleService(initialArgs, {
|
||||||
|
shouldStartApp: (args) => shouldStartApp(args),
|
||||||
if (!gotTheLock) {
|
parseArgs: (argv) => parseArgs(argv),
|
||||||
app.quit();
|
requestSingleInstanceLock: () => app.requestSingleInstanceLock(),
|
||||||
} else {
|
quitApp: () => app.quit(),
|
||||||
app.on("second-instance", (_event, argv) => {
|
onSecondInstance: (handler) => {
|
||||||
handleCliCommand(parseArgs(argv), "second-instance");
|
app.on("second-instance", handler);
|
||||||
});
|
},
|
||||||
if (initialArgs.help && !shouldStartApp(initialArgs)) {
|
handleCliCommand: (args, source) => handleCliCommand(args, source),
|
||||||
printHelp(DEFAULT_TEXTHOOKER_PORT);
|
printHelp: () => printHelp(DEFAULT_TEXTHOOKER_PORT),
|
||||||
app.quit();
|
logNoRunningInstance: () => {
|
||||||
} else if (!shouldStartApp(initialArgs)) {
|
|
||||||
if (initialArgs.stop && !initialArgs.start) {
|
|
||||||
app.quit();
|
|
||||||
} else {
|
|
||||||
console.error("No running instance. Use --start to launch the app.");
|
console.error("No running instance. Use --start to launch the app.");
|
||||||
app.quit();
|
},
|
||||||
}
|
whenReady: (handler) => {
|
||||||
} else {
|
app.whenReady().then(handler);
|
||||||
app.whenReady().then(async () => {
|
},
|
||||||
|
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();
|
loadSubtitlePosition();
|
||||||
keybindings = resolveKeybindings(getResolvedConfig(), DEFAULT_KEYBINDINGS);
|
keybindings = resolveKeybindings(getResolvedConfig(), DEFAULT_KEYBINDINGS);
|
||||||
|
|
||||||
@@ -509,17 +517,9 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
|||||||
"Overlay runtime deferred: waiting for explicit overlay command.",
|
"Overlay runtime deferred: waiting for explicit overlay command.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleInitialArgs();
|
handleInitialArgs();
|
||||||
});
|
},
|
||||||
|
onWillQuitCleanup: () => {
|
||||||
app.on("window-all-closed", () => {
|
|
||||||
if (process.platform !== "darwin") {
|
|
||||||
app.quit();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.on("will-quit", () => {
|
|
||||||
globalShortcut.unregisterAll();
|
globalShortcut.unregisterAll();
|
||||||
subtitleWsService.stop();
|
subtitleWsService.stop();
|
||||||
texthookerService.stop();
|
texthookerService.stop();
|
||||||
@@ -544,21 +544,16 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
|
|||||||
if (ankiIntegration) {
|
if (ankiIntegration) {
|
||||||
ankiIntegration.destroy();
|
ankiIntegration.destroy();
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
shouldRestoreWindowsOnActivate: () =>
|
||||||
app.on("activate", () => {
|
overlayRuntimeInitialized && BrowserWindow.getAllWindows().length === 0,
|
||||||
if (
|
restoreWindowsOnActivate: () => {
|
||||||
overlayRuntimeInitialized &&
|
|
||||||
BrowserWindow.getAllWindows().length === 0
|
|
||||||
) {
|
|
||||||
createMainWindow();
|
createMainWindow();
|
||||||
createInvisibleWindow();
|
createInvisibleWindow();
|
||||||
updateVisibleOverlayVisibility();
|
updateVisibleOverlayVisibility();
|
||||||
updateInvisibleOverlayVisibility();
|
updateInvisibleOverlayVisibility();
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleCliCommand(
|
function handleCliCommand(
|
||||||
|
|||||||
Reference in New Issue
Block a user