redis-smq-common
Version:
RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.
59 lines • 2.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBusRedisFactory = void 0;
const index_js_1 = require("../errors/index.js");
const index_js_2 = require("../event/index.js");
const index_js_3 = require("./errors/index.js");
const event_bus_redis_js_1 = require("./event-bus-redis.js");
class EventBusRedisFactory extends index_js_2.EventEmitter {
constructor(config) {
super();
this.instance = null;
this.locked = false;
this.init = (cb) => {
this.getSetInstance((err) => cb(err));
};
this.shutdown = (cb) => {
if (this.instance) {
this.instance.removeAllListeners();
this.instance.shutdown(() => {
this.instance = null;
cb();
});
}
else
cb();
};
this.config = config;
}
getInstance() {
if (!this.instance)
return new index_js_1.PanicError(`Use first getSetInstance() to initialize the EventBusRedisInstance class`);
return this.instance;
}
getSetInstance(cb) {
if (!this.locked) {
if (!this.instance) {
this.locked = true;
event_bus_redis_js_1.EventBusRedis.createInstance(this.config, (err, inst) => {
this.locked = false;
if (err)
cb(err);
else if (!inst)
cb(new index_js_1.CallbackEmptyReplyError());
else {
this.instance = inst;
this.instance.on('error', (err) => this.emit('error', err));
cb(null, this.instance);
}
});
}
else
cb(null, this.instance);
}
else
cb(new index_js_3.EventBusInstanceLockError());
}
}
exports.EventBusRedisFactory = EventBusRedisFactory;
//# sourceMappingURL=event-bus-redis-factory.js.map