mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 06:22:45 -08:00
refactor: extract numeric shortcut runtime service
This commit is contained in:
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user