mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-27 18:22:41 -08:00
refactor: extract startup and initial args runtime wiring
This commit is contained in:
113
src/main.ts
113
src/main.ts
@@ -243,8 +243,7 @@ import {
|
||||
createBuildStartJellyfinRemoteSessionMainDepsHandler,
|
||||
createBuildStopJellyfinRemoteSessionMainDepsHandler,
|
||||
} from './main/runtime/jellyfin-remote-session-main-deps';
|
||||
import { createHandleInitialArgsHandler } from './main/runtime/initial-args-handler';
|
||||
import { createBuildHandleInitialArgsMainDepsHandler } from './main/runtime/initial-args-main-deps';
|
||||
import { createInitialArgsRuntimeHandler } from './main/runtime/initial-args-runtime-handler';
|
||||
import { createHandleTexthookerOnlyModeTransitionHandler } from './main/runtime/cli-command-prechecks';
|
||||
import { createBuildHandleTexthookerOnlyModeTransitionMainDepsHandler } from './main/runtime/cli-command-prechecks-main-deps';
|
||||
import {
|
||||
@@ -394,7 +393,6 @@ import {
|
||||
createBuildRestoreWindowsOnActivateMainDepsHandler,
|
||||
createBuildShouldRestoreWindowsOnActivateMainDepsHandler,
|
||||
} from './main/runtime/app-lifecycle-main-activate';
|
||||
import { createBuildStartupBootstrapRuntimeFactoryDepsHandler } from './main/runtime/startup-bootstrap-deps-builder';
|
||||
import {
|
||||
buildRestartRequiredConfigMessage,
|
||||
createConfigHotReloadAppliedHandler,
|
||||
@@ -413,8 +411,7 @@ import {
|
||||
createBuildReloadConfigMainDepsHandler,
|
||||
} from './main/runtime/startup-config-main-deps';
|
||||
import { createBuildAppReadyRuntimeMainDepsHandler } from './main/runtime/app-ready-main-deps';
|
||||
import { createBuildAppLifecycleRuntimeRunnerMainDepsHandler } from './main/runtime/startup-lifecycle-main-deps';
|
||||
import { createBuildStartupBootstrapMainDepsHandler } from './main/runtime/startup-bootstrap-main-deps';
|
||||
import { createStartupRuntimeHandlers } from './main/runtime/startup-runtime-handlers';
|
||||
import {
|
||||
enforceUnsupportedWaylandMode,
|
||||
forceX11Backend,
|
||||
@@ -484,6 +481,8 @@ import { createAnilistUpdateQueue } from './core/services/anilist/anilist-update
|
||||
import { createJellyfinTokenStore } from './core/services/jellyfin-token-store';
|
||||
import { applyRuntimeOptionResultRuntime } from './core/services/runtime-options-ipc';
|
||||
import { createAppReadyRuntimeRunner } from './main/app-lifecycle';
|
||||
import { createStartupBootstrapRuntimeDeps } from './main/startup';
|
||||
import { createAppLifecycleRuntimeRunner } from './main/startup-lifecycle';
|
||||
import { handleMpvCommandFromIpcRuntime } from './main/ipc-mpv-command';
|
||||
import { registerIpcRuntimeServices } from './main/ipc-runtime';
|
||||
import { createAnkiJimakuIpcRuntimeServiceDeps } from './main/dependencies';
|
||||
@@ -500,13 +499,11 @@ import {
|
||||
} from './main/frequency-dictionary-runtime';
|
||||
import { createMediaRuntimeService } from './main/media-runtime';
|
||||
import { createOverlayVisibilityRuntimeService } from './main/overlay-visibility-runtime';
|
||||
import { type AppState, applyStartupState, createAppState } from './main/state';
|
||||
import { type AppState, type StartupState, applyStartupState, createAppState } from './main/state';
|
||||
import {
|
||||
isAllowedAnilistExternalUrl,
|
||||
isAllowedAnilistSetupNavigationUrl,
|
||||
} from './main/anilist-url-guard';
|
||||
import { createStartupBootstrapRuntimeDeps } from './main/startup';
|
||||
import { createAppLifecycleRuntimeRunner } from './main/startup-lifecycle';
|
||||
import {
|
||||
ConfigService,
|
||||
DEFAULT_CONFIG,
|
||||
@@ -2130,8 +2127,12 @@ const buildAppReadyRuntimeMainDepsHandler = createBuildAppReadyRuntimeMainDepsHa
|
||||
});
|
||||
const appReadyRuntimeRunner = createAppReadyRuntimeRunner(buildAppReadyRuntimeMainDepsHandler());
|
||||
|
||||
const buildAppLifecycleRuntimeRunnerMainDepsHandler =
|
||||
createBuildAppLifecycleRuntimeRunnerMainDepsHandler({
|
||||
const { appLifecycleRuntimeRunner, runAndApplyStartupState } = createStartupRuntimeHandlers<
|
||||
CliArgs,
|
||||
StartupState,
|
||||
ReturnType<typeof createStartupBootstrapRuntimeDeps>
|
||||
>({
|
||||
appLifecycleRuntimeRunnerMainDeps: {
|
||||
app,
|
||||
platform: process.platform,
|
||||
shouldStartApp: (nextArgs: CliArgs) => shouldStartApp(nextArgs),
|
||||
@@ -2145,53 +2146,47 @@ const buildAppLifecycleRuntimeRunnerMainDepsHandler =
|
||||
shouldRestoreWindowsOnActivate: () => shouldRestoreWindowsOnActivateHandler(),
|
||||
restoreWindowsOnActivate: () => restoreWindowsOnActivateHandler(),
|
||||
shouldQuitOnWindowAllClosed: () => !appState.backgroundMode,
|
||||
});
|
||||
const appLifecycleRuntimeRunnerMainDeps = buildAppLifecycleRuntimeRunnerMainDepsHandler();
|
||||
const appLifecycleRuntimeRunner = createAppLifecycleRuntimeRunner(
|
||||
appLifecycleRuntimeRunnerMainDeps,
|
||||
);
|
||||
},
|
||||
createAppLifecycleRuntimeRunner: (params) => createAppLifecycleRuntimeRunner(params),
|
||||
buildStartupBootstrapMainDeps: (startAppLifecycle) => ({
|
||||
argv: process.argv,
|
||||
parseArgs: (argv: string[]) => parseArgs(argv),
|
||||
setLogLevel: (level: string, source: LogLevelSource) => {
|
||||
setLogLevel(level, source);
|
||||
},
|
||||
forceX11Backend: (args: CliArgs) => {
|
||||
forceX11Backend(args);
|
||||
},
|
||||
enforceUnsupportedWaylandMode: (args: CliArgs) => {
|
||||
enforceUnsupportedWaylandMode(args);
|
||||
},
|
||||
shouldStartApp: (args: CliArgs) => shouldStartApp(args),
|
||||
getDefaultSocketPath: () => getDefaultSocketPath(),
|
||||
defaultTexthookerPort: DEFAULT_TEXTHOOKER_PORT,
|
||||
configDir: CONFIG_DIR,
|
||||
defaultConfig: DEFAULT_CONFIG,
|
||||
generateConfigTemplate: (config: ResolvedConfig) => generateConfigTemplate(config),
|
||||
generateDefaultConfigFile: (
|
||||
args: CliArgs,
|
||||
options: {
|
||||
configDir: string;
|
||||
defaultConfig: unknown;
|
||||
generateTemplate: (config: unknown) => string;
|
||||
},
|
||||
) => generateDefaultConfigFile(args, options),
|
||||
setExitCode: (code) => {
|
||||
process.exitCode = code;
|
||||
},
|
||||
quitApp: () => app.quit(),
|
||||
logGenerateConfigError: (message) => logger.error(message),
|
||||
startAppLifecycle,
|
||||
}),
|
||||
createStartupBootstrapRuntimeDeps: (deps) => createStartupBootstrapRuntimeDeps(deps),
|
||||
runStartupBootstrapRuntime,
|
||||
applyStartupState: (startupState) => applyStartupState(appState, startupState),
|
||||
});
|
||||
|
||||
const buildStartupBootstrapMainDepsHandler = createBuildStartupBootstrapMainDepsHandler({
|
||||
argv: process.argv,
|
||||
parseArgs: (argv: string[]) => parseArgs(argv),
|
||||
setLogLevel: (level: string, source: LogLevelSource) => {
|
||||
setLogLevel(level, source);
|
||||
},
|
||||
forceX11Backend: (args: CliArgs) => {
|
||||
forceX11Backend(args);
|
||||
},
|
||||
enforceUnsupportedWaylandMode: (args: CliArgs) => {
|
||||
enforceUnsupportedWaylandMode(args);
|
||||
},
|
||||
shouldStartApp: (args: CliArgs) => shouldStartApp(args),
|
||||
getDefaultSocketPath: () => getDefaultSocketPath(),
|
||||
defaultTexthookerPort: DEFAULT_TEXTHOOKER_PORT,
|
||||
configDir: CONFIG_DIR,
|
||||
defaultConfig: DEFAULT_CONFIG,
|
||||
generateConfigTemplate: (config: ResolvedConfig) => generateConfigTemplate(config),
|
||||
generateDefaultConfigFile: (
|
||||
args: CliArgs,
|
||||
options: {
|
||||
configDir: string;
|
||||
defaultConfig: unknown;
|
||||
generateTemplate: (config: unknown) => string;
|
||||
},
|
||||
) => generateDefaultConfigFile(args, options),
|
||||
setExitCode: (code) => {
|
||||
process.exitCode = code;
|
||||
},
|
||||
quitApp: () => app.quit(),
|
||||
logGenerateConfigError: (message) => logger.error(message),
|
||||
startAppLifecycle: appLifecycleRuntimeRunner,
|
||||
});
|
||||
const buildStartupBootstrapRuntimeFactoryDepsHandler =
|
||||
createBuildStartupBootstrapRuntimeFactoryDepsHandler(buildStartupBootstrapMainDepsHandler());
|
||||
|
||||
const startupState = runStartupBootstrapRuntime(
|
||||
createStartupBootstrapRuntimeDeps(buildStartupBootstrapRuntimeFactoryDepsHandler()),
|
||||
);
|
||||
|
||||
applyStartupState(appState, startupState);
|
||||
runAndApplyStartupState();
|
||||
void refreshAnilistClientSecretState({ force: true });
|
||||
anilistStateRuntime.refreshRetryQueueState();
|
||||
|
||||
@@ -2214,7 +2209,7 @@ function handleCliCommand(args: CliArgs, source: CliCommandSource = 'initial'):
|
||||
handleCliCommandRuntimeServiceWithContext(args, source, cliContext);
|
||||
}
|
||||
|
||||
const buildHandleInitialArgsMainDepsHandler = createBuildHandleInitialArgsMainDepsHandler({
|
||||
const handleInitialArgsRuntimeHandler = createInitialArgsRuntimeHandler({
|
||||
getInitialArgs: () => appState.initialArgs,
|
||||
isBackgroundMode: () => appState.backgroundMode,
|
||||
ensureTray: () => ensureTray(),
|
||||
@@ -2224,10 +2219,6 @@ const buildHandleInitialArgsMainDepsHandler = createBuildHandleInitialArgsMainDe
|
||||
logInfo: (message) => logger.info(message),
|
||||
handleCliCommand: (args, source) => handleCliCommand(args, source),
|
||||
});
|
||||
const handleInitialArgsMainDeps = buildHandleInitialArgsMainDepsHandler();
|
||||
const handleInitialArgsRuntimeHandler = createHandleInitialArgsHandler(
|
||||
handleInitialArgsMainDeps,
|
||||
);
|
||||
|
||||
function handleInitialArgs(): void {
|
||||
handleInitialArgsRuntimeHandler();
|
||||
|
||||
24
src/main/runtime/initial-args-runtime-handler.test.ts
Normal file
24
src/main/runtime/initial-args-runtime-handler.test.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createInitialArgsRuntimeHandler } from './initial-args-runtime-handler';
|
||||
|
||||
test('initial args runtime handler composes main deps and runs initial command flow', () => {
|
||||
const calls: string[] = [];
|
||||
const handleInitialArgs = createInitialArgsRuntimeHandler({
|
||||
getInitialArgs: () => ({ start: true } as never),
|
||||
isBackgroundMode: () => true,
|
||||
ensureTray: () => calls.push('tray'),
|
||||
isTexthookerOnlyMode: () => false,
|
||||
hasImmersionTracker: () => true,
|
||||
getMpvClient: () => ({
|
||||
connected: false,
|
||||
connect: () => calls.push('connect'),
|
||||
}),
|
||||
logInfo: (message) => calls.push(`log:${message}`),
|
||||
handleCliCommand: (_args, source) => calls.push(`cli:${source}`),
|
||||
});
|
||||
|
||||
handleInitialArgs();
|
||||
|
||||
assert.deepEqual(calls, ['tray', 'log:Auto-connecting MPV client for immersion tracking', 'connect', 'cli:initial']);
|
||||
});
|
||||
10
src/main/runtime/initial-args-runtime-handler.ts
Normal file
10
src/main/runtime/initial-args-runtime-handler.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { createHandleInitialArgsHandler } from './initial-args-handler';
|
||||
import { createBuildHandleInitialArgsMainDepsHandler } from './initial-args-main-deps';
|
||||
|
||||
type InitialArgsMainDeps = Parameters<typeof createBuildHandleInitialArgsMainDepsHandler>[0];
|
||||
|
||||
export function createInitialArgsRuntimeHandler(deps: InitialArgsMainDeps) {
|
||||
const buildHandleInitialArgsMainDepsHandler = createBuildHandleInitialArgsMainDepsHandler(deps);
|
||||
const handleInitialArgsMainDeps = buildHandleInitialArgsMainDepsHandler();
|
||||
return createHandleInitialArgsHandler(handleInitialArgsMainDeps);
|
||||
}
|
||||
72
src/main/runtime/startup-runtime-handlers.test.ts
Normal file
72
src/main/runtime/startup-runtime-handlers.test.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import type { CliArgs } from '../../cli/args';
|
||||
import { createStartupRuntimeHandlers } from './startup-runtime-handlers';
|
||||
|
||||
test('startup runtime handlers compose lifecycle runner and startup bootstrap apply flow', () => {
|
||||
const calls: string[] = [];
|
||||
const appliedStates: Array<{ mode: string }> = [];
|
||||
|
||||
const runtime = createStartupRuntimeHandlers<
|
||||
{ command: string },
|
||||
{ mode: string },
|
||||
{ startAppLifecycle: (args: CliArgs) => void }
|
||||
>({
|
||||
appLifecycleRuntimeRunnerMainDeps: {
|
||||
app: { on: () => {} } as never,
|
||||
platform: 'darwin',
|
||||
shouldStartApp: () => true,
|
||||
parseArgs: () => ({}) as never,
|
||||
handleCliCommand: () => calls.push('handle-cli'),
|
||||
printHelp: () => calls.push('help'),
|
||||
logNoRunningInstance: () => calls.push('no-instance'),
|
||||
onReady: async () => {
|
||||
calls.push('ready');
|
||||
},
|
||||
onWillQuitCleanup: () => calls.push('cleanup'),
|
||||
shouldRestoreWindowsOnActivate: () => true,
|
||||
restoreWindowsOnActivate: () => calls.push('restore'),
|
||||
shouldQuitOnWindowAllClosed: () => false,
|
||||
},
|
||||
createAppLifecycleRuntimeRunner: () => (args) => {
|
||||
calls.push(`lifecycle:${args.command}`);
|
||||
},
|
||||
buildStartupBootstrapMainDeps: (startAppLifecycle) => ({
|
||||
argv: ['node', 'main.js'],
|
||||
parseArgs: () => ({ command: 'start' }) as never,
|
||||
setLogLevel: () => {},
|
||||
forceX11Backend: () => {},
|
||||
enforceUnsupportedWaylandMode: () => {},
|
||||
shouldStartApp: () => true,
|
||||
getDefaultSocketPath: () => '/tmp/mpv.sock',
|
||||
defaultTexthookerPort: 5174,
|
||||
configDir: '/tmp/config',
|
||||
defaultConfig: {} as never,
|
||||
generateConfigTemplate: () => 'template',
|
||||
generateDefaultConfigFile: async () => 0,
|
||||
setExitCode: () => {},
|
||||
quitApp: () => {},
|
||||
logGenerateConfigError: () => {},
|
||||
startAppLifecycle: (args) => {
|
||||
const typedArgs = args as unknown as { command: string };
|
||||
calls.push(`start-lifecycle:${typedArgs.command}`);
|
||||
startAppLifecycle(typedArgs);
|
||||
},
|
||||
}),
|
||||
createStartupBootstrapRuntimeDeps: (deps) => ({
|
||||
startAppLifecycle: deps.startAppLifecycle,
|
||||
}),
|
||||
runStartupBootstrapRuntime: (deps) => {
|
||||
deps.startAppLifecycle({ command: 'start' } as never);
|
||||
return { mode: 'started' };
|
||||
},
|
||||
applyStartupState: (state) => {
|
||||
appliedStates.push(state);
|
||||
},
|
||||
});
|
||||
|
||||
const state = runtime.runAndApplyStartupState();
|
||||
assert.deepEqual(state, { mode: 'started' });
|
||||
assert.deepEqual(appliedStates, [{ mode: 'started' }]);
|
||||
assert.deepEqual(calls, ['start-lifecycle:start', 'lifecycle:start']);
|
||||
});
|
||||
55
src/main/runtime/startup-runtime-handlers.ts
Normal file
55
src/main/runtime/startup-runtime-handlers.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { createBuildStartupBootstrapRuntimeFactoryDepsHandler } from './startup-bootstrap-deps-builder';
|
||||
import { createBuildAppLifecycleRuntimeRunnerMainDepsHandler } from './startup-lifecycle-main-deps';
|
||||
import { createBuildStartupBootstrapMainDepsHandler } from './startup-bootstrap-main-deps';
|
||||
|
||||
type AppLifecycleRuntimeRunnerMainDeps = Parameters<
|
||||
typeof createBuildAppLifecycleRuntimeRunnerMainDepsHandler
|
||||
>[0];
|
||||
type StartupBootstrapMainDeps = Parameters<typeof createBuildStartupBootstrapMainDepsHandler>[0];
|
||||
type StartupBootstrapRuntimeFactoryDeps = Parameters<
|
||||
typeof createBuildStartupBootstrapRuntimeFactoryDepsHandler
|
||||
>[0];
|
||||
|
||||
export function createStartupRuntimeHandlers<
|
||||
TCliArgs,
|
||||
TStartupState,
|
||||
TStartupBootstrapRuntimeDeps = unknown,
|
||||
>(deps: {
|
||||
appLifecycleRuntimeRunnerMainDeps: AppLifecycleRuntimeRunnerMainDeps;
|
||||
createAppLifecycleRuntimeRunner: (
|
||||
params: AppLifecycleRuntimeRunnerMainDeps,
|
||||
) => (args: TCliArgs) => void;
|
||||
buildStartupBootstrapMainDeps: (
|
||||
startAppLifecycle: (args: TCliArgs) => void,
|
||||
) => StartupBootstrapMainDeps;
|
||||
createStartupBootstrapRuntimeDeps: (
|
||||
deps: StartupBootstrapRuntimeFactoryDeps,
|
||||
) => TStartupBootstrapRuntimeDeps;
|
||||
runStartupBootstrapRuntime: (deps: TStartupBootstrapRuntimeDeps) => TStartupState;
|
||||
applyStartupState: (startupState: TStartupState) => void;
|
||||
}) {
|
||||
const appLifecycleRuntimeRunnerMainDeps =
|
||||
createBuildAppLifecycleRuntimeRunnerMainDepsHandler(deps.appLifecycleRuntimeRunnerMainDeps)();
|
||||
const appLifecycleRuntimeRunner = deps.createAppLifecycleRuntimeRunner(
|
||||
appLifecycleRuntimeRunnerMainDeps,
|
||||
);
|
||||
|
||||
const startupBootstrapMainDeps = createBuildStartupBootstrapMainDepsHandler(
|
||||
deps.buildStartupBootstrapMainDeps(appLifecycleRuntimeRunner),
|
||||
)();
|
||||
const startupBootstrapRuntimeFactoryDeps =
|
||||
createBuildStartupBootstrapRuntimeFactoryDepsHandler(startupBootstrapMainDeps)();
|
||||
|
||||
const runAndApplyStartupState = (): TStartupState => {
|
||||
const startupState = deps.runStartupBootstrapRuntime(
|
||||
deps.createStartupBootstrapRuntimeDeps(startupBootstrapRuntimeFactoryDeps),
|
||||
);
|
||||
deps.applyStartupState(startupState);
|
||||
return startupState;
|
||||
};
|
||||
|
||||
return {
|
||||
appLifecycleRuntimeRunner,
|
||||
runAndApplyStartupState,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user