- Added support for a new worker in the team file system to list teams, improving performance and reliability in team management. - Introduced event loop lag monitoring in various IPC handlers to track and log slow operations, enhancing observability. - Implemented caching for CLI installation status to reduce redundant calls and improve responsiveness. - Updated project and team data services to include total session counts, optimizing data handling and performance. - Enhanced error handling and logging across multiple services to provide clearer insights into performance issues and failures. Made-with: Cursor
103 lines
3.3 KiB
TypeScript
103 lines
3.3 KiB
TypeScript
/**
|
|
* IPC Handlers for CLI Installer Operations.
|
|
*
|
|
* Handlers:
|
|
* - cliInstaller:getStatus: Get current CLI installation status
|
|
* - cliInstaller:install: Start CLI install/update flow
|
|
* - cliInstaller:progress: Progress events (main → renderer, not a handler)
|
|
*/
|
|
|
|
import {
|
|
CLI_INSTALLER_GET_STATUS,
|
|
CLI_INSTALLER_INSTALL,
|
|
// eslint-disable-next-line boundaries/element-types -- IPC channel constants shared between main and preload
|
|
} from '@preload/constants/ipcChannels';
|
|
import { getErrorMessage } from '@shared/utils/errorHandling';
|
|
import { createLogger } from '@shared/utils/logger';
|
|
|
|
import type { CliInstallerService } from '../services';
|
|
import type { CliInstallationStatus, IpcResult } from '@shared/types';
|
|
import type { IpcMain, IpcMainInvokeEvent } from 'electron';
|
|
|
|
const logger = createLogger('IPC:cliInstaller');
|
|
|
|
let service: CliInstallerService;
|
|
let statusInFlight: Promise<CliInstallationStatus> | null = null;
|
|
let cachedStatus: { value: CliInstallationStatus; at: number } | null = null;
|
|
const STATUS_CACHE_TTL_MS = 5_000;
|
|
|
|
/**
|
|
* Initializes CLI installer handlers with the service instance.
|
|
*/
|
|
export function initializeCliInstallerHandlers(installerService: CliInstallerService): void {
|
|
service = installerService;
|
|
}
|
|
|
|
/**
|
|
* Registers all CLI installer IPC handlers.
|
|
*/
|
|
export function registerCliInstallerHandlers(ipcMain: IpcMain): void {
|
|
ipcMain.handle(CLI_INSTALLER_GET_STATUS, handleGetStatus);
|
|
ipcMain.handle(CLI_INSTALLER_INSTALL, handleInstall);
|
|
|
|
logger.info('CLI installer handlers registered');
|
|
}
|
|
|
|
/**
|
|
* Removes all CLI installer IPC handlers.
|
|
*/
|
|
export function removeCliInstallerHandlers(ipcMain: IpcMain): void {
|
|
ipcMain.removeHandler(CLI_INSTALLER_GET_STATUS);
|
|
ipcMain.removeHandler(CLI_INSTALLER_INSTALL);
|
|
|
|
logger.info('CLI installer handlers removed');
|
|
}
|
|
|
|
// =============================================================================
|
|
// Handler Implementations
|
|
// =============================================================================
|
|
|
|
async function handleGetStatus(
|
|
_event: IpcMainInvokeEvent
|
|
): Promise<IpcResult<CliInstallationStatus>> {
|
|
try {
|
|
if (cachedStatus && Date.now() - cachedStatus.at < STATUS_CACHE_TTL_MS) {
|
|
return { success: true, data: cachedStatus.value };
|
|
}
|
|
|
|
if (!statusInFlight) {
|
|
const startedAt = Date.now();
|
|
statusInFlight = service
|
|
.getStatus()
|
|
.then((status) => {
|
|
cachedStatus = { value: status, at: Date.now() };
|
|
return status;
|
|
})
|
|
.finally(() => {
|
|
const ms = Date.now() - startedAt;
|
|
if (ms >= 2000) {
|
|
logger.warn(`cliInstaller:getStatus slow ms=${ms}`);
|
|
}
|
|
statusInFlight = null;
|
|
});
|
|
}
|
|
|
|
const status = await statusInFlight;
|
|
return { success: true, data: status };
|
|
} catch (error) {
|
|
const msg = getErrorMessage(error);
|
|
logger.error('Error in cliInstaller:getStatus:', msg);
|
|
return { success: false, error: msg };
|
|
}
|
|
}
|
|
|
|
async function handleInstall(_event: IpcMainInvokeEvent): Promise<IpcResult<void>> {
|
|
try {
|
|
await service.install();
|
|
return { success: true, data: undefined };
|
|
} catch (error) {
|
|
const msg = getErrorMessage(error);
|
|
logger.error('Error in cliInstaller:install:', msg);
|
|
return { success: false, error: msg };
|
|
}
|
|
}
|