refactor: extract app shutdown orchestration service

This commit is contained in:
2026-02-10 00:06:10 -08:00
parent 2878a1f3d1
commit 8b286f15e8
4 changed files with 107 additions and 25 deletions

View File

@@ -16,7 +16,7 @@
"docs:build": "vitepress build docs", "docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort", "docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort",
"test:config": "pnpm run build && node --test dist/config/config.test.js", "test:config": "pnpm run build && node --test dist/config/config.test.js",
"test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js", "test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js",
"test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js", "test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js",
"generate:config-example": "pnpm run build && node dist/generate-config-example.js", "generate:config-example": "pnpm run build && node dist/generate-config-example.js",
"start": "pnpm run build && electron . --start", "start": "pnpm run build && electron . --start",

View File

@@ -0,0 +1,32 @@
import test from "node:test";
import assert from "node:assert/strict";
import { runAppShutdownRuntimeService } from "./app-shutdown-runtime-service";
test("runAppShutdownRuntimeService runs teardown steps in order", () => {
const calls: string[] = [];
runAppShutdownRuntimeService({
unregisterAllGlobalShortcuts: () => calls.push("unregisterAllGlobalShortcuts"),
stopSubtitleWebsocket: () => calls.push("stopSubtitleWebsocket"),
stopTexthookerService: () => calls.push("stopTexthookerService"),
destroyYomitanParserWindow: () => calls.push("destroyYomitanParserWindow"),
clearYomitanParserPromises: () => calls.push("clearYomitanParserPromises"),
stopWindowTracker: () => calls.push("stopWindowTracker"),
destroyMpvSocket: () => calls.push("destroyMpvSocket"),
clearReconnectTimer: () => calls.push("clearReconnectTimer"),
destroySubtitleTimingTracker: () => calls.push("destroySubtitleTimingTracker"),
destroyAnkiIntegration: () => calls.push("destroyAnkiIntegration"),
});
assert.deepEqual(calls, [
"unregisterAllGlobalShortcuts",
"stopSubtitleWebsocket",
"stopTexthookerService",
"destroyYomitanParserWindow",
"clearYomitanParserPromises",
"stopWindowTracker",
"destroyMpvSocket",
"clearReconnectTimer",
"destroySubtitleTimingTracker",
"destroyAnkiIntegration",
]);
});

View File

@@ -0,0 +1,27 @@
export interface AppShutdownRuntimeDeps {
unregisterAllGlobalShortcuts: () => void;
stopSubtitleWebsocket: () => void;
stopTexthookerService: () => void;
destroyYomitanParserWindow: () => void;
clearYomitanParserPromises: () => void;
stopWindowTracker: () => void;
destroyMpvSocket: () => void;
clearReconnectTimer: () => void;
destroySubtitleTimingTracker: () => void;
destroyAnkiIntegration: () => void;
}
export function runAppShutdownRuntimeService(
deps: AppShutdownRuntimeDeps,
): void {
deps.unregisterAllGlobalShortcuts();
deps.stopSubtitleWebsocket();
deps.stopTexthookerService();
deps.destroyYomitanParserWindow();
deps.clearYomitanParserPromises();
deps.stopWindowTracker();
deps.destroyMpvSocket();
deps.clearReconnectTimer();
deps.destroySubtitleTimingTracker();
deps.destroyAnkiIntegration();
}

View File

@@ -202,6 +202,7 @@ import {
setOverlayDebugVisualizationEnabledRuntimeService, setOverlayDebugVisualizationEnabledRuntimeService,
} from "./core/services/overlay-broadcast-runtime-service"; } from "./core/services/overlay-broadcast-runtime-service";
import { runAppReadyRuntimeService } from "./core/services/app-ready-runtime-service"; import { runAppReadyRuntimeService } from "./core/services/app-ready-runtime-service";
import { runAppShutdownRuntimeService } from "./core/services/app-shutdown-runtime-service";
import { import {
runSubsyncManualFromIpcRuntimeService, runSubsyncManualFromIpcRuntimeService,
triggerSubsyncFromConfigRuntimeService, triggerSubsyncFromConfigRuntimeService,
@@ -564,30 +565,52 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
}); });
}, },
onWillQuitCleanup: () => { onWillQuitCleanup: () => {
globalShortcut.unregisterAll(); runAppShutdownRuntimeService({
subtitleWsService.stop(); unregisterAllGlobalShortcuts: () => {
texthookerService.stop(); globalShortcut.unregisterAll();
if (yomitanParserWindow && !yomitanParserWindow.isDestroyed()) { },
yomitanParserWindow.destroy(); stopSubtitleWebsocket: () => {
} subtitleWsService.stop();
yomitanParserWindow = null; },
yomitanParserReadyPromise = null; stopTexthookerService: () => {
yomitanParserInitPromise = null; texthookerService.stop();
if (windowTracker) { },
windowTracker.stop(); destroyYomitanParserWindow: () => {
} if (yomitanParserWindow && !yomitanParserWindow.isDestroyed()) {
if (mpvClient && mpvClient.socket) { yomitanParserWindow.destroy();
mpvClient.socket.destroy(); }
} yomitanParserWindow = null;
if (reconnectTimer) { },
clearTimeout(reconnectTimer); clearYomitanParserPromises: () => {
} yomitanParserReadyPromise = null;
if (subtitleTimingTracker) { yomitanParserInitPromise = null;
subtitleTimingTracker.destroy(); },
} stopWindowTracker: () => {
if (ankiIntegration) { if (windowTracker) {
ankiIntegration.destroy(); windowTracker.stop();
} }
},
destroyMpvSocket: () => {
if (mpvClient && mpvClient.socket) {
mpvClient.socket.destroy();
}
},
clearReconnectTimer: () => {
if (reconnectTimer) {
clearTimeout(reconnectTimer);
}
},
destroySubtitleTimingTracker: () => {
if (subtitleTimingTracker) {
subtitleTimingTracker.destroy();
}
},
destroyAnkiIntegration: () => {
if (ankiIntegration) {
ankiIntegration.destroy();
}
},
});
}, },
shouldRestoreWindowsOnActivate: () => shouldRestoreWindowsOnActivate: () =>
overlayRuntimeInitialized && BrowserWindow.getAllWindows().length === 0, overlayRuntimeInitialized && BrowserWindow.getAllWindows().length === 0,