UNPKG

gather-ts

Version:

A powerful code analysis and packaging tool designed for creating AI-friendly code representations for javascript and typescript projects.

176 lines 6.52 kB
"use strict"; // src/container/Container.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceTokens = exports.Container = void 0; const events_1 = require("events"); const errors_1 = require("@/errors"); class Container extends events_1.EventEmitter { constructor(debug = false) { super(); this.services = new Map(); this.isInitialized = false; this.debug = debug; } static getInstance(debug = false) { if (!Container.instance) { Container.instance = new Container(debug); } return Container.instance; } logDebug(message) { if (this.debug) { console.debug(`[Container] ${message}`); } } async initialize() { this.logDebug("Initializing Container"); try { if (this.isInitialized) { this.logDebug("Container already initialized"); return; } this.emit("initialization:start", { timestamp: Date.now() }); for (const [token, registration] of this.services.entries()) { const instance = registration.instance; if (instance && "initialize" in instance) { try { await instance.initialize(); this.logDebug(`Initialized service: ${token}`); } catch (error) { throw new Error(`Failed to initialize service ${token}: ${error instanceof Error ? error.message : String(error)}`); } } } this.isInitialized = true; this.emit("initialization:complete", { timestamp: Date.now() }); this.logDebug("Container initialization complete"); } catch (error) { this.handleError("initialization", error); } } cleanup() { this.logDebug("Cleaning up Container"); try { this.emit("cleanup:start", { timestamp: Date.now() }); for (const [token, registration] of this.services.entries()) { const instance = registration.instance; if (instance && "cleanup" in instance) { try { instance.cleanup(); this.logDebug(`Cleaned up service: ${token}`); } catch (error) { this.logDebug(`Error cleaning up service ${token}: ${error instanceof Error ? error.message : String(error)}`); } } } this.services.clear(); this.isInitialized = false; this.emit("cleanup:complete", { timestamp: Date.now() }); this.logDebug("Container cleanup complete"); } catch (error) { this.handleError("cleanup", error); } } handleError(operation, error) { const message = error instanceof Error ? error.message : String(error); throw new errors_1.ValidationError(`Container ${operation} failed: ${message}`); } register(token, instance) { this.logDebug(`Registering service: ${token}`); if (!token) { throw new errors_1.ValidationError("Service token cannot be empty"); } const tokenStr = token.toString(); if (this.services.has(tokenStr)) { throw new errors_1.ValidationError(`Service already registered: ${tokenStr}`); } this.services.set(tokenStr, { token, factory: () => instance, instance, }); this.emit("service:registered", { token: tokenStr, timestamp: Date.now(), }); } registerFactory(token, factory) { this.logDebug(`Registering factory: ${token}`); if (!token) { throw new errors_1.ValidationError("Service token cannot be empty"); } if (!factory || typeof factory !== "function") { throw new errors_1.ValidationError("Factory must be a function"); } const tokenStr = token.toString(); this.services.set(tokenStr, { token, factory, }); this.emit("service:registered", { token: tokenStr, timestamp: Date.now(), }); } resolve(token) { if (!this.isInitialized && !this.services.has(token.toString())) { throw new errors_1.ValidationError("Container not initialized"); } this.logDebug(`Resolving service: ${token}`); const tokenStr = token.toString(); const registration = this.services.get(tokenStr); if (!registration) { throw new errors_1.ValidationError(`No service registered for token: ${tokenStr}`); } try { if (!registration.instance) { const instance = registration.factory(); if (instance instanceof Promise) { throw new errors_1.ValidationError("Async service factories are not supported"); } registration.instance = instance; } this.emit("service:resolved", { token: tokenStr, timestamp: Date.now(), }); return registration.instance; } catch (error) { this.emit("service:error", { token: tokenStr, error: error instanceof Error ? error : new Error(String(error)), timestamp: Date.now(), }); throw new errors_1.ValidationError(`Failed to resolve service ${tokenStr}: ${error instanceof Error ? error.message : String(error)}`); } } hasService(token) { return this.services.has(token.toString()); } clear() { this.logDebug("Clearing all services"); this.cleanup(); } } exports.Container = Container; exports.ServiceTokens = { CONFIG_MANAGER: "ConfigManager", IGNORE_HANDLER: "IgnoreHandler", TOKEN_COUNTER: "TokenCounter", ERROR_HANDLER: "ErrorHandler", ERROR_UTILS: "ErrorUtils", FILE_SYSTEM: "FileSystem", DEPENDENCY_ANALYZER: "DependencyAnalyzer", LOGGER: "Logger", TOKEN_CACHE: "TokenCache", COMPILER: "Compiler", VALIDATOR: "Validator", ARGUMENT_PARSER: "ArgumentParser", TEMPLATE_MANAGER: "TemplateManager", }; //# sourceMappingURL=Container.js.map