UNPKG

redis-smq-common

Version:

RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.

56 lines 1.82 kB
import { CallbackEmptyReplyError, PanicError } from '../errors/index.js'; import { EventEmitter } from '../event/index.js'; import { EventBusInstanceLockError } from './errors/index.js'; import { EventBusRedis } from './event-bus-redis.js'; export class EventBusRedisFactory extends EventEmitter { instance = null; locked = false; config; constructor(config) { super(); this.config = config; } 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; EventBusRedis.createInstance(this.config, (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-redis-factory.js.map