UNPKG

node-redisson

Version:

Distributed lock with Redis implementation for Node.js

42 lines (41 loc) 1.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PubSubCommandExecutor = void 0; const CommandExecutor_1 = require("./CommandExecutor"); const node_events_1 = __importDefault(require("node:events")); class PubSubCommandExecutor extends CommandExecutor_1.CommandExecutor { constructor(config) { super(config); this.eventEmitter = new node_events_1.default(); this.subscribeRedis.on('message', (channel, message) => { this.eventEmitter.emit(channel, message); }); } async subscribe(eventName, listener) { await this.subscribeRedis.subscribe(eventName); this.eventEmitter.on(eventName, listener); } async unsubscribe(eventName, listener) { await this.subscribeRedis.unsubscribe(eventName); this.eventEmitter.off(eventName, listener); } async subscribeOnce(eventName, listener) { await this.subscribeRedis.subscribe(eventName); this.eventEmitter.once(eventName, listener); } async publish(eventName, e) { await this.redis.publish(eventName, e); return null; } getRedisScripts() { const unlockCommand = `redis.call('publish', KEYS[2], ARGV[1]);`; return { rUnlockInner: CommandExecutor_1.DEFAULT_REDIS_SCRIPTS.rUnlockInner.lua.replace('#PUB_UNLOCK_REPLACE#', unlockCommand), rForceUnlock: CommandExecutor_1.DEFAULT_REDIS_SCRIPTS.rForceUnlock.lua.replace('#PUB_UNLOCK_REPLACE#', unlockCommand), }; } } exports.PubSubCommandExecutor = PubSubCommandExecutor;