refactor: extract runtime options manager initializer

This commit is contained in:
2026-02-10 00:26:20 -08:00
parent caa9a40585
commit c624b940b3
4 changed files with 55 additions and 14 deletions

View File

@@ -16,7 +16,7 @@
"docs:build": "vitepress build docs", "docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort", "docs:preview": "vitepress preview docs --host 0.0.0.0 --port 4173 --strictPort",
"test:config": "pnpm run build && node --test dist/config/config.test.js", "test:config": "pnpm run build && node --test dist/config/config.test.js",
"test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js", "test:core": "pnpm run build && node --test dist/cli/args.test.js dist/cli/help.test.js dist/core/services/cli-command-service.test.js dist/core/services/numeric-shortcut-session-service.test.js dist/core/services/secondary-subtitle-service.test.js dist/core/services/mpv-render-metrics-service.test.js dist/core/services/mpv-runtime-service.test.js dist/core/services/runtime-options-runtime-service.test.js dist/core/services/overlay-modal-restore-service.test.js dist/core/services/runtime-config-service.test.js dist/core/services/overlay-bridge-runtime-service.test.js dist/core/services/overlay-visibility-facade-service.test.js dist/core/services/overlay-broadcast-runtime-service.test.js dist/core/services/app-ready-runtime-service.test.js dist/core/services/app-shutdown-runtime-service.test.js dist/core/services/mpv-client-deps-runtime-service.test.js dist/core/services/app-lifecycle-deps-runtime-service.test.js dist/core/services/runtime-options-manager-runtime-service.test.js",
"test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js", "test:subtitle": "pnpm run build && node --test dist/subtitle/stages.test.js dist/subtitle/pipeline.test.js",
"generate:config-example": "pnpm run build && node dist/generate-config-example.js", "generate:config-example": "pnpm run build && node dist/generate-config-example.js",
"start": "pnpm run build && electron . --start", "start": "pnpm run build && electron . --start",

View File

@@ -0,0 +1,25 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createRuntimeOptionsManagerRuntimeService } from "./runtime-options-manager-runtime-service";
test("createRuntimeOptionsManagerRuntimeService wires patch + options changed callbacks", () => {
const patches: unknown[] = [];
const changedSnapshots: unknown[] = [];
const manager = createRuntimeOptionsManagerRuntimeService({
getAnkiConfig: () => ({
behavior: { autoUpdateNewCards: true },
isKiku: { fieldGrouping: "manual" },
}),
applyAnkiPatch: (patch) => {
patches.push(patch);
},
onOptionsChanged: (options) => {
changedSnapshots.push(options);
},
});
const result = manager.setOptionValue("anki.autoUpdateNewCards", false);
assert.equal(result.ok, true);
assert.equal(patches.length > 0, true);
assert.equal(changedSnapshots.length > 0, true);
});

View File

@@ -0,0 +1,17 @@
import { RuntimeOptionsManager } from "../../runtime-options";
import { AnkiConnectConfig, RuntimeOptionState } from "../../types";
export interface RuntimeOptionsManagerRuntimeDeps {
getAnkiConfig: () => AnkiConnectConfig;
applyAnkiPatch: (patch: Partial<AnkiConnectConfig>) => void;
onOptionsChanged: (options: RuntimeOptionState[]) => void;
}
export function createRuntimeOptionsManagerRuntimeService(
deps: RuntimeOptionsManagerRuntimeDeps,
): RuntimeOptionsManager {
return new RuntimeOptionsManager(deps.getAnkiConfig, {
applyAnkiPatch: deps.applyAnkiPatch,
onOptionsChanged: deps.onOptionsChanged,
});
}

View File

@@ -205,6 +205,7 @@ import { runAppReadyRuntimeService } from "./core/services/app-ready-runtime-ser
import { runAppShutdownRuntimeService } from "./core/services/app-shutdown-runtime-service"; import { runAppShutdownRuntimeService } from "./core/services/app-shutdown-runtime-service";
import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service"; import { createMpvIpcClientDepsRuntimeService } from "./core/services/mpv-client-deps-runtime-service";
import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service"; import { createAppLifecycleDepsRuntimeService } from "./core/services/app-lifecycle-deps-runtime-service";
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
import { import {
runSubsyncManualFromIpcRuntimeService, runSubsyncManualFromIpcRuntimeService,
triggerSubsyncFromConfigRuntimeService, triggerSubsyncFromConfigRuntimeService,
@@ -537,20 +538,18 @@ if (initialArgs.generateConfig && !shouldStartApp(initialArgs)) {
); );
}, },
initRuntimeOptionsManager: () => { initRuntimeOptionsManager: () => {
runtimeOptionsManager = new RuntimeOptionsManager( runtimeOptionsManager = createRuntimeOptionsManagerRuntimeService({
() => configService.getConfig().ankiConnect, getAnkiConfig: () => configService.getConfig().ankiConnect,
{ applyAnkiPatch: (patch) => {
applyAnkiPatch: (patch) => { if (ankiIntegration) {
if (ankiIntegration) { ankiIntegration.applyRuntimeConfigPatch(patch);
ankiIntegration.applyRuntimeConfigPatch(patch); }
}
},
onOptionsChanged: () => {
broadcastRuntimeOptionsChanged();
refreshOverlayShortcuts();
},
}, },
); onOptionsChanged: () => {
broadcastRuntimeOptionsChanged();
refreshOverlayShortcuts();
},
});
}, },
setSecondarySubMode: (mode) => { setSecondarySubMode: (mode) => {
secondarySubMode = mode; secondarySubMode = mode;