mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract numeric shortcut runtime service
This commit is contained in:
@@ -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/cli-command-deps-runtime-service.test.js dist/core/services/ipc-deps-runtime-service.test.js dist/core/services/anki-jimaku-ipc-deps-runtime-service.test.js dist/core/services/field-grouping-overlay-runtime-service.test.js dist/core/services/subsync-deps-runtime-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 dist/core/services/config-warning-runtime-service.test.js dist/core/services/app-logging-runtime-service.test.js dist/core/services/startup-resource-runtime-service.test.js dist/core/services/config-generation-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/cli-command-deps-runtime-service.test.js dist/core/services/ipc-deps-runtime-service.test.js dist/core/services/anki-jimaku-ipc-deps-runtime-service.test.js dist/core/services/field-grouping-overlay-runtime-service.test.js dist/core/services/subsync-deps-runtime-service.test.js dist/core/services/numeric-shortcut-runtime-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 dist/core/services/config-warning-runtime-service.test.js dist/core/services/app-logging-runtime-service.test.js dist/core/services/startup-resource-runtime-service.test.js dist/core/services/config-generation-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",
|
||||||
|
|||||||
48
src/core/services/numeric-shortcut-runtime-service.test.ts
Normal file
48
src/core/services/numeric-shortcut-runtime-service.test.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { createNumericShortcutRuntimeService } from "./numeric-shortcut-runtime-service";
|
||||||
|
|
||||||
|
test("createNumericShortcutRuntimeService creates sessions wired to globalShortcut", () => {
|
||||||
|
const registered: string[] = [];
|
||||||
|
const unregistered: string[] = [];
|
||||||
|
const osd: string[] = [];
|
||||||
|
const handlers = new Map<string, () => void>();
|
||||||
|
|
||||||
|
const runtime = createNumericShortcutRuntimeService({
|
||||||
|
globalShortcut: {
|
||||||
|
register: (accelerator, callback) => {
|
||||||
|
registered.push(accelerator);
|
||||||
|
handlers.set(accelerator, callback);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
unregister: (accelerator) => {
|
||||||
|
unregistered.push(accelerator);
|
||||||
|
handlers.delete(accelerator);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
showMpvOsd: (text) => {
|
||||||
|
osd.push(text);
|
||||||
|
},
|
||||||
|
setTimer: () => setTimeout(() => {}, 1000),
|
||||||
|
clearTimer: (timer) => clearTimeout(timer),
|
||||||
|
});
|
||||||
|
|
||||||
|
const session = runtime.createSession();
|
||||||
|
session.start({
|
||||||
|
timeoutMs: 5000,
|
||||||
|
onDigit: () => {},
|
||||||
|
messages: {
|
||||||
|
prompt: "Select count",
|
||||||
|
timeout: "Timed out",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(session.isActive(), true);
|
||||||
|
assert.ok(registered.includes("1"));
|
||||||
|
assert.ok(registered.includes("Escape"));
|
||||||
|
assert.equal(osd[0], "Select count");
|
||||||
|
|
||||||
|
handlers.get("Escape")?.();
|
||||||
|
assert.equal(session.isActive(), false);
|
||||||
|
assert.ok(unregistered.includes("Escape"));
|
||||||
|
});
|
||||||
37
src/core/services/numeric-shortcut-runtime-service.ts
Normal file
37
src/core/services/numeric-shortcut-runtime-service.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import {
|
||||||
|
createNumericShortcutSessionService,
|
||||||
|
} from "./numeric-shortcut-session-service";
|
||||||
|
|
||||||
|
interface GlobalShortcutLike {
|
||||||
|
register: (accelerator: string, callback: () => void) => boolean;
|
||||||
|
unregister: (accelerator: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NumericShortcutRuntimeOptions {
|
||||||
|
globalShortcut: GlobalShortcutLike;
|
||||||
|
showMpvOsd: (text: string) => void;
|
||||||
|
setTimer: (
|
||||||
|
handler: () => void,
|
||||||
|
timeoutMs: number,
|
||||||
|
) => ReturnType<typeof setTimeout>;
|
||||||
|
clearTimer: (timer: ReturnType<typeof setTimeout>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createNumericShortcutRuntimeService(
|
||||||
|
options: NumericShortcutRuntimeOptions,
|
||||||
|
) {
|
||||||
|
const createSession = () =>
|
||||||
|
createNumericShortcutSessionService({
|
||||||
|
registerShortcut: (accelerator, handler) =>
|
||||||
|
options.globalShortcut.register(accelerator, handler),
|
||||||
|
unregisterShortcut: (accelerator) =>
|
||||||
|
options.globalShortcut.unregister(accelerator),
|
||||||
|
setTimer: options.setTimer,
|
||||||
|
clearTimer: options.clearTimer,
|
||||||
|
showMpvOsd: options.showMpvOsd,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
createSession,
|
||||||
|
};
|
||||||
|
}
|
||||||
21
src/main.ts
21
src/main.ts
@@ -112,7 +112,6 @@ import {
|
|||||||
} from "./core/services/overlay-shortcut-service";
|
} from "./core/services/overlay-shortcut-service";
|
||||||
import { runOverlayShortcutLocalFallback } from "./core/services/overlay-shortcut-fallback-runner";
|
import { runOverlayShortcutLocalFallback } from "./core/services/overlay-shortcut-fallback-runner";
|
||||||
import { createOverlayShortcutRuntimeHandlers } from "./core/services/overlay-shortcut-runtime-service";
|
import { createOverlayShortcutRuntimeHandlers } from "./core/services/overlay-shortcut-runtime-service";
|
||||||
import { createNumericShortcutSessionService } from "./core/services/numeric-shortcut-session-service";
|
|
||||||
import { handleCliCommandService } from "./core/services/cli-command-service";
|
import { handleCliCommandService } from "./core/services/cli-command-service";
|
||||||
import { cycleSecondarySubModeService } from "./core/services/secondary-subtitle-service";
|
import { cycleSecondarySubModeService } from "./core/services/secondary-subtitle-service";
|
||||||
import {
|
import {
|
||||||
@@ -205,6 +204,7 @@ import { createIpcDepsRuntimeService } from "./core/services/ipc-deps-runtime-se
|
|||||||
import { createAnkiJimakuIpcDepsRuntimeService } from "./core/services/anki-jimaku-ipc-deps-runtime-service";
|
import { createAnkiJimakuIpcDepsRuntimeService } from "./core/services/anki-jimaku-ipc-deps-runtime-service";
|
||||||
import { createFieldGroupingOverlayRuntimeService } from "./core/services/field-grouping-overlay-runtime-service";
|
import { createFieldGroupingOverlayRuntimeService } from "./core/services/field-grouping-overlay-runtime-service";
|
||||||
import { createSubsyncRuntimeDepsService } from "./core/services/subsync-deps-runtime-service";
|
import { createSubsyncRuntimeDepsService } from "./core/services/subsync-deps-runtime-service";
|
||||||
|
import { createNumericShortcutRuntimeService } from "./core/services/numeric-shortcut-runtime-service";
|
||||||
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
|
import { createRuntimeOptionsManagerRuntimeService } from "./core/services/runtime-options-manager-runtime-service";
|
||||||
import { createAppLoggingRuntimeService } from "./core/services/app-logging-runtime-service";
|
import { createAppLoggingRuntimeService } from "./core/services/app-logging-runtime-service";
|
||||||
import {
|
import {
|
||||||
@@ -959,23 +959,14 @@ function showMpvOsd(text: string): void {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const multiCopySession = createNumericShortcutSessionService({
|
const numericShortcutRuntime = createNumericShortcutRuntimeService({
|
||||||
registerShortcut: (accelerator, handler) =>
|
globalShortcut,
|
||||||
globalShortcut.register(accelerator, handler),
|
showMpvOsd: (text) => showMpvOsd(text),
|
||||||
unregisterShortcut: (accelerator) => globalShortcut.unregister(accelerator),
|
|
||||||
setTimer: (handler, timeoutMs) => setTimeout(handler, timeoutMs),
|
setTimer: (handler, timeoutMs) => setTimeout(handler, timeoutMs),
|
||||||
clearTimer: (timer) => clearTimeout(timer),
|
clearTimer: (timer) => clearTimeout(timer),
|
||||||
showMpvOsd: (text) => showMpvOsd(text),
|
|
||||||
});
|
|
||||||
|
|
||||||
const mineSentenceSession = createNumericShortcutSessionService({
|
|
||||||
registerShortcut: (accelerator, handler) =>
|
|
||||||
globalShortcut.register(accelerator, handler),
|
|
||||||
unregisterShortcut: (accelerator) => globalShortcut.unregister(accelerator),
|
|
||||||
setTimer: (handler, timeoutMs) => setTimeout(handler, timeoutMs),
|
|
||||||
clearTimer: (timer) => clearTimeout(timer),
|
|
||||||
showMpvOsd: (text) => showMpvOsd(text),
|
|
||||||
});
|
});
|
||||||
|
const multiCopySession = numericShortcutRuntime.createSession();
|
||||||
|
const mineSentenceSession = numericShortcutRuntime.createSession();
|
||||||
|
|
||||||
function getSubsyncRuntimeDeps() {
|
function getSubsyncRuntimeDeps() {
|
||||||
return createSubsyncRuntimeDepsService({
|
return createSubsyncRuntimeDepsService({
|
||||||
|
|||||||
Reference in New Issue
Block a user