refactor(main): move mpv IPC command composition into helper module

This commit is contained in:
2026-02-14 13:46:17 -08:00
parent 65d9f5d54d
commit 84c2bbcc0d
2 changed files with 62 additions and 31 deletions

View File

@@ -46,7 +46,6 @@ import { BaseWindowTracker } from "./window-trackers";
import type { import type {
JimakuApiResponse, JimakuApiResponse,
JimakuLanguagePreference, JimakuLanguagePreference,
RuntimeOptionId,
SubtitleData, SubtitleData,
SubtitlePosition, SubtitlePosition,
Keybinding, Keybinding,
@@ -112,7 +111,6 @@ import {
getJimakuMaxEntryResultsService, getJimakuMaxEntryResultsService,
handleCliCommandService, handleCliCommandService,
handleMineSentenceDigitService, handleMineSentenceDigitService,
handleMpvCommandFromIpcService,
handleMultiCopyDigitService, handleMultiCopyDigitService,
hasMpvWebsocketPlugin, hasMpvWebsocketPlugin,
initializeOverlayRuntimeService, initializeOverlayRuntimeService,
@@ -162,28 +160,25 @@ import {
} from "./core/services/startup-service"; } from "./core/services/startup-service";
import type { AppReadyRuntimeDeps } from "./core/services/startup-service"; import type { AppReadyRuntimeDeps } from "./core/services/startup-service";
import type { SubsyncRuntimeDeps } from "./core/services/subsync-runner-service"; import type { SubsyncRuntimeDeps } from "./core/services/subsync-runner-service";
import { import { applyRuntimeOptionResultRuntimeService } from "./core/services/runtime-options-ipc-service";
applyRuntimeOptionResultRuntimeService,
} from "./core/services/runtime-options-ipc-service";
import { import {
createRuntimeOptionsIpcDeps, createRuntimeOptionsIpcDeps,
createAnkiJimakuIpcRuntimeServiceDeps, createAnkiJimakuIpcRuntimeServiceDeps,
createCliCommandRuntimeServiceDeps, createCliCommandRuntimeServiceDeps,
createMainIpcRuntimeServiceDeps, createMainIpcRuntimeServiceDeps,
createMpvCommandRuntimeServiceDeps,
createSubsyncRuntimeDeps, createSubsyncRuntimeDeps,
} from "./main/dependencies"; } from "./main/dependencies";
import { import {
createAppLifecycleRuntimeDeps as createAppLifecycleRuntimeDepsBuilder, createAppLifecycleRuntimeDeps as createAppLifecycleRuntimeDepsBuilder,
createAppReadyRuntimeDeps as createAppReadyRuntimeDepsBuilder, createAppReadyRuntimeDeps as createAppReadyRuntimeDepsBuilder,
} from "./main/app-lifecycle"; } from "./main/app-lifecycle";
import { handleMpvCommandFromIpcRuntime } from "./main/ipc-mpv-command";
import { createStartupBootstrapRuntimeDeps } from "./main/startup"; import { createStartupBootstrapRuntimeDeps } from "./main/startup";
import { import {
ConfigService, ConfigService,
DEFAULT_CONFIG, DEFAULT_CONFIG,
DEFAULT_KEYBINDINGS, DEFAULT_KEYBINDINGS,
generateConfigTemplate, generateConfigTemplate,
SPECIAL_COMMANDS,
} from "./config"; } from "./config";
import type { import type {
AppLifecycleDepsRuntimeOptions, AppLifecycleDepsRuntimeOptions,
@@ -1504,30 +1499,26 @@ function handleOverlayModalClosed(modal: OverlayHostedModal): void {
} }
function handleMpvCommandFromIpc(command: (string | number)[]): void { function handleMpvCommandFromIpc(command: (string | number)[]): void {
handleMpvCommandFromIpcService( handleMpvCommandFromIpcRuntime(command, {
command, triggerSubsyncFromConfig: () => triggerSubsyncFromConfig(),
createMpvCommandRuntimeServiceDeps({ openRuntimeOptionsPalette: () => openRuntimeOptionsPalette(),
specialCommands: SPECIAL_COMMANDS, cycleRuntimeOption: (id, direction) => {
triggerSubsyncFromConfig: () => triggerSubsyncFromConfig(), if (!appState.runtimeOptionsManager) {
openRuntimeOptionsPalette: () => openRuntimeOptionsPalette(), return { ok: false, error: "Runtime options manager unavailable" };
runtimeOptionsCycle: (id: RuntimeOptionId, direction: 1 | -1) => { }
if (!appState.runtimeOptionsManager) { return applyRuntimeOptionResultRuntimeService(
return { ok: false, error: "Runtime options manager unavailable" }; appState.runtimeOptionsManager.cycleOption(id, direction),
} (text) => showMpvOsd(text),
return applyRuntimeOptionResultRuntimeService( );
appState.runtimeOptionsManager.cycleOption(id, direction), },
(text) => showMpvOsd(text), showMpvOsd: (text: string) => showMpvOsd(text),
); replayCurrentSubtitle: () => replayCurrentSubtitleRuntimeService(appState.mpvClient),
}, playNextSubtitle: () => playNextSubtitleRuntimeService(appState.mpvClient),
showMpvOsd: (text: string) => showMpvOsd(text), sendMpvCommand: (rawCommand: (string | number)[]) =>
mpvReplaySubtitle: () => replayCurrentSubtitleRuntimeService(appState.mpvClient), sendMpvCommandRuntimeService(appState.mpvClient, rawCommand),
mpvPlayNextSubtitle: () => playNextSubtitleRuntimeService(appState.mpvClient), isMpvConnected: () => Boolean(appState.mpvClient && appState.mpvClient.connected),
mpvSendCommand: (rawCommand: (string | number)[]) => hasRuntimeOptionsManager: () => appState.runtimeOptionsManager !== null,
sendMpvCommandRuntimeService(appState.mpvClient, rawCommand), });
isMpvConnected: () => Boolean(appState.mpvClient && appState.mpvClient.connected),
hasRuntimeOptionsManager: () => appState.runtimeOptionsManager !== null,
}),
);
} }
async function runSubsyncManualFromIpc( async function runSubsyncManualFromIpc(

View File

@@ -0,0 +1,40 @@
import type { RuntimeOptionApplyResult, RuntimeOptionId } from "../types";
import { handleMpvCommandFromIpcService } from "../core/services";
import { createMpvCommandRuntimeServiceDeps } from "./dependencies";
import { SPECIAL_COMMANDS } from "../config";
export interface MpvCommandFromIpcRuntimeDeps {
triggerSubsyncFromConfig: () => void;
openRuntimeOptionsPalette: () => void;
cycleRuntimeOption: (
id: RuntimeOptionId,
direction: 1 | -1,
) => RuntimeOptionApplyResult;
showMpvOsd: (text: string) => void;
replayCurrentSubtitle: () => void;
playNextSubtitle: () => void;
sendMpvCommand: (command: (string | number)[]) => void;
isMpvConnected: () => boolean;
hasRuntimeOptionsManager: () => boolean;
}
export function handleMpvCommandFromIpcRuntime(
command: (string | number)[],
deps: MpvCommandFromIpcRuntimeDeps,
): void {
handleMpvCommandFromIpcService(
command,
createMpvCommandRuntimeServiceDeps({
specialCommands: SPECIAL_COMMANDS,
triggerSubsyncFromConfig: deps.triggerSubsyncFromConfig,
openRuntimeOptionsPalette: deps.openRuntimeOptionsPalette,
runtimeOptionsCycle: deps.cycleRuntimeOption,
showMpvOsd: deps.showMpvOsd,
mpvReplaySubtitle: deps.replayCurrentSubtitle,
mpvPlayNextSubtitle: deps.playNextSubtitle,
mpvSendCommand: deps.sendMpvCommand,
isMpvConnected: deps.isMpvConnected,
hasRuntimeOptionsManager: deps.hasRuntimeOptionsManager,
}),
);
}