multi-command.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const commander_1 = require("./commander");
  4. const errors_1 = require("./errors");
  5. class RedisMultiCommand {
  6. constructor() {
  7. Object.defineProperty(this, "queue", {
  8. enumerable: true,
  9. configurable: true,
  10. writable: true,
  11. value: []
  12. });
  13. Object.defineProperty(this, "scriptsInUse", {
  14. enumerable: true,
  15. configurable: true,
  16. writable: true,
  17. value: new Set()
  18. });
  19. }
  20. static generateChainId() {
  21. return Symbol('RedisMultiCommand Chain Id');
  22. }
  23. addCommand(args, transformReply) {
  24. this.queue.push({
  25. args,
  26. transformReply
  27. });
  28. }
  29. addFunction(name, fn, args) {
  30. const transformedArguments = (0, commander_1.fCallArguments)(name, fn, fn.transformArguments(...args));
  31. this.queue.push({
  32. args: transformedArguments,
  33. transformReply: fn.transformReply
  34. });
  35. return transformedArguments;
  36. }
  37. addScript(script, args) {
  38. const transformedArguments = [];
  39. if (this.scriptsInUse.has(script.SHA1)) {
  40. transformedArguments.push('EVALSHA', script.SHA1);
  41. }
  42. else {
  43. this.scriptsInUse.add(script.SHA1);
  44. transformedArguments.push('EVAL', script.SCRIPT);
  45. }
  46. if (script.NUMBER_OF_KEYS !== undefined) {
  47. transformedArguments.push(script.NUMBER_OF_KEYS.toString());
  48. }
  49. const scriptArguments = script.transformArguments(...args);
  50. transformedArguments.push(...scriptArguments);
  51. if (scriptArguments.preserve) {
  52. transformedArguments.preserve = scriptArguments.preserve;
  53. }
  54. this.addCommand(transformedArguments, script.transformReply);
  55. return transformedArguments;
  56. }
  57. handleExecReplies(rawReplies) {
  58. const execReply = rawReplies[rawReplies.length - 1];
  59. if (execReply === null) {
  60. throw new errors_1.WatchError();
  61. }
  62. return this.transformReplies(execReply);
  63. }
  64. transformReplies(rawReplies) {
  65. const errorIndexes = [], replies = rawReplies.map((reply, i) => {
  66. if (reply instanceof errors_1.ErrorReply) {
  67. errorIndexes.push(i);
  68. return reply;
  69. }
  70. const { transformReply, args } = this.queue[i];
  71. return transformReply ? transformReply(reply, args.preserve) : reply;
  72. });
  73. if (errorIndexes.length)
  74. throw new errors_1.MultiErrorReply(replies, errorIndexes);
  75. return replies;
  76. }
  77. }
  78. exports.default = RedisMultiCommand;