multi-command.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  3. if (kind === "m") throw new TypeError("Private method is not writable");
  4. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  5. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  6. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  7. };
  8. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  9. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  10. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  11. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  12. };
  13. var _RedisClientMultiCommand_instances, _RedisClientMultiCommand_multi, _RedisClientMultiCommand_executor, _RedisClientMultiCommand_selectedDB, _RedisClientMultiCommand_legacyMode, _RedisClientMultiCommand_defineLegacyCommand;
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. const commands_1 = require("./commands");
  16. const multi_command_1 = require("../multi-command");
  17. const commander_1 = require("../commander");
  18. class RedisClientMultiCommand {
  19. static extend(extensions) {
  20. return (0, commander_1.attachExtensions)({
  21. BaseClass: RedisClientMultiCommand,
  22. modulesExecutor: RedisClientMultiCommand.prototype.commandsExecutor,
  23. modules: extensions?.modules,
  24. functionsExecutor: RedisClientMultiCommand.prototype.functionsExecutor,
  25. functions: extensions?.functions,
  26. scriptsExecutor: RedisClientMultiCommand.prototype.scriptsExecutor,
  27. scripts: extensions?.scripts
  28. });
  29. }
  30. constructor(executor, legacyMode = false) {
  31. _RedisClientMultiCommand_instances.add(this);
  32. _RedisClientMultiCommand_multi.set(this, new multi_command_1.default());
  33. _RedisClientMultiCommand_executor.set(this, void 0);
  34. Object.defineProperty(this, "v4", {
  35. enumerable: true,
  36. configurable: true,
  37. writable: true,
  38. value: {}
  39. });
  40. _RedisClientMultiCommand_selectedDB.set(this, void 0);
  41. Object.defineProperty(this, "select", {
  42. enumerable: true,
  43. configurable: true,
  44. writable: true,
  45. value: this.SELECT
  46. });
  47. Object.defineProperty(this, "EXEC", {
  48. enumerable: true,
  49. configurable: true,
  50. writable: true,
  51. value: this.exec
  52. });
  53. __classPrivateFieldSet(this, _RedisClientMultiCommand_executor, executor, "f");
  54. if (legacyMode) {
  55. __classPrivateFieldGet(this, _RedisClientMultiCommand_instances, "m", _RedisClientMultiCommand_legacyMode).call(this);
  56. }
  57. }
  58. commandsExecutor(command, args) {
  59. return this.addCommand(command.transformArguments(...args), command.transformReply);
  60. }
  61. SELECT(db, transformReply) {
  62. __classPrivateFieldSet(this, _RedisClientMultiCommand_selectedDB, db, "f");
  63. return this.addCommand(['SELECT', db.toString()], transformReply);
  64. }
  65. addCommand(args, transformReply) {
  66. __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").addCommand(args, transformReply);
  67. return this;
  68. }
  69. functionsExecutor(fn, args, name) {
  70. __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").addFunction(name, fn, args);
  71. return this;
  72. }
  73. scriptsExecutor(script, args) {
  74. __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").addScript(script, args);
  75. return this;
  76. }
  77. async exec(execAsPipeline = false) {
  78. if (execAsPipeline) {
  79. return this.execAsPipeline();
  80. }
  81. return __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").handleExecReplies(await __classPrivateFieldGet(this, _RedisClientMultiCommand_executor, "f").call(this, __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").queue, __classPrivateFieldGet(this, _RedisClientMultiCommand_selectedDB, "f"), multi_command_1.default.generateChainId()));
  82. }
  83. async execAsPipeline() {
  84. if (__classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").queue.length === 0)
  85. return [];
  86. return __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").transformReplies(await __classPrivateFieldGet(this, _RedisClientMultiCommand_executor, "f").call(this, __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").queue, __classPrivateFieldGet(this, _RedisClientMultiCommand_selectedDB, "f")));
  87. }
  88. }
  89. _RedisClientMultiCommand_multi = new WeakMap(), _RedisClientMultiCommand_executor = new WeakMap(), _RedisClientMultiCommand_selectedDB = new WeakMap(), _RedisClientMultiCommand_instances = new WeakSet(), _RedisClientMultiCommand_legacyMode = function _RedisClientMultiCommand_legacyMode() {
  90. var _a, _b;
  91. this.v4.addCommand = this.addCommand.bind(this);
  92. this.addCommand = (...args) => {
  93. __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").addCommand((0, commander_1.transformLegacyCommandArguments)(args));
  94. return this;
  95. };
  96. this.v4.exec = this.exec.bind(this);
  97. this.exec = (callback) => {
  98. this.v4.exec()
  99. .then((reply) => {
  100. if (!callback)
  101. return;
  102. callback(null, reply);
  103. })
  104. .catch((err) => {
  105. if (!callback) {
  106. // this.emit('error', err);
  107. return;
  108. }
  109. callback(err);
  110. });
  111. };
  112. for (const [name, command] of Object.entries(commands_1.default)) {
  113. __classPrivateFieldGet(this, _RedisClientMultiCommand_instances, "m", _RedisClientMultiCommand_defineLegacyCommand).call(this, name, command);
  114. (_a = this)[_b = name.toLowerCase()] ?? (_a[_b] = this[name]);
  115. }
  116. }, _RedisClientMultiCommand_defineLegacyCommand = function _RedisClientMultiCommand_defineLegacyCommand(name, command) {
  117. this.v4[name] = this[name].bind(this.v4);
  118. this[name] = command && command.TRANSFORM_LEGACY_REPLY && command.transformReply ?
  119. (...args) => {
  120. __classPrivateFieldGet(this, _RedisClientMultiCommand_multi, "f").addCommand([name, ...(0, commander_1.transformLegacyCommandArguments)(args)], command.transformReply);
  121. return this;
  122. } :
  123. (...args) => this.addCommand(name, ...args);
  124. };
  125. exports.default = RedisClientMultiCommand;
  126. (0, commander_1.attachCommands)({
  127. BaseClass: RedisClientMultiCommand,
  128. commands: commands_1.default,
  129. executor: RedisClientMultiCommand.prototype.commandsExecutor
  130. });