| 1 | import { EventEmitter } from "events"; |
| 2 | |
| 3 | import { logger } from "../util/logger.js"; |
| 4 | |
| 5 | /** |
| 6 | * Base abstract class for all services |
| 7 | * Provides common lifecycle methods and state management |
| 8 | */ |
| 9 | export abstract class BaseService<TState> extends EventEmitter { |
| 10 | protected serviceName: string; |
| 11 | protected currentState: TState; |
| 12 | private isInitialized: boolean = false; |
| 13 | |
| 14 | constructor(serviceName: string, initialState: TState) { |
| 15 | super(); |
| 16 | this.serviceName = serviceName; |
| 17 | this.currentState = initialState; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Initialize the service (must be implemented by subclasses) |
| 22 | * This is automatically wrapped with initialization logic |
| 23 | */ |
| 24 | abstract doInitialize(...args: any[]): Promise<TState>; |
| 25 | |
| 26 | /** |
| 27 | * Public initialization method that wraps the implementation |
| 28 | */ |
| 29 | async initialize(...args: any[]): Promise<TState> { |
| 30 | logger.debug(`Initializing ${this.serviceName}`, { |
| 31 | wasInitialized: this.isInitialized, |
| 32 | hadPreviousState: !!this.currentState, |
| 33 | }); |
| 34 | this.emit("initializing"); |
| 35 | |
| 36 | try { |
| 37 | const state = await this.doInitialize(...args); |
| 38 | this.currentState = state; |
| 39 | this.isInitialized = true; |
| 40 | logger.debug(`${this.serviceName} initialized successfully`, { |
| 41 | stateKeys: state ? Object.keys(state as any) : [], |
| 42 | isNowInitialized: this.isInitialized, |
| 43 | }); |
| 44 | this.emit("initialized", state); |
| 45 | return state; |
| 46 | } catch (error: any) { |
| 47 | logger.debug(`Failed to initialize ${this.serviceName}:`, error); |
| 48 | if (this.listenerCount("error") > 0) { |
| 49 | this.emit("error", error); |
| 50 | } |
| 51 | throw error; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get current service state (shallow copy for immutability) |
| 57 | */ |
| 58 | getState(): TState { |
| 59 | return { ...this.currentState }; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Update service state and emit change event |
| 64 | */ |
| 65 | protected setState(newState: Partial<TState>): void { |
| 66 | const previousState = this.currentState; |
| 67 | this.currentState = { ...this.currentState, ...newState }; |
| 68 | // Only log state updates if not in production to avoid circular reference issues |
| 69 | if (process.env.NODE_ENV !== "production") { |
| 70 | logger.debug(`${this.serviceName} state updated`); |
| 71 | } |
| 72 | this.emit("stateChanged", this.currentState, previousState); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check if service is ready for use |
| 77 | */ |
| 78 | isReady(): boolean { |
| 79 | return this.isInitialized; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Reload/refresh the service |
| 84 | */ |
| 85 | async reload(...args: any[]): Promise<TState> { |
| 86 | logger.debug(`Reloading ${this.serviceName}`); |
| 87 | this.isInitialized = false; |
| 88 | return this.initialize(...args); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Cleanup resources (can be overridden by subclasses) |
| 93 | */ |
| 94 | async cleanup(): Promise<void> { |
| 95 | logger.debug(`Cleaning up ${this.serviceName}`); |
| 96 | this.removeAllListeners(); |
| 97 | this.isInitialized = false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Interface for services with dependencies |
| 103 | */ |
| 104 | export interface ServiceWithDependencies { |
| 105 | getDependencies(): string[]; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Helper to check if a service has dependencies |
| 110 | */ |
| 111 | export function hasDependencies( |
| 112 | service: any, |
| 113 | ): service is ServiceWithDependencies { |
| 114 | return !!service && typeof service.getDependencies === "function"; |
| 115 | } |