refactor: extract mpv osd and secondary-sub runtime wiring

This commit is contained in:
2026-02-20 03:33:06 -08:00
parent 2d89dd43f2
commit eef8a7eb41
7 changed files with 136 additions and 41 deletions

View File

@@ -288,12 +288,8 @@ import {
} from './main/runtime/overlay-window-layout-main-deps';
import { buildTrayMenuTemplateRuntime, resolveTrayIconPathRuntime } from './main/runtime/tray-runtime';
import { createGlobalShortcutsRuntimeHandlers } from './main/runtime/global-shortcuts-runtime-handlers';
import { createAppendToMpvLogHandler, createShowMpvOsdHandler } from './main/runtime/mpv-osd-log';
import {
createBuildAppendToMpvLogMainDepsHandler,
createBuildShowMpvOsdMainDepsHandler,
} from './main/runtime/mpv-osd-log-main-deps';
import { createBuildCycleSecondarySubModeMainDepsHandler } from './main/runtime/secondary-sub-mode-main-deps';
import { createMpvOsdRuntimeHandlers } from './main/runtime/mpv-osd-runtime-handlers';
import { createCycleSecondarySubModeRuntimeHandler } from './main/runtime/secondary-sub-mode-runtime-handler';
import {
createCancelNumericShortcutSessionHandler,
createStartNumericShortcutSessionHandler,
@@ -2522,8 +2518,25 @@ const {
}),
});
const buildCycleSecondarySubModeMainDepsHandler = createBuildCycleSecondarySubModeMainDepsHandler(
{
const { appendToMpvLog, showMpvOsd } = createMpvOsdRuntimeHandlers({
appendToMpvLogMainDeps: {
logPath: DEFAULT_MPV_LOG_PATH,
dirname: (targetPath) => path.dirname(targetPath),
mkdirSync: (targetPath, options) => fs.mkdirSync(targetPath, options),
appendFileSync: (targetPath, data, options) => fs.appendFileSync(targetPath, data, options),
now: () => new Date(),
},
buildShowMpvOsdMainDeps: (appendToMpvLogHandler) => ({
appendToMpvLog: (message) => appendToMpvLogHandler(message),
showMpvOsdRuntime: (mpvClient, text, fallbackLog) =>
showMpvOsdRuntime(mpvClient as never, text, fallbackLog),
getMpvClient: () => appState.mpvClient,
logInfo: (line) => logger.info(line),
}),
});
const cycleSecondarySubMode = createCycleSecondarySubModeRuntimeHandler({
cycleSecondarySubModeMainDeps: {
getSecondarySubMode: () => appState.secondarySubMode,
setSecondarySubMode: (mode: SecondarySubMode) => {
appState.secondarySubMode = mode;
@@ -2537,39 +2550,8 @@ const buildCycleSecondarySubModeMainDepsHandler = createBuildCycleSecondarySubMo
},
showMpvOsd: (text: string) => showMpvOsd(text),
},
);
function cycleSecondarySubMode(): void {
cycleSecondarySubModeCore(buildCycleSecondarySubModeMainDepsHandler());
}
const buildAppendToMpvLogMainDepsHandler = createBuildAppendToMpvLogMainDepsHandler({
logPath: DEFAULT_MPV_LOG_PATH,
dirname: (targetPath) => path.dirname(targetPath),
mkdirSync: (targetPath, options) => fs.mkdirSync(targetPath, options),
appendFileSync: (targetPath, data, options) => fs.appendFileSync(targetPath, data, options),
now: () => new Date(),
cycleSecondarySubMode: (deps) => cycleSecondarySubModeCore(deps),
});
const appendToMpvLogMainDeps = buildAppendToMpvLogMainDepsHandler();
const appendToMpvLogHandler = createAppendToMpvLogHandler(appendToMpvLogMainDeps);
const buildShowMpvOsdMainDepsHandler = createBuildShowMpvOsdMainDepsHandler({
appendToMpvLog: (message) => appendToMpvLog(message),
showMpvOsdRuntime: (mpvClient, text, fallbackLog) =>
showMpvOsdRuntime(mpvClient as never, text, fallbackLog),
getMpvClient: () => appState.mpvClient,
logInfo: (line) => logger.info(line),
});
const showMpvOsdMainDeps = buildShowMpvOsdMainDepsHandler();
const showMpvOsdHandler = createShowMpvOsdHandler(showMpvOsdMainDeps);
function showMpvOsd(text: string): void {
showMpvOsdHandler(text);
}
function appendToMpvLog(message: string): void {
appendToMpvLogHandler(message);
}
const buildNumericShortcutRuntimeMainDepsHandler = createBuildNumericShortcutRuntimeMainDepsHandler({
globalShortcut,

View File

@@ -0,0 +1,33 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createMpvOsdRuntimeHandlers } from './mpv-osd-runtime-handlers';
test('mpv osd runtime handlers compose append and osd logging flow', () => {
const calls: string[] = [];
const runtime = createMpvOsdRuntimeHandlers({
appendToMpvLogMainDeps: {
logPath: '/tmp/subminer/mpv.log',
dirname: () => '/tmp/subminer',
mkdirSync: () => {},
appendFileSync: (_targetPath, data) => calls.push(`append:${data.trimEnd()}`),
now: () => new Date('2026-02-20T00:00:00.000Z'),
},
buildShowMpvOsdMainDeps: (appendToMpvLog) => ({
appendToMpvLog,
showMpvOsdRuntime: (_client, text, fallbackLog) => {
calls.push(`show:${text}`);
fallbackLog('fallback');
},
getMpvClient: () => null,
logInfo: (line) => calls.push(`info:${line}`),
}),
});
runtime.showMpvOsd('hello');
assert.deepEqual(calls, [
'append:[2026-02-20T00:00:00.000Z] [OSD] hello',
'show:hello',
'info:fallback',
]);
});

View File

@@ -0,0 +1,29 @@
import { createAppendToMpvLogHandler, createShowMpvOsdHandler } from './mpv-osd-log';
import {
createBuildAppendToMpvLogMainDepsHandler,
createBuildShowMpvOsdMainDepsHandler,
} from './mpv-osd-log-main-deps';
type AppendToMpvLogMainDeps = Parameters<typeof createBuildAppendToMpvLogMainDepsHandler>[0];
type ShowMpvOsdMainDeps = Parameters<typeof createBuildShowMpvOsdMainDepsHandler>[0];
export function createMpvOsdRuntimeHandlers(deps: {
appendToMpvLogMainDeps: AppendToMpvLogMainDeps;
buildShowMpvOsdMainDeps: (appendToMpvLog: (message: string) => void) => ShowMpvOsdMainDeps;
}) {
const appendToMpvLogMainDeps =
createBuildAppendToMpvLogMainDepsHandler(deps.appendToMpvLogMainDeps)();
const appendToMpvLogHandler = createAppendToMpvLogHandler(appendToMpvLogMainDeps);
const appendToMpvLog = (message: string) => appendToMpvLogHandler(message);
const showMpvOsdMainDeps = createBuildShowMpvOsdMainDepsHandler(
deps.buildShowMpvOsdMainDeps(appendToMpvLog),
)();
const showMpvOsdHandler = createShowMpvOsdHandler(showMpvOsdMainDeps);
const showMpvOsd = (text: string) => showMpvOsdHandler(text);
return {
appendToMpvLog,
showMpvOsd,
};
}

View File

@@ -0,0 +1,29 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createCycleSecondarySubModeRuntimeHandler } from './secondary-sub-mode-runtime-handler';
test('secondary sub mode runtime handler composes deps for runtime call', () => {
const calls: string[] = [];
const handleCycleSecondarySubMode = createCycleSecondarySubModeRuntimeHandler({
cycleSecondarySubModeMainDeps: {
getSecondarySubMode: () => 'hidden',
setSecondarySubMode: () => calls.push('set-mode'),
getLastSecondarySubToggleAtMs: () => 10,
setLastSecondarySubToggleAtMs: () => calls.push('set-ts'),
broadcastToOverlayWindows: (channel, mode) => calls.push(`broadcast:${channel}:${mode}`),
showMpvOsd: (text) => calls.push(`osd:${text}`),
},
cycleSecondarySubMode: (deps) => {
deps.setSecondarySubMode('romaji' as never);
deps.broadcastSecondarySubMode('romaji' as never);
deps.showMpvOsd('romaji');
},
});
handleCycleSecondarySubMode();
assert.deepEqual(calls, [
'set-mode',
'broadcast:secondary-subtitle:mode:romaji',
'osd:romaji',
]);
});

View File

@@ -0,0 +1,17 @@
import { createBuildCycleSecondarySubModeMainDepsHandler } from './secondary-sub-mode-main-deps';
type CycleSecondarySubModeMainDeps = Parameters<
typeof createBuildCycleSecondarySubModeMainDepsHandler
>[0];
type CycleSecondarySubModeDeps = ReturnType<
ReturnType<typeof createBuildCycleSecondarySubModeMainDepsHandler>
>;
export function createCycleSecondarySubModeRuntimeHandler(deps: {
cycleSecondarySubModeMainDeps: CycleSecondarySubModeMainDeps;
cycleSecondarySubMode: (deps: CycleSecondarySubModeDeps) => void;
}) {
const buildCycleSecondarySubModeMainDepsHandler =
createBuildCycleSecondarySubModeMainDepsHandler(deps.cycleSecondarySubModeMainDeps);
return () => deps.cycleSecondarySubMode(buildCycleSecondarySubModeMainDepsHandler());
}