UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

119 lines 4.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentRegistry = void 0; class ComponentRegistry { constructor(eventBus) { this.factories = new Map(); this.components = new Map(); this.eventBus = eventBus; this.setupEventListeners(); } setupEventListeners() { this.eventBus.on('component:destroyed', (data) => { this.removeComponent(data.componentId); }); } registerFactory(factory) { if (this.factories.has(factory.type)) { throw new Error(`Component factory for type '${factory.type}' already registered`); } this.factories.set(factory.type, factory); } unregisterFactory(type) { this.factories.delete(type); } getRegisteredTypes() { return Array.from(this.factories.keys()); } hasFactory(type) { return this.factories.has(type); } createComponent(config) { const factory = this.factories.get(config.type); if (!factory) { throw new Error(`No factory registered for component type '${config.type}'`); } try { const component = factory.create(config, this.eventBus); this.components.set(component.getState().id, component); this.eventBus.emit('component:created', { componentId: component.getState().id, type: config.type, timestamp: new Date(), }); return component; } catch (error) { this.eventBus.emit('component:createError', { type: config.type, error: error instanceof Error ? error.message : 'Unknown error', timestamp: new Date(), }); throw error; } } getComponent(componentId) { return this.components.get(componentId); } getAllComponents() { return Array.from(this.components.values()); } getComponentsByType(type) { return this.getAllComponents().filter(component => component.getState().type === type); } getComponentStates() { return this.getAllComponents().map(component => component.getState()); } removeComponent(componentId) { const component = this.components.get(componentId); if (!component) { return false; } try { const componentType = component.getState().type; component.destroy(); this.components.delete(componentId); this.eventBus.emit('component:removed', { componentId, type: componentType, timestamp: new Date(), }); return true; } catch (error) { this.eventBus.emit('component:removeError', { componentId, error: error instanceof Error ? error.message : 'Unknown error', timestamp: new Date(), }); return false; } } removeAllComponents() { const componentIds = Array.from(this.components.keys()); for (const componentId of componentIds) { this.removeComponent(componentId); } } getComponentCount() { return this.components.size; } getHealthStatus() { const states = this.getComponentStates(); return { total: states.length, running: states.filter(s => s.status === 'running').length, error: states.filter(s => s.status === 'error').length, initializing: states.filter(s => s.status === 'initializing').length, paused: states.filter(s => s.status === 'paused').length, destroyed: states.filter(s => s.status === 'destroyed').length, }; } destroy() { this.removeAllComponents(); this.factories.clear(); this.eventBus.removeAllListeners(); } } exports.ComponentRegistry = ComponentRegistry; //# sourceMappingURL=component-registry.js.map