redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
58 lines • 2.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBus = void 0;
const redis_smq_common_1 = require("redis-smq-common");
const index_js_1 = require("../../config/index.js");
const index_js_2 = require("./errors/index.js");
class EventBus extends redis_smq_common_1.EventEmitter {
constructor() {
super(...arguments);
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();
};
}
getInstance() {
if (!this.instance)
return new redis_smq_common_1.PanicError(`Use first getSetInstance() to initialize the EventBusRedisInstance class`);
return this.instance;
}
getSetInstance(cb) {
if (!this.locked) {
if (!this.instance) {
this.locked = true;
const redisConfig = index_js_1.Configuration.getSetConfig().redis;
redis_smq_common_1.EventBusRedis.createInstance(redisConfig, (err, inst) => {
this.locked = false;
if (err)
cb(err);
else if (!inst)
cb(new redis_smq_common_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_2.EventBusInstanceLockError());
}
}
exports.EventBus = EventBus;
//# sourceMappingURL=event-bus.js.map
;