mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-13 08:12:54 -07:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import type { BackgroundStatsServerState } from './stats-daemon';
|
|
|
|
type EnsureStatsServerUrlDeps = {
|
|
currentPid: number;
|
|
readBackgroundState: () => BackgroundStatsServerState | null;
|
|
removeBackgroundState: () => void;
|
|
isProcessAlive: (pid: number) => boolean;
|
|
hasLocalStatsServer: () => boolean;
|
|
startLocalStatsServer: () => void;
|
|
getConfiguredPort: () => number;
|
|
};
|
|
|
|
function formatStatsServerUrl(port: number): string {
|
|
return `http://127.0.0.1:${port}`;
|
|
}
|
|
|
|
export type EnsureStatsServerUrlResult = { url: string; source: 'background' | 'local' };
|
|
|
|
export function createEnsureStatsServerUrlHandler(
|
|
deps: EnsureStatsServerUrlDeps,
|
|
): () => EnsureStatsServerUrlResult {
|
|
return () => {
|
|
const state = deps.readBackgroundState();
|
|
if (!state) {
|
|
deps.removeBackgroundState();
|
|
} else if (state.pid === deps.currentPid && !deps.hasLocalStatsServer()) {
|
|
deps.removeBackgroundState();
|
|
} else if (!deps.isProcessAlive(state.pid)) {
|
|
deps.removeBackgroundState();
|
|
} else if (state.pid !== deps.currentPid) {
|
|
return { url: formatStatsServerUrl(state.port), source: 'background' };
|
|
}
|
|
|
|
if (!deps.hasLocalStatsServer()) {
|
|
deps.startLocalStatsServer();
|
|
}
|
|
return { url: formatStatsServerUrl(deps.getConfiguredPort()), source: 'local' };
|
|
};
|
|
}
|