redis-smq
Version:
A simple high-performance Redis message queue for Node.js.
51 lines • 1.75 kB
JavaScript
import { CallbackEmptyReplyError, EventBusRedis, EventEmitter, PanicError, } from 'redis-smq-common';
import { Configuration } from '../../config/index.js';
import { EventBusInstanceLockError } from './errors/index.js';
export class EventBus extends EventEmitter {
instance = null;
locked = false;
init = (cb) => {
this.getSetInstance((err) => cb(err));
};
getInstance() {
if (!this.instance)
return new 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 = Configuration.getSetConfig().redis;
EventBusRedis.createInstance(redisConfig, (err, inst) => {
this.locked = false;
if (err)
cb(err);
else if (!inst)
cb(new 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 EventBusInstanceLockError());
}
shutdown = (cb) => {
if (this.instance) {
this.instance.removeAllListeners();
this.instance.shutdown(() => {
this.instance = null;
cb();
});
}
else
cb();
};
}
//# sourceMappingURL=event-bus.js.map