refactor(ipc): centralize contracts and validate payloads

This commit is contained in:
2026-02-21 17:02:00 -08:00
parent 2a5830c4c5
commit 05be13be9e
16 changed files with 915 additions and 190 deletions

View File

@@ -26,23 +26,17 @@ export interface SubsyncRuntimeDepsParams {
}
export function createRuntimeOptionsIpcDeps(params: RuntimeOptionsIpcDepsParams): {
setRuntimeOption: (id: string, value: unknown) => unknown;
cycleRuntimeOption: (id: string, direction: 1 | -1) => unknown;
setRuntimeOption: (id: RuntimeOptionId, value: RuntimeOptionValue) => unknown;
cycleRuntimeOption: (id: RuntimeOptionId, direction: 1 | -1) => unknown;
} {
return {
setRuntimeOption: (id, value) =>
setRuntimeOptionFromIpcRuntime(
params.getRuntimeOptionsManager(),
id as RuntimeOptionId,
value as RuntimeOptionValue,
(text) => params.showMpvOsd(text),
setRuntimeOptionFromIpcRuntime(params.getRuntimeOptionsManager(), id, value, (text) =>
params.showMpvOsd(text),
),
cycleRuntimeOption: (id, direction) =>
cycleRuntimeOptionFromIpcRuntime(
params.getRuntimeOptionsManager(),
id as RuntimeOptionId,
direction,
(text) => params.showMpvOsd(text),
cycleRuntimeOptionFromIpcRuntime(params.getRuntimeOptionsManager(), id, direction, (text) =>
params.showMpvOsd(text),
),
};
}

View File

@@ -4,6 +4,7 @@ import { composeIpcRuntimeHandlers } from './ipc-runtime-composer';
test('composeIpcRuntimeHandlers returns callable IPC handlers and registration bridge', async () => {
let registered = false;
let receivedSourceTrackId: number | null | undefined;
const composed = composeIpcRuntimeHandlers({
mpvCommandMainDeps: {
@@ -18,10 +19,13 @@ test('composeIpcRuntimeHandlers returns callable IPC handlers and registration b
hasRuntimeOptionsManager: () => true,
},
handleMpvCommandFromIpcRuntime: () => {},
runSubsyncManualFromIpc: async (request) => ({
ok: true,
received: (request as { value: number }).value,
}),
runSubsyncManualFromIpc: async (request) => {
receivedSourceTrackId = request.sourceTrackId;
return {
ok: true,
message: 'ok',
};
},
registration: {
runtimeOptions: {
getRuntimeOptionsManager: () => null,
@@ -92,11 +96,12 @@ test('composeIpcRuntimeHandlers returns callable IPC handlers and registration b
assert.equal(typeof composed.runSubsyncManualFromIpc, 'function');
assert.equal(typeof composed.registerIpcRuntimeHandlers, 'function');
const result = (await composed.runSubsyncManualFromIpc({ value: 7 })) as {
ok: boolean;
received: number;
};
assert.deepEqual(result, { ok: true, received: 7 });
const result = await composed.runSubsyncManualFromIpc({
engine: 'alass',
sourceTrackId: 7,
});
assert.deepEqual(result, { ok: true, message: 'ok' });
assert.equal(receivedSourceTrackId, 7);
composed.registerIpcRuntimeHandlers();
assert.equal(registered, true);

View File

@@ -1,4 +1,5 @@
import type { RegisterIpcRuntimeServicesParams } from '../../ipc-runtime';
import type { SubsyncManualRunRequest, SubsyncResult } from '../../../types';
import {
createBuildMpvCommandFromIpcRuntimeMainDepsHandler,
createIpcRuntimeHandlers,
@@ -11,7 +12,9 @@ type IpcMainDeps = RegisterIpcRuntimeServicesParams['mainDeps'];
type IpcMainDepsWithoutHandlers = Omit<IpcMainDeps, 'handleMpvCommand' | 'runSubsyncManual'>;
type RunSubsyncManual = IpcMainDeps['runSubsyncManual'];
type IpcRuntimeDeps = Parameters<typeof createIpcRuntimeHandlers<unknown, unknown>>[0];
type IpcRuntimeDeps = Parameters<
typeof createIpcRuntimeHandlers<SubsyncManualRunRequest, SubsyncResult>
>[0];
export type IpcRuntimeComposerOptions = ComposerInputs<{
mpvCommandMainDeps: Parameters<typeof createBuildMpvCommandFromIpcRuntimeMainDepsHandler>[0];
@@ -38,8 +41,8 @@ export function composeIpcRuntimeHandlers(
options.mpvCommandMainDeps,
)();
const { handleMpvCommandFromIpc, runSubsyncManualFromIpc } = createIpcRuntimeHandlers<
unknown,
unknown
SubsyncManualRunRequest,
SubsyncResult
>({
handleMpvCommandFromIpcDeps: {
handleMpvCommandFromIpcRuntime: options.handleMpvCommandFromIpcRuntime,
@@ -56,7 +59,7 @@ export function composeIpcRuntimeHandlers(
mainDeps: {
...options.registration.mainDeps,
handleMpvCommand: (command) => handleMpvCommandFromIpc(command),
runSubsyncManual: (request: unknown) => runSubsyncManualFromIpc(request),
runSubsyncManual: (request) => runSubsyncManualFromIpc(request),
},
ankiJimakuDeps: options.registration.ankiJimakuDeps,
});