UNPKG

redis-smq-common

Version:

Provides essential components and utilities shared across RedisSMQ packages.

153 lines 5.78 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 index_js_3 = require("./errors/index.js"); const index_js_4 = require("./types/index.js"); const index_js_5 = 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) { this.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) throw new index_js_1.PanicError({ message: 'Use first init() to initialize the RedisClientInstance class', }); return this.instance; } createNodeRedisClient(config, cb) { Promise.resolve().then(() => __importStar(require('./clients/node-redis/node-redis-client.js'))).then(({ NodeRedisClient }) => { const client = new NodeRedisClient(config.options); cb(null, client); }) .catch(() => cb(new index_js_5.RedisClientNotInstalledError({ metadata: { clientId: '@redis/client' }, }))); } createIORedisClient(config, cb) { Promise.resolve().then(() => __importStar(require('./clients/ioredis/ioredis-client.js'))).then(({ IoredisClient }) => { const client = new IoredisClient(config.options); cb(null, client); }) .catch(() => cb(new index_js_5.RedisClientNotInstalledError({ metadata: { clientId: 'ioredis' }, }))); } initializeRedisClient(config, cb) { if (config.client === index_js_4.ERedisConfigClient.REDIS) { return this.createNodeRedisClient(config, cb); } if (config.client === index_js_4.ERedisConfigClient.IOREDIS) { return this.createIORedisClient(config, cb); } cb(new index_js_5.UnsupportedClientError()); } createRedisClient(config, cb) { this.initializeRedisClient(config, (err, client) => { if (err) return cb(err); if (!client) return cb(new index_js_1.CallbackEmptyReplyError()); const onReady = () => { removeListeners(); cb(null, client); }; const onError = (err) => { removeListeners(); cb(err); }; const removeListeners = () => { client.removeListener('ready', onReady); client.removeListener('error', onError); }; client.once('ready', onReady); client.once('error', onError); }); } } exports.RedisClientFactory = RedisClientFactory; //# sourceMappingURL=redis-client-factory.js.map