@amirmarmul/waba-common
Version:

38 lines (37 loc) • 1.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelEvent = void 0;
const Connection_1 = require("./Connection");
const logger_1 = __importDefault(require("../../../core/utils/logger"));
class ChannelEvent {
static channels = {
default: null,
publisher: null
};
constructor() {
//
}
static createChannel(channelType = 'default') {
const connection = channelType === 'default'
? Connection_1.Connection.getConnection('default')
: Connection_1.Connection.getConnection('publisher');
const channel = connection.createChannel({ json: true });
channel.on('error', (err) => {
logger_1.default.error('AMQP channel error:', { msg: err.message, stack: err.stack });
this.channels[channelType] = null;
});
channel.on('close', () => {
logger_1.default.error('AMQP channel error:', { msg: 'closed' });
this.channels[channelType] = null;
});
this.channels[channelType] = channel;
return channel;
}
static getChannel(channelType = 'default') {
return this.channels[channelType] ?? this.createChannel(channelType);
}
}
exports.ChannelEvent = ChannelEvent;