UNPKG

@sailboat-computer/event-bus

Version:

Standardized event bus for sailboat computer v3 with resilience features and offline capabilities

160 lines 5.02 kB
"use strict"; /** * Base adapter for event bus implementations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseAdapter = void 0; const uuid_1 = require("uuid"); const errors_1 = require("../errors"); const utils_1 = require("../utils"); /** * Base adapter implementation */ class BaseAdapter { /** * Create a new base adapter */ constructor() { /** * Whether the adapter is initialized */ this.initialized = false; /** * Whether the adapter is connected */ this.connected = false; /** * Subscriptions by event type */ this.subscriptions = new Map(); // Configuration will be set in initialize() this.config = { serviceName: '' }; } /** * Initialize the adapter * * @param config - Adapter configuration */ async initialize(config) { if (this.initialized) { throw new Error('Adapter already initialized'); } this.validateConfig(config); this.config = { ...config }; this.initialized = true; utils_1.logger.info(`Initialized ${this.constructor.name} for service ${this.config.serviceName}`); } /** * Shutdown the adapter */ async shutdown() { if (!this.initialized) { return; } this.initialized = false; this.connected = false; this.subscriptions.clear(); utils_1.logger.info(`Shut down ${this.constructor.name} for service ${this.config.serviceName}`); } /** * Subscribe to an event * * @param eventType - Event type * @param handler - Event handler * @returns Subscription */ async subscribe(eventType, handler) { this.ensureInitialized(); // Create subscription ID const subscriptionId = (0, uuid_1.v4)(); // Get or create subscriptions map for this event type if (!this.subscriptions.has(eventType)) { this.subscriptions.set(eventType, new Map()); } // Add handler to subscriptions this.subscriptions.get(eventType).set(subscriptionId, handler); utils_1.logger.debug(`Subscribed to ${eventType} with ID ${subscriptionId}`); // Return subscription object return { id: subscriptionId, eventType, unsubscribe: async () => { await this.unsubscribeById(subscriptionId, eventType); } }; } /** * Unsubscribe from a specific subscription * * @param subscriptionId - Subscription ID * @param eventType - Event type */ async unsubscribeById(subscriptionId, eventType) { // Get subscriptions map for this event type const eventSubscriptions = this.subscriptions.get(eventType); if (!eventSubscriptions) { return; } // Remove handler from subscriptions eventSubscriptions.delete(subscriptionId); // Remove event type if no more subscriptions if (eventSubscriptions.size === 0) { this.subscriptions.delete(eventType); } utils_1.logger.debug(`Unsubscribed from ${eventType} with ID ${subscriptionId}`); } /** * Unsubscribe from all handlers for an event type * * @param eventType - Event type to unsubscribe from */ async unsubscribe(eventType) { this.ensureInitialized(); // Get subscriptions map for this event type const eventSubscriptions = this.subscriptions.get(eventType); if (!eventSubscriptions) { return; } // Get all subscription IDs for this event type const subscriptionIds = Array.from(eventSubscriptions.keys()); // Unsubscribe from each subscription for (const subscriptionId of subscriptionIds) { await this.unsubscribeById(subscriptionId, eventType); } utils_1.logger.debug(`Unsubscribed from all handlers for event type ${eventType}`); } /** * Check if the adapter is connected * * @returns True if connected */ isConnected() { return this.connected; } /** * Ensure the adapter is initialized * * @throws NotInitializedError if not initialized */ ensureInitialized() { if (!this.initialized) { throw new errors_1.NotInitializedError(); } } /** * Validate adapter configuration * * @param config - Adapter configuration * @throws ConfigurationError if configuration is invalid */ validateConfig(config) { if (!config) { throw new errors_1.ConfigurationError('Adapter configuration is required'); } if (!config.serviceName) { throw new errors_1.ConfigurationError('Service name is required'); } } } exports.BaseAdapter = BaseAdapter; //# sourceMappingURL=base-adapter.js.map