UNPKG

@gabliam/amqp

Version:
45 lines (44 loc) 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AmqpConnectionManager = void 0; const amqp_connection_1 = require("./amqp-connection"); const errors_1 = require("./errors"); const queue_1 = require("./queue"); class AmqpConnectionManager { constructor(connectionConfigs, valueExtractor) { this.connections = []; for (const [index, { name, url, queues, undefinedValue, gzipEnabled },] of connectionConfigs.entries()) { if (this.connections.find((c) => c.name === name) !== undefined) { throw new errors_1.AmqpDuplicateConnectionError(name); } this.connections.push(new amqp_connection_1.AmqpConnection(index, name, url, undefinedValue, this.createQueue(queues), valueExtractor, gzipEnabled)); } } async start() { await Promise.all(this.connections.map((c) => c.start())); } async stop() { await Promise.all(this.connections.map((c) => c.stop())); } getConnection(name) { const connection = this.connections.find((c) => c.name === name); if (!connection) { throw new errors_1.AmqpConnectionNotFoundError(name); } return connection; } getDefaultConnection() { const connection = this.connections.find((c) => c.name === 'default'); if (connection === undefined) { return this.connections[0]; } return connection; } createQueue(queueConfig = {}) { return Object.keys(queueConfig).map((k) => { const q = queueConfig[k]; return new queue_1.Queue(q.queueName, q.options); }); } } exports.AmqpConnectionManager = AmqpConnectionManager;