UNPKG

redis-smq

Version:

A simple high-performance Redis message queue for Node.js.

68 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisClient = void 0; const redis_smq_common_1 = require("redis-smq-common"); const index_js_1 = require("../../config/index.js"); const redis_client_instance_lock_error_js_1 = require("./errors/redis-client-instance-lock.error.js"); const scripts_js_1 = require("./scripts/scripts.js"); class RedisClient extends redis_smq_common_1.EventEmitter { constructor() { super(...arguments); 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(index_js_1.Configuration.getSetConfig().redis, (err, client) => { this.locked = false; if (err) return cb(err); if (!client) return cb(new redis_smq_common_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 redis_client_instance_lock_error_js_1.RedisClientInstanceLockError()); }; this.shutdown = (cb) => { if (this.instance) { this.instance.halt(() => { this.instance = null; cb(); }); } else cb(); }; } createClient(config, cb) { (0, redis_smq_common_1.createRedisClient)(config, (err, client) => { if (err) return cb(err); if (!client) return cb(new redis_smq_common_1.CallbackEmptyReplyError()); (0, scripts_js_1.loadScriptFiles)(client, (err) => { if (err) return cb(err); cb(null, client); }); }); } getInstance() { if (!this.instance) return new redis_smq_common_1.PanicError(`Use first getSetInstance() to initialize the RedisClientInstance class`); return this.instance; } } exports.RedisClient = RedisClient; //# sourceMappingURL=redis-client.js.map