redis-smq-common
Version:
RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.
68 lines • 2.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisClientFactory = void 0;
const index_js_1 = require("../errors/index.js");
const index_js_2 = require("../event/index.js");
const create_redis_client_js_1 = require("./create-redis-client.js");
const index_js_3 = require("./errors/index.js");
class RedisClientFactory extends index_js_2.EventEmitter {
constructor(config) {
super();
this.instance = null;
this.locked = false;
this.init = (cb) => {
this.getSetInstance((err) => cb(err));
};
this.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 index_js_1.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 index_js_3.InstanceLockError());
};
this.shutdown = (cb) => {
if (this.instance) {
this.instance.halt(() => {
this.instance = null;
cb();
});
}
else
cb();
};
this.config = config;
}
createClient(config, cb) {
(0, create_redis_client_js_1.createRedisClient)(config, (err, client) => {
if (err)
return cb(err);
if (!client)
return cb(new index_js_1.CallbackEmptyReplyError());
this.setupClient(client, cb);
});
}
setupClient(client, cb) {
cb(null, client);
}
getInstance() {
if (!this.instance)
return new index_js_1.PanicError(`Use first getSetInstance() to initialize the RedisClientInstance class`);
return this.instance;
}
}
exports.RedisClientFactory = RedisClientFactory;
//# sourceMappingURL=redis-client-factory.js.map