redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
137 lines • 4.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBusRedis = void 0;
const index_js_1 = require("../redis-client/index.js");
const index_js_2 = require("./errors/index.js");
const event_bus_js_1 = require("./event-bus.js");
class EventBusRedis extends event_bus_js_1.EventBus {
constructor(config, namespace = '') {
super(config, namespace);
this.subscribedEvents = new Set();
this.pubClient = new index_js_1.RedisClientFactory(config.redis);
this.pubClient.on('error', (err) => this.handleError(err));
this.subClient = new index_js_1.RedisClientFactory(config.redis);
this.subClient.on('error', (err) => this.handleError(err));
}
emit(event, ...args) {
if (event === 'error') {
return super.emit(event, ...args);
}
if (!this.isOperational()) {
this.eventEmitter.emit('error', new index_js_2.EventBusNotConnectedError());
return false;
}
const namespacedEvent = this.toNamespacedEvent(String(event));
this.pubClient
.getInstance()
.publish(namespacedEvent, JSON.stringify(args), () => void 0);
return true;
}
on(event, listener) {
super.on(event, listener);
const namespacedEvent = this.toNamespacedEvent(String(event));
this.ensureSubscribed(namespacedEvent);
return this;
}
once(event, listener) {
super.once(event, listener);
const namespacedEvent = this.toNamespacedEvent(String(event));
this.ensureSubscribed(namespacedEvent);
return this;
}
removeListener(event, listener) {
super.removeListener(event, listener);
if (event !== 'error' &&
this.eventEmitter.listenerCount(String(event)) === 0) {
const namespacedEvent = this.toNamespacedEvent(String(event));
this.ensureUnsubscribed(namespacedEvent);
}
return this;
}
removeAllListeners(event) {
super.removeAllListeners(event);
if (event && event !== 'error') {
const namespacedEvent = this.toNamespacedEvent(String(event));
this.ensureUnsubscribed(namespacedEvent);
}
else if (!event) {
this.ensureUnsubscribed();
}
return this;
}
ensureSubscribed(eventName) {
if (eventName !== 'error' &&
this.isOperational() &&
!this.subscribedEvents.has(eventName)) {
this.subClient.getInstance().subscribe(eventName);
this.subscribedEvents.add(eventName);
}
}
ensureUnsubscribed(eventName) {
if (eventName === 'error')
return;
if (!eventName) {
if (this.subscribedEvents.size > 0) {
this.subClient.getInstance().unsubscribe();
this.subscribedEvents.clear();
}
return;
}
if (this.subscribedEvents.has(eventName)) {
this.subClient.getInstance().unsubscribe(eventName);
this.subscribedEvents.delete(eventName);
}
}
syncSubscriptionsWithListeners() {
if (!this.isOperational())
return;
for (const name of this.getListenerEventNames()) {
this.ensureSubscribed(name);
}
}
getListenerEventNames() {
return this.eventEmitter
.eventNames()
.map((n) => String(n))
.filter((n) => n !== 'error');
}
goingUp() {
return super.goingUp().concat([
(cb) => this.pubClient.init(cb),
(cb) => {
this.subClient.init((err) => {
if (err)
return cb(err);
this.subClient
.getInstance()
.on('message', (channel, message) => {
try {
this.eventEmitter.emit(channel, ...JSON.parse(message));
}
catch (error) {
this.handleError(new index_js_2.EventBusMessageJSONParseError({
metadata: {
error: String(error),
},
}));
}
});
this.syncSubscriptionsWithListeners();
cb();
});
},
]);
}
goingDown() {
return [
(cb) => {
this.ensureUnsubscribed();
cb();
},
(cb) => this.subClient.shutdown(cb),
(cb) => this.pubClient.shutdown(cb),
].concat(super.goingDown());
}
}
exports.EventBusRedis = EventBusRedis;
//# sourceMappingURL=event-bus-redis.js.map