Elffuss BraidHub

kikocis/continue public

438b2c9c3ac0 · 2.9 KiBRawPermalink
1import { EventEmitter } from "events";
2
3import { logger } from "../util/logger.js";
4
5/**
6 * Base abstract class for all services
7 * Provides common lifecycle methods and state management
8 */
9export 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 this.emit("initializing");
32
33 try {
34 const state = await this.doInitialize(...args);
35 this.currentState = state;
36 this.isInitialized = true;
37 logger.debug(`${this.serviceName} initialized successfully`);
38 this.emit("initialized", state);
39 return state;
40 } catch (error: any) {
41 logger.debug(`Failed to initialize ${this.serviceName}:`, error);
42 if (this.listenerCount("error") > 0) {
43 this.emit("error", error);
44 }
45 throw error;
46 }
47 }
48
49 /**
50 * Get current service state (shallow copy for immutability)
51 */
52 getState(): TState {
53 return { ...this.currentState };
54 }
55
56 /**
57 * Update service state and emit change event
58 */
59 protected setState(newState: Partial<TState>): void {
60 const previousState = this.currentState;
61 this.currentState = { ...this.currentState, ...newState };
62 // Only log state updates if not in production to avoid circular reference issues
63 if (process.env.NODE_ENV !== "production") {
64 logger.debug(`${this.serviceName} state updated`);
65 }
66 this.emit("stateChanged", this.currentState, previousState);
67 }
68
69 /**
70 * Check if service is ready for use
71 */
72 isReady(): boolean {
73 return this.isInitialized;
74 }
75
76 /**
77 * Reload/refresh the service
78 */
79 async reload(...args: any[]): Promise<TState> {
80 logger.debug(`Reloading ${this.serviceName}`);
81 this.isInitialized = false;
82 return this.initialize(...args);
83 }
84
85 /**
86 * Cleanup resources (can be overridden by subclasses)
87 */
88 async cleanup(): Promise<void> {
89 logger.debug(`Cleaning up ${this.serviceName}`);
90 this.removeAllListeners();
91 this.isInitialized = false;
92 }
93}
94
95/**
96 * Interface for services with dependencies
97 */
98export interface ServiceWithDependencies {
99 getDependencies(): string[];
100}
101
102/**
103 * Helper to check if a service has dependencies
104 */
105export function hasDependencies(
106 service: any,
107): service is ServiceWithDependencies {
108 return !!service && typeof service.getDependencies === "function";
109}