redis-smq-common
Version:
RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.
65 lines • 2.04 kB
JavaScript
import { CallbackEmptyReplyError, PanicError } from '../errors/index.js';
import { EventEmitter } from '../event/index.js';
import { createRedisClient } from './create-redis-client.js';
import { InstanceLockError } from './errors/index.js';
export class RedisClientFactory extends EventEmitter {
instance = null;
locked = false;
config;
constructor(config) {
super();
this.config = config;
}
createClient(config, cb) {
createRedisClient(config, (err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new CallbackEmptyReplyError());
this.setupClient(client, cb);
});
}
setupClient(client, cb) {
cb(null, client);
}
init = (cb) => {
this.getSetInstance((err) => cb(err));
};
getSetInstance = (cb) => {
if (!this.locked) {
if (!this.instance) {
this.locked = true;
this.createClient(this.config, (err, client) => {
this.locked = false;
if (err)
return cb(err);
if (!client)
return cb(new CallbackEmptyReplyError());
this.instance = client;
this.instance.on('error', (err) => this.emit('error', err));
cb(null, this.instance);
});
}
else
cb(null, this.instance);
}
else
cb(new InstanceLockError());
};
shutdown = (cb) => {
if (this.instance) {
this.instance.halt(() => {
this.instance = null;
cb();
});
}
else
cb();
};
getInstance() {
if (!this.instance)
return new PanicError(`Use first getSetInstance() to initialize the RedisClientInstance class`);
return this.instance;
}
}
//# sourceMappingURL=redis-client-factory.js.map