refactor: extract ipc runtime handler wiring

This commit is contained in:
2026-02-20 02:04:50 -08:00
parent 5b432fa156
commit f56de54c10
5 changed files with 91 additions and 29 deletions

View File

@@ -0,0 +1,38 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createIpcRuntimeHandlers } from './ipc-runtime-handlers';
test('ipc runtime handlers wire command and subsync handlers through built deps', async () => {
let receivedCommand: (string | number)[] | null = null;
let receivedCommandDeps: { tag: string } | null = null;
let buildMpvCommandDepsCalls = 0;
let receivedSubsyncRequest: { id: string } | null = null;
const runtime = createIpcRuntimeHandlers({
handleMpvCommandFromIpcDeps: {
handleMpvCommandFromIpcRuntime: (command, deps) => {
receivedCommand = command;
receivedCommandDeps = deps as unknown as { tag: string };
},
buildMpvCommandDeps: () => {
buildMpvCommandDepsCalls += 1;
return { tag: 'mpv-deps' } as never;
},
},
runSubsyncManualFromIpcDeps: {
runManualFromIpc: async (request: { id: string }) => {
receivedSubsyncRequest = request;
return { ok: true, id: request.id };
},
},
});
runtime.handleMpvCommandFromIpc(['set_property', 'pause', 'yes']);
assert.deepEqual(receivedCommand, ['set_property', 'pause', 'yes']);
assert.deepEqual(receivedCommandDeps, { tag: 'mpv-deps' });
assert.equal(buildMpvCommandDepsCalls, 1);
const response = await runtime.runSubsyncManualFromIpc({ id: 'abc' });
assert.deepEqual(receivedSubsyncRequest, { id: 'abc' });
assert.deepEqual(response, { ok: true, id: 'abc' });
});

View File

@@ -0,0 +1,37 @@
import { createHandleMpvCommandFromIpcHandler, createRunSubsyncManualFromIpcHandler } from './ipc-bridge-actions';
import {
createBuildHandleMpvCommandFromIpcMainDepsHandler,
createBuildRunSubsyncManualFromIpcMainDepsHandler,
} from './ipc-bridge-actions-main-deps';
type HandleMpvCommandFromIpcMainDeps = Parameters<
typeof createBuildHandleMpvCommandFromIpcMainDepsHandler
>[0];
type RunSubsyncManualFromIpcMainDeps<TRequest, TResult> = Parameters<
typeof createBuildRunSubsyncManualFromIpcMainDepsHandler<TRequest, TResult>
>[0];
export function createIpcRuntimeHandlers<TRequest, TResult>(deps: {
handleMpvCommandFromIpcDeps: HandleMpvCommandFromIpcMainDeps;
runSubsyncManualFromIpcDeps: RunSubsyncManualFromIpcMainDeps<TRequest, TResult>;
}) {
const handleMpvCommandFromIpcMainDeps = createBuildHandleMpvCommandFromIpcMainDepsHandler(
deps.handleMpvCommandFromIpcDeps,
)();
const handleMpvCommandFromIpc = createHandleMpvCommandFromIpcHandler(
handleMpvCommandFromIpcMainDeps,
);
const runSubsyncManualFromIpcMainDeps =
createBuildRunSubsyncManualFromIpcMainDepsHandler<TRequest, TResult>(
deps.runSubsyncManualFromIpcDeps,
)();
const runSubsyncManualFromIpc = createRunSubsyncManualFromIpcHandler<TRequest, TResult>(
runSubsyncManualFromIpcMainDeps,
);
return {
handleMpvCommandFromIpc,
runSubsyncManualFromIpc,
};
}