continue-custom/extensions/cli/src/services/BaseService.ts
Nate be45ab63ca Add 'extensions/cli/' from commit 'e27a5c87d520e28844c9eac10bc6d301feee4b5e'
git-subtree-dir: extensions/cli
git-subtree-mainline: 1674c2aff1
git-subtree-split: e27a5c87d5
2025-08-20 17:19:56 -07:00

109 lines
2.9 KiB
TypeScript

import { EventEmitter } from "events";
import { logger } from "../util/logger.js";
/**
* Base abstract class for all services
* Provides common lifecycle methods and state management
*/
export abstract class BaseService<TState> extends EventEmitter {
protected serviceName: string;
protected currentState: TState;
private isInitialized: boolean = false;
constructor(serviceName: string, initialState: TState) {
super();
this.serviceName = serviceName;
this.currentState = initialState;
}
/**
* Initialize the service (must be implemented by subclasses)
* This is automatically wrapped with initialization logic
*/
abstract doInitialize(...args: any[]): Promise<TState>;
/**
* Public initialization method that wraps the implementation
*/
async initialize(...args: any[]): Promise<TState> {
logger.debug(`Initializing ${this.serviceName}`);
this.emit("initializing");
try {
const state = await this.doInitialize(...args);
this.currentState = state;
this.isInitialized = true;
logger.debug(`${this.serviceName} initialized successfully`);
this.emit("initialized", state);
return state;
} catch (error: any) {
logger.debug(`Failed to initialize ${this.serviceName}:`, error);
if (this.listenerCount("error") > 0) {
this.emit("error", error);
}
throw error;
}
}
/**
* Get current service state (shallow copy for immutability)
*/
getState(): TState {
return { ...this.currentState };
}
/**
* Update service state and emit change event
*/
protected setState(newState: Partial<TState>): void {
const previousState = this.currentState;
this.currentState = { ...this.currentState, ...newState };
// Only log state updates if not in production to avoid circular reference issues
if (process.env.NODE_ENV !== "production") {
logger.debug(`${this.serviceName} state updated`);
}
this.emit("stateChanged", this.currentState, previousState);
}
/**
* Check if service is ready for use
*/
isReady(): boolean {
return this.isInitialized;
}
/**
* Reload/refresh the service
*/
async reload(...args: any[]): Promise<TState> {
logger.debug(`Reloading ${this.serviceName}`);
this.isInitialized = false;
return this.initialize(...args);
}
/**
* Cleanup resources (can be overridden by subclasses)
*/
async cleanup(): Promise<void> {
logger.debug(`Cleaning up ${this.serviceName}`);
this.removeAllListeners();
this.isInitialized = false;
}
}
/**
* Interface for services with dependencies
*/
export interface ServiceWithDependencies {
getDependencies(): string[];
}
/**
* Helper to check if a service has dependencies
*/
export function hasDependencies(
service: any,
): service is ServiceWithDependencies {
return !!service && typeof service.getDependencies === "function";
}