From 84c2bbcc0d3d5867ab1d79ab59fafaf990bf79f7 Mon Sep 17 00:00:00 2001 From: sudacode Date: Sat, 14 Feb 2026 13:46:17 -0800 Subject: [PATCH] refactor(main): move mpv IPC command composition into helper module --- src/main.ts | 53 +++++++++++++++---------------------- src/main/ipc-mpv-command.ts | 40 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 src/main/ipc-mpv-command.ts diff --git a/src/main.ts b/src/main.ts index b3899f6..f7d7e23 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,7 +46,6 @@ import { BaseWindowTracker } from "./window-trackers"; import type { JimakuApiResponse, JimakuLanguagePreference, - RuntimeOptionId, SubtitleData, SubtitlePosition, Keybinding, @@ -112,7 +111,6 @@ import { getJimakuMaxEntryResultsService, handleCliCommandService, handleMineSentenceDigitService, - handleMpvCommandFromIpcService, handleMultiCopyDigitService, hasMpvWebsocketPlugin, initializeOverlayRuntimeService, @@ -162,28 +160,25 @@ import { } from "./core/services/startup-service"; import type { AppReadyRuntimeDeps } from "./core/services/startup-service"; import type { SubsyncRuntimeDeps } from "./core/services/subsync-runner-service"; -import { - applyRuntimeOptionResultRuntimeService, -} from "./core/services/runtime-options-ipc-service"; +import { applyRuntimeOptionResultRuntimeService } from "./core/services/runtime-options-ipc-service"; import { createRuntimeOptionsIpcDeps, createAnkiJimakuIpcRuntimeServiceDeps, createCliCommandRuntimeServiceDeps, createMainIpcRuntimeServiceDeps, - createMpvCommandRuntimeServiceDeps, createSubsyncRuntimeDeps, } from "./main/dependencies"; import { createAppLifecycleRuntimeDeps as createAppLifecycleRuntimeDepsBuilder, createAppReadyRuntimeDeps as createAppReadyRuntimeDepsBuilder, } from "./main/app-lifecycle"; +import { handleMpvCommandFromIpcRuntime } from "./main/ipc-mpv-command"; import { createStartupBootstrapRuntimeDeps } from "./main/startup"; import { ConfigService, DEFAULT_CONFIG, DEFAULT_KEYBINDINGS, generateConfigTemplate, - SPECIAL_COMMANDS, } from "./config"; import type { AppLifecycleDepsRuntimeOptions, @@ -1504,30 +1499,26 @@ function handleOverlayModalClosed(modal: OverlayHostedModal): void { } function handleMpvCommandFromIpc(command: (string | number)[]): void { - handleMpvCommandFromIpcService( - command, - createMpvCommandRuntimeServiceDeps({ - specialCommands: SPECIAL_COMMANDS, - triggerSubsyncFromConfig: () => triggerSubsyncFromConfig(), - openRuntimeOptionsPalette: () => openRuntimeOptionsPalette(), - runtimeOptionsCycle: (id: RuntimeOptionId, direction: 1 | -1) => { - if (!appState.runtimeOptionsManager) { - return { ok: false, error: "Runtime options manager unavailable" }; - } - return applyRuntimeOptionResultRuntimeService( - appState.runtimeOptionsManager.cycleOption(id, direction), - (text) => showMpvOsd(text), - ); - }, - showMpvOsd: (text: string) => showMpvOsd(text), - mpvReplaySubtitle: () => replayCurrentSubtitleRuntimeService(appState.mpvClient), - mpvPlayNextSubtitle: () => playNextSubtitleRuntimeService(appState.mpvClient), - mpvSendCommand: (rawCommand: (string | number)[]) => - sendMpvCommandRuntimeService(appState.mpvClient, rawCommand), - isMpvConnected: () => Boolean(appState.mpvClient && appState.mpvClient.connected), - hasRuntimeOptionsManager: () => appState.runtimeOptionsManager !== null, - }), - ); + handleMpvCommandFromIpcRuntime(command, { + triggerSubsyncFromConfig: () => triggerSubsyncFromConfig(), + openRuntimeOptionsPalette: () => openRuntimeOptionsPalette(), + cycleRuntimeOption: (id, direction) => { + if (!appState.runtimeOptionsManager) { + return { ok: false, error: "Runtime options manager unavailable" }; + } + return applyRuntimeOptionResultRuntimeService( + appState.runtimeOptionsManager.cycleOption(id, direction), + (text) => showMpvOsd(text), + ); + }, + showMpvOsd: (text: string) => showMpvOsd(text), + replayCurrentSubtitle: () => replayCurrentSubtitleRuntimeService(appState.mpvClient), + playNextSubtitle: () => playNextSubtitleRuntimeService(appState.mpvClient), + sendMpvCommand: (rawCommand: (string | number)[]) => + sendMpvCommandRuntimeService(appState.mpvClient, rawCommand), + isMpvConnected: () => Boolean(appState.mpvClient && appState.mpvClient.connected), + hasRuntimeOptionsManager: () => appState.runtimeOptionsManager !== null, + }); } async function runSubsyncManualFromIpc( diff --git a/src/main/ipc-mpv-command.ts b/src/main/ipc-mpv-command.ts new file mode 100644 index 0000000..716296c --- /dev/null +++ b/src/main/ipc-mpv-command.ts @@ -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, + }), + ); +}