mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract startup lifecycle runtime deps
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
createStartupAppReadyDepsRuntimeService,
|
||||
createStartupAppShutdownDepsRuntimeService,
|
||||
} from "./startup-lifecycle-runtime-deps-service";
|
||||
|
||||
test("createStartupAppReadyDepsRuntimeService preserves runtime deps behavior", async () => {
|
||||
const calls: string[] = [];
|
||||
const deps = createStartupAppReadyDepsRuntimeService({
|
||||
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: () => {},
|
||||
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("createMecab");
|
||||
},
|
||||
createSubtitleTimingTracker: () => calls.push("createSubtitleTimingTracker"),
|
||||
loadYomitanExtension: async () => {
|
||||
calls.push("loadYomitan");
|
||||
},
|
||||
texthookerOnlyMode: false,
|
||||
shouldAutoInitializeOverlayRuntimeFromConfig: () => false,
|
||||
initializeOverlayRuntime: () => calls.push("initOverlayRuntime"),
|
||||
handleInitialArgs: () => calls.push("handleInitialArgs"),
|
||||
});
|
||||
|
||||
deps.loadSubtitlePosition();
|
||||
await deps.createMecabTokenizerAndCheck();
|
||||
deps.handleInitialArgs();
|
||||
|
||||
assert.equal(deps.defaultWebsocketPort, 8765);
|
||||
assert.equal(deps.defaultSecondarySubMode, "hover");
|
||||
assert.deepEqual(calls, ["loadSubtitlePosition", "createMecab", "handleInitialArgs"]);
|
||||
});
|
||||
|
||||
test("createStartupAppShutdownDepsRuntimeService preserves shutdown handlers", () => {
|
||||
const calls: string[] = [];
|
||||
const deps = createStartupAppShutdownDepsRuntimeService({
|
||||
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"),
|
||||
});
|
||||
|
||||
deps.stopSubtitleWebsocket();
|
||||
deps.clearReconnectTimer();
|
||||
deps.destroyAnkiIntegration();
|
||||
|
||||
assert.deepEqual(calls, [
|
||||
"stopSubtitleWebsocket",
|
||||
"clearReconnectTimer",
|
||||
"destroyAnkiIntegration",
|
||||
]);
|
||||
});
|
||||
55
src/core/services/startup-lifecycle-runtime-deps-service.ts
Normal file
55
src/core/services/startup-lifecycle-runtime-deps-service.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {
|
||||
AppReadyRuntimeDeps,
|
||||
} from "./app-ready-runtime-service";
|
||||
import {
|
||||
AppShutdownRuntimeDeps,
|
||||
} from "./app-shutdown-runtime-service";
|
||||
|
||||
export type StartupAppReadyDepsRuntimeOptions = AppReadyRuntimeDeps;
|
||||
export type StartupAppShutdownDepsRuntimeOptions = AppShutdownRuntimeDeps;
|
||||
|
||||
export function createStartupAppReadyDepsRuntimeService(
|
||||
options: StartupAppReadyDepsRuntimeOptions,
|
||||
): AppReadyRuntimeDeps {
|
||||
return {
|
||||
loadSubtitlePosition: options.loadSubtitlePosition,
|
||||
resolveKeybindings: options.resolveKeybindings,
|
||||
createMpvClient: options.createMpvClient,
|
||||
reloadConfig: options.reloadConfig,
|
||||
getResolvedConfig: options.getResolvedConfig,
|
||||
getConfigWarnings: options.getConfigWarnings,
|
||||
logConfigWarning: options.logConfigWarning,
|
||||
initRuntimeOptionsManager: options.initRuntimeOptionsManager,
|
||||
setSecondarySubMode: options.setSecondarySubMode,
|
||||
defaultSecondarySubMode: options.defaultSecondarySubMode,
|
||||
defaultWebsocketPort: options.defaultWebsocketPort,
|
||||
hasMpvWebsocketPlugin: options.hasMpvWebsocketPlugin,
|
||||
startSubtitleWebsocket: options.startSubtitleWebsocket,
|
||||
log: options.log,
|
||||
createMecabTokenizerAndCheck: options.createMecabTokenizerAndCheck,
|
||||
createSubtitleTimingTracker: options.createSubtitleTimingTracker,
|
||||
loadYomitanExtension: options.loadYomitanExtension,
|
||||
texthookerOnlyMode: options.texthookerOnlyMode,
|
||||
shouldAutoInitializeOverlayRuntimeFromConfig:
|
||||
options.shouldAutoInitializeOverlayRuntimeFromConfig,
|
||||
initializeOverlayRuntime: options.initializeOverlayRuntime,
|
||||
handleInitialArgs: options.handleInitialArgs,
|
||||
};
|
||||
}
|
||||
|
||||
export function createStartupAppShutdownDepsRuntimeService(
|
||||
options: StartupAppShutdownDepsRuntimeOptions,
|
||||
): AppShutdownRuntimeDeps {
|
||||
return {
|
||||
unregisterAllGlobalShortcuts: options.unregisterAllGlobalShortcuts,
|
||||
stopSubtitleWebsocket: options.stopSubtitleWebsocket,
|
||||
stopTexthookerService: options.stopTexthookerService,
|
||||
destroyYomitanParserWindow: options.destroyYomitanParserWindow,
|
||||
clearYomitanParserPromises: options.clearYomitanParserPromises,
|
||||
stopWindowTracker: options.stopWindowTracker,
|
||||
destroyMpvSocket: options.destroyMpvSocket,
|
||||
clearReconnectTimer: options.clearReconnectTimer,
|
||||
destroySubtitleTimingTracker: options.destroySubtitleTimingTracker,
|
||||
destroyAnkiIntegration: options.destroyAnkiIntegration,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user