mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract startup lifecycle hooks orchestration
This commit is contained in:
@@ -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/cli-command-deps-runtime-service.test.js dist/core/services/ipc-deps-runtime-service.test.js dist/core/services/anki-jimaku-ipc-deps-runtime-service.test.js dist/core/services/field-grouping-overlay-runtime-service.test.js dist/core/services/subsync-deps-runtime-service.test.js dist/core/services/numeric-shortcut-runtime-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/overlay-visibility-facade-deps-runtime-service.test.js dist/core/services/mpv-command-ipc-deps-runtime-service.test.js dist/core/services/runtime-options-ipc-deps-runtime-service.test.js dist/core/services/tokenizer-deps-runtime-service.test.js dist/core/services/overlay-runtime-deps-service.test.js dist/core/services/startup-lifecycle-runtime-deps-service.test.js dist/core/services/overlay-shortcut-runtime-deps-service.test.js dist/core/services/mining-runtime-deps-service.test.js dist/core/services/shortcut-ui-runtime-deps-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 dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js dist/core/services/runtime-options-manager-runtime-service.test.js dist/core/services/config-warning-runtime-service.test.js dist/core/services/app-logging-runtime-service.test.js dist/core/services/startup-resource-runtime-service.test.js dist/core/services/config-generation-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/cli-command-deps-runtime-service.test.js dist/core/services/ipc-deps-runtime-service.test.js dist/core/services/anki-jimaku-ipc-deps-runtime-service.test.js dist/core/services/field-grouping-overlay-runtime-service.test.js dist/core/services/subsync-deps-runtime-service.test.js dist/core/services/numeric-shortcut-runtime-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/overlay-visibility-facade-deps-runtime-service.test.js dist/core/services/mpv-command-ipc-deps-runtime-service.test.js dist/core/services/runtime-options-ipc-deps-runtime-service.test.js dist/core/services/tokenizer-deps-runtime-service.test.js dist/core/services/overlay-runtime-deps-service.test.js dist/core/services/startup-lifecycle-runtime-deps-service.test.js dist/core/services/startup-lifecycle-hooks-runtime-service.test.js dist/core/services/overlay-shortcut-runtime-deps-service.test.js dist/core/services/mining-runtime-deps-service.test.js dist/core/services/shortcut-ui-runtime-deps-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 dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js dist/core/services/runtime-options-manager-runtime-service.test.js dist/core/services/config-warning-runtime-service.test.js dist/core/services/app-logging-runtime-service.test.js dist/core/services/startup-resource-runtime-service.test.js dist/core/services/config-generation-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",
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { createStartupLifecycleHooksRuntimeService } from "./startup-lifecycle-hooks-runtime-service";
|
||||||
|
|
||||||
|
test("createStartupLifecycleHooksRuntimeService wires app-ready and app-shutdown handlers", async () => {
|
||||||
|
const calls: string[] = [];
|
||||||
|
const hooks = createStartupLifecycleHooksRuntimeService({
|
||||||
|
appReadyDeps: {
|
||||||
|
loadSubtitlePosition: () => calls.push("loadSubtitlePosition"),
|
||||||
|
resolveKeybindings: () => calls.push("resolveKeybindings"),
|
||||||
|
createMpvClient: () => calls.push("createMpvClient"),
|
||||||
|
reloadConfig: () => calls.push("reloadConfig"),
|
||||||
|
getResolvedConfig: () => ({
|
||||||
|
secondarySub: { defaultMode: "hover" },
|
||||||
|
websocket: { enabled: "auto", port: 1234 },
|
||||||
|
}),
|
||||||
|
getConfigWarnings: () => [],
|
||||||
|
logConfigWarning: () => calls.push("logConfigWarning"),
|
||||||
|
initRuntimeOptionsManager: () => calls.push("initRuntimeOptionsManager"),
|
||||||
|
setSecondarySubMode: () => calls.push("setSecondarySubMode"),
|
||||||
|
defaultSecondarySubMode: "hover",
|
||||||
|
defaultWebsocketPort: 8765,
|
||||||
|
hasMpvWebsocketPlugin: () => true,
|
||||||
|
startSubtitleWebsocket: () => calls.push("startSubtitleWebsocket"),
|
||||||
|
log: () => calls.push("log"),
|
||||||
|
createMecabTokenizerAndCheck: async () => {
|
||||||
|
calls.push("createMecabTokenizerAndCheck");
|
||||||
|
},
|
||||||
|
createSubtitleTimingTracker: () => calls.push("createSubtitleTimingTracker"),
|
||||||
|
loadYomitanExtension: async () => {
|
||||||
|
calls.push("loadYomitanExtension");
|
||||||
|
},
|
||||||
|
texthookerOnlyMode: false,
|
||||||
|
shouldAutoInitializeOverlayRuntimeFromConfig: () => false,
|
||||||
|
initializeOverlayRuntime: () => calls.push("initializeOverlayRuntime"),
|
||||||
|
handleInitialArgs: () => calls.push("handleInitialArgs"),
|
||||||
|
},
|
||||||
|
appShutdownDeps: {
|
||||||
|
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"),
|
||||||
|
},
|
||||||
|
shouldRestoreWindowsOnActivate: () => true,
|
||||||
|
restoreWindowsOnActivate: () => calls.push("restoreWindowsOnActivate"),
|
||||||
|
});
|
||||||
|
|
||||||
|
await hooks.onReady();
|
||||||
|
hooks.onWillQuitCleanup();
|
||||||
|
assert.equal(hooks.shouldRestoreWindowsOnActivate(), true);
|
||||||
|
hooks.restoreWindowsOnActivate();
|
||||||
|
|
||||||
|
assert.ok(calls.includes("loadSubtitlePosition"));
|
||||||
|
assert.ok(calls.includes("handleInitialArgs"));
|
||||||
|
assert.ok(calls.includes("destroyAnkiIntegration"));
|
||||||
|
assert.ok(calls.includes("restoreWindowsOnActivate"));
|
||||||
|
});
|
||||||
44
src/core/services/startup-lifecycle-hooks-runtime-service.ts
Normal file
44
src/core/services/startup-lifecycle-hooks-runtime-service.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { AppLifecycleDepsRuntimeOptions } from "./app-lifecycle-deps-runtime-service";
|
||||||
|
import {
|
||||||
|
AppReadyRuntimeDeps,
|
||||||
|
runAppReadyRuntimeService,
|
||||||
|
} from "./app-ready-runtime-service";
|
||||||
|
import {
|
||||||
|
AppShutdownRuntimeDeps,
|
||||||
|
runAppShutdownRuntimeService,
|
||||||
|
} from "./app-shutdown-runtime-service";
|
||||||
|
import {
|
||||||
|
createStartupAppReadyDepsRuntimeService,
|
||||||
|
createStartupAppShutdownDepsRuntimeService,
|
||||||
|
} from "./startup-lifecycle-runtime-deps-service";
|
||||||
|
|
||||||
|
type StartupLifecycleHookDeps = Pick<
|
||||||
|
AppLifecycleDepsRuntimeOptions,
|
||||||
|
"onReady" | "onWillQuitCleanup" | "shouldRestoreWindowsOnActivate" | "restoreWindowsOnActivate"
|
||||||
|
>;
|
||||||
|
|
||||||
|
export interface StartupLifecycleHooksRuntimeOptions {
|
||||||
|
appReadyDeps: AppReadyRuntimeDeps;
|
||||||
|
appShutdownDeps: AppShutdownRuntimeDeps;
|
||||||
|
shouldRestoreWindowsOnActivate: () => boolean;
|
||||||
|
restoreWindowsOnActivate: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createStartupLifecycleHooksRuntimeService(
|
||||||
|
options: StartupLifecycleHooksRuntimeOptions,
|
||||||
|
): StartupLifecycleHookDeps {
|
||||||
|
return {
|
||||||
|
onReady: async () => {
|
||||||
|
await runAppReadyRuntimeService(
|
||||||
|
createStartupAppReadyDepsRuntimeService(options.appReadyDeps),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onWillQuitCleanup: () => {
|
||||||
|
runAppShutdownRuntimeService(
|
||||||
|
createStartupAppShutdownDepsRuntimeService(options.appShutdownDeps),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
shouldRestoreWindowsOnActivate: options.shouldRestoreWindowsOnActivate,
|
||||||
|
restoreWindowsOnActivate: options.restoreWindowsOnActivate,
|
||||||
|
};
|
||||||
|
}
|
||||||
21
src/main.ts
21
src/main.ts
@@ -186,8 +186,6 @@ import {
|
|||||||
getOverlayWindowsRuntimeService,
|
getOverlayWindowsRuntimeService,
|
||||||
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 { runAppShutdownRuntimeService } from "./core/services/app-shutdown-runtime-service";
|
|
||||||
import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service";
|
import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service";
|
||||||
import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service";
|
import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service";
|
||||||
import { createCliCommandDepsRuntimeService } from "./core/services/cli-command-deps-runtime-service";
|
import { createCliCommandDepsRuntimeService } from "./core/services/cli-command-deps-runtime-service";
|
||||||
@@ -225,10 +223,7 @@ import {
|
|||||||
createYomitanSettingsWindowDepsRuntimeService,
|
createYomitanSettingsWindowDepsRuntimeService,
|
||||||
runOverlayShortcutLocalFallbackRuntimeService,
|
runOverlayShortcutLocalFallbackRuntimeService,
|
||||||
} from "./core/services/shortcut-ui-runtime-deps-service";
|
} from "./core/services/shortcut-ui-runtime-deps-service";
|
||||||
import {
|
import { createStartupLifecycleHooksRuntimeService } from "./core/services/startup-lifecycle-hooks-runtime-service";
|
||||||
createStartupAppReadyDepsRuntimeService,
|
|
||||||
createStartupAppShutdownDepsRuntimeService,
|
|
||||||
} from "./core/services/startup-lifecycle-runtime-deps-service";
|
|
||||||
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
|
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
|
||||||
import { createAppLoggingRuntimeService } from "./core/services/app-logging-runtime-service";
|
import { createAppLoggingRuntimeService } from "./core/services/app-logging-runtime-service";
|
||||||
import {
|
import {
|
||||||
@@ -525,9 +520,8 @@ const startupState = runStartupBootstrapRuntimeService({
|
|||||||
handleCliCommand: (nextArgs, source) => handleCliCommand(nextArgs, source),
|
handleCliCommand: (nextArgs, source) => handleCliCommand(nextArgs, source),
|
||||||
printHelp: () => printHelp(DEFAULT_TEXTHOOKER_PORT),
|
printHelp: () => printHelp(DEFAULT_TEXTHOOKER_PORT),
|
||||||
logNoRunningInstance: () => appLogger.logNoRunningInstance(),
|
logNoRunningInstance: () => appLogger.logNoRunningInstance(),
|
||||||
onReady: async () => {
|
...createStartupLifecycleHooksRuntimeService({
|
||||||
await runAppReadyRuntimeService(
|
appReadyDeps: {
|
||||||
createStartupAppReadyDepsRuntimeService({
|
|
||||||
loadSubtitlePosition: () => loadSubtitlePosition(),
|
loadSubtitlePosition: () => loadSubtitlePosition(),
|
||||||
resolveKeybindings: () => {
|
resolveKeybindings: () => {
|
||||||
keybindings = resolveKeybindings(getResolvedConfig(), DEFAULT_KEYBINDINGS);
|
keybindings = resolveKeybindings(getResolvedConfig(), DEFAULT_KEYBINDINGS);
|
||||||
@@ -630,12 +624,8 @@ const startupState = runStartupBootstrapRuntimeService({
|
|||||||
shouldAutoInitializeOverlayRuntimeFromConfig(),
|
shouldAutoInitializeOverlayRuntimeFromConfig(),
|
||||||
initializeOverlayRuntime: () => initializeOverlayRuntime(),
|
initializeOverlayRuntime: () => initializeOverlayRuntime(),
|
||||||
handleInitialArgs: () => handleInitialArgs(),
|
handleInitialArgs: () => handleInitialArgs(),
|
||||||
}),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
onWillQuitCleanup: () => {
|
appShutdownDeps: {
|
||||||
runAppShutdownRuntimeService(
|
|
||||||
createStartupAppShutdownDepsRuntimeService({
|
|
||||||
unregisterAllGlobalShortcuts: () => {
|
unregisterAllGlobalShortcuts: () => {
|
||||||
globalShortcut.unregisterAll();
|
globalShortcut.unregisterAll();
|
||||||
},
|
},
|
||||||
@@ -680,8 +670,6 @@ const startupState = runStartupBootstrapRuntimeService({
|
|||||||
ankiIntegration.destroy();
|
ankiIntegration.destroy();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
shouldRestoreWindowsOnActivate: () =>
|
shouldRestoreWindowsOnActivate: () =>
|
||||||
overlayRuntimeInitialized && BrowserWindow.getAllWindows().length === 0,
|
overlayRuntimeInitialized && BrowserWindow.getAllWindows().length === 0,
|
||||||
@@ -691,6 +679,7 @@ const startupState = runStartupBootstrapRuntimeService({
|
|||||||
updateVisibleOverlayVisibility();
|
updateVisibleOverlayVisibility();
|
||||||
updateInvisibleOverlayVisibility();
|
updateInvisibleOverlayVisibility();
|
||||||
},
|
},
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user