refactor: extract numeric and overlay shortcuts runtime wiring

This commit is contained in:
2026-02-20 03:35:47 -08:00
parent eef8a7eb41
commit 12c5d956bc
7 changed files with 203 additions and 138 deletions

View File

@@ -0,0 +1,35 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createNumericShortcutSessionRuntimeHandlers } from './numeric-shortcut-session-runtime-handlers';
test('numeric shortcut session runtime handlers compose cancel/start handlers', () => {
const calls: string[] = [];
const createSession = (name: string) => ({
start: ({ timeoutMs, onDigit }: { timeoutMs: number; onDigit: (digit: number) => void }) => {
calls.push(`${name}:start:${timeoutMs}`);
onDigit(3);
},
cancel: () => calls.push(`${name}:cancel`),
});
const runtime = createNumericShortcutSessionRuntimeHandlers({
multiCopySession: createSession('multi-copy'),
mineSentenceSession: createSession('mine-sentence'),
onMultiCopyDigit: (count) => calls.push(`multi-copy:digit:${count}`),
onMineSentenceDigit: (count) => calls.push(`mine-sentence:digit:${count}`),
});
runtime.cancelPendingMultiCopy();
runtime.startPendingMultiCopy(500);
runtime.cancelPendingMineSentenceMultiple();
runtime.startPendingMineSentenceMultiple(700);
assert.deepEqual(calls, [
'multi-copy:cancel',
'multi-copy:start:500',
'multi-copy:digit:3',
'mine-sentence:cancel',
'mine-sentence:start:700',
'mine-sentence:digit:3',
]);
});

View File

@@ -0,0 +1,67 @@
import {
createCancelNumericShortcutSessionHandler,
createStartNumericShortcutSessionHandler,
} from './numeric-shortcut-session-handlers';
import {
createBuildCancelNumericShortcutSessionMainDepsHandler,
createBuildStartNumericShortcutSessionMainDepsHandler,
} from './numeric-shortcut-session-main-deps';
type CancelNumericShortcutSessionMainDeps = Parameters<
typeof createBuildCancelNumericShortcutSessionMainDepsHandler
>[0];
export function createNumericShortcutSessionRuntimeHandlers(deps: {
multiCopySession: CancelNumericShortcutSessionMainDeps['session'];
mineSentenceSession: CancelNumericShortcutSessionMainDeps['session'];
onMultiCopyDigit: (count: number) => void;
onMineSentenceDigit: (count: number) => void;
}) {
const cancelPendingMultiCopyMainDeps = createBuildCancelNumericShortcutSessionMainDepsHandler({
session: deps.multiCopySession,
})();
const cancelPendingMultiCopyHandler =
createCancelNumericShortcutSessionHandler(cancelPendingMultiCopyMainDeps);
const startPendingMultiCopyMainDeps = createBuildStartNumericShortcutSessionMainDepsHandler({
session: deps.multiCopySession,
onDigit: deps.onMultiCopyDigit,
messages: {
prompt: 'Copy how many lines? Press 1-9 (Esc to cancel)',
timeout: 'Copy timeout',
cancelled: 'Cancelled',
},
})();
const startPendingMultiCopyHandler =
createStartNumericShortcutSessionHandler(startPendingMultiCopyMainDeps);
const cancelPendingMineSentenceMultipleMainDeps =
createBuildCancelNumericShortcutSessionMainDepsHandler({
session: deps.mineSentenceSession,
})();
const cancelPendingMineSentenceMultipleHandler = createCancelNumericShortcutSessionHandler(
cancelPendingMineSentenceMultipleMainDeps,
);
const startPendingMineSentenceMultipleMainDeps =
createBuildStartNumericShortcutSessionMainDepsHandler({
session: deps.mineSentenceSession,
onDigit: deps.onMineSentenceDigit,
messages: {
prompt: 'Mine how many lines? Press 1-9 (Esc to cancel)',
timeout: 'Mine sentence timeout',
cancelled: 'Cancelled',
},
})();
const startPendingMineSentenceMultipleHandler = createStartNumericShortcutSessionHandler(
startPendingMineSentenceMultipleMainDeps,
);
return {
cancelPendingMultiCopy: () => cancelPendingMultiCopyHandler(),
startPendingMultiCopy: (timeoutMs: number) => startPendingMultiCopyHandler(timeoutMs),
cancelPendingMineSentenceMultiple: () => cancelPendingMineSentenceMultipleHandler(),
startPendingMineSentenceMultiple: (timeoutMs: number) =>
startPendingMineSentenceMultipleHandler(timeoutMs),
};
}

View File

@@ -0,0 +1,24 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createOverlayShortcutsRuntimeHandlers } from './overlay-shortcuts-runtime-handlers';
test('overlay shortcuts runtime handlers compose lifecycle handlers', () => {
const calls: string[] = [];
const runtime = createOverlayShortcutsRuntimeHandlers({
overlayShortcutsRuntimeMainDeps: {
overlayShortcutsRuntime: {
registerOverlayShortcuts: () => calls.push('register'),
unregisterOverlayShortcuts: () => calls.push('unregister'),
syncOverlayShortcuts: () => calls.push('sync'),
refreshOverlayShortcuts: () => calls.push('refresh'),
},
},
});
runtime.registerOverlayShortcuts();
runtime.unregisterOverlayShortcuts();
runtime.syncOverlayShortcuts();
runtime.refreshOverlayShortcuts();
assert.deepEqual(calls, ['register', 'unregister', 'sync', 'refresh']);
});

View File

@@ -0,0 +1,51 @@
import {
createRefreshOverlayShortcutsHandler,
createRegisterOverlayShortcutsHandler,
createSyncOverlayShortcutsHandler,
createUnregisterOverlayShortcutsHandler,
} from './overlay-shortcuts-lifecycle';
import {
createBuildRefreshOverlayShortcutsMainDepsHandler,
createBuildRegisterOverlayShortcutsMainDepsHandler,
createBuildSyncOverlayShortcutsMainDepsHandler,
createBuildUnregisterOverlayShortcutsMainDepsHandler,
} from './overlay-shortcuts-lifecycle-main-deps';
type RegisterOverlayShortcutsMainDeps = Parameters<
typeof createBuildRegisterOverlayShortcutsMainDepsHandler
>[0];
export function createOverlayShortcutsRuntimeHandlers(deps: {
overlayShortcutsRuntimeMainDeps: RegisterOverlayShortcutsMainDeps;
}) {
const registerOverlayShortcutsMainDeps = createBuildRegisterOverlayShortcutsMainDepsHandler(
deps.overlayShortcutsRuntimeMainDeps,
)();
const registerOverlayShortcutsHandler =
createRegisterOverlayShortcutsHandler(registerOverlayShortcutsMainDeps);
const unregisterOverlayShortcutsMainDeps =
createBuildUnregisterOverlayShortcutsMainDepsHandler(
deps.overlayShortcutsRuntimeMainDeps,
)();
const unregisterOverlayShortcutsHandler =
createUnregisterOverlayShortcutsHandler(unregisterOverlayShortcutsMainDeps);
const syncOverlayShortcutsMainDeps = createBuildSyncOverlayShortcutsMainDepsHandler(
deps.overlayShortcutsRuntimeMainDeps,
)();
const syncOverlayShortcutsHandler = createSyncOverlayShortcutsHandler(syncOverlayShortcutsMainDeps);
const refreshOverlayShortcutsMainDeps = createBuildRefreshOverlayShortcutsMainDepsHandler(
deps.overlayShortcutsRuntimeMainDeps,
)();
const refreshOverlayShortcutsHandler =
createRefreshOverlayShortcutsHandler(refreshOverlayShortcutsMainDeps);
return {
registerOverlayShortcuts: () => registerOverlayShortcutsHandler(),
unregisterOverlayShortcuts: () => unregisterOverlayShortcutsHandler(),
syncOverlayShortcuts: () => syncOverlayShortcutsHandler(),
refreshOverlayShortcuts: () => refreshOverlayShortcutsHandler(),
};
}