/node_modules/nohm/node_modules/redis/lib/multi.js

https://gitlab.com/Ahleph1/sgf-to-pdf · JavaScript · 186 lines · 160 code · 13 blank · 13 comment · 35 complexity · 386f0436478ce9db96e43ad510c14d1d MD5 · raw file

  1. 'use strict';
  2. var Queue = require('double-ended-queue');
  3. var utils = require('./utils');
  4. var Command = require('./command');
  5. function Multi (client, args) {
  6. this._client = client;
  7. this.queue = new Queue();
  8. var command, tmp_args;
  9. if (args) { // Either undefined or an array. Fail hard if it's not an array
  10. for (var i = 0; i < args.length; i++) {
  11. command = args[i][0];
  12. tmp_args = args[i].slice(1);
  13. if (Array.isArray(command)) {
  14. this[command[0]].apply(this, command.slice(1).concat(tmp_args));
  15. } else {
  16. this[command].apply(this, tmp_args);
  17. }
  18. }
  19. }
  20. }
  21. function pipeline_transaction_command (self, command_obj, index) {
  22. // Queueing is done first, then the commands are executed
  23. var tmp = command_obj.callback;
  24. command_obj.callback = function (err, reply) {
  25. // Ignore the multi command. This is applied by node_redis and the user does not benefit by it
  26. if (err && index !== -1) {
  27. if (tmp) {
  28. tmp(err);
  29. }
  30. err.position = index;
  31. self.errors.push(err);
  32. }
  33. // Keep track of who wants buffer responses:
  34. // By the time the callback is called the command_obj got the buffer_args attribute attached
  35. self.wants_buffers[index] = command_obj.buffer_args;
  36. };
  37. self._client.internal_send_command(command_obj);
  38. }
  39. Multi.prototype.exec_atomic = Multi.prototype.EXEC_ATOMIC = Multi.prototype.execAtomic = function exec_atomic (callback) {
  40. if (this.queue.length < 2) {
  41. return this.exec_batch(callback);
  42. }
  43. return this.exec(callback);
  44. };
  45. function multi_callback (self, err, replies) {
  46. var i = 0, command_obj;
  47. if (err) {
  48. err.errors = self.errors;
  49. if (self.callback) {
  50. self.callback(err);
  51. // Exclude connection errors so that those errors won't be emitted twice
  52. } else if (err.code !== 'CONNECTION_BROKEN') {
  53. self._client.emit('error', err);
  54. }
  55. return;
  56. }
  57. if (replies) {
  58. while (command_obj = self.queue.shift()) {
  59. if (replies[i] instanceof Error) {
  60. var match = replies[i].message.match(utils.err_code);
  61. // LUA script could return user errors that don't behave like all other errors!
  62. if (match) {
  63. replies[i].code = match[1];
  64. }
  65. replies[i].command = command_obj.command.toUpperCase();
  66. if (typeof command_obj.callback === 'function') {
  67. command_obj.callback(replies[i]);
  68. }
  69. } else {
  70. // If we asked for strings, even in detect_buffers mode, then return strings:
  71. replies[i] = self._client.handle_reply(replies[i], command_obj.command, self.wants_buffers[i]);
  72. if (typeof command_obj.callback === 'function') {
  73. command_obj.callback(null, replies[i]);
  74. }
  75. }
  76. i++;
  77. }
  78. }
  79. if (self.callback) {
  80. self.callback(null, replies);
  81. }
  82. }
  83. Multi.prototype.exec_transaction = function exec_transaction (callback) {
  84. if (this.monitoring || this._client.monitoring) {
  85. var err = new RangeError(
  86. 'Using transaction with a client that is in monitor mode does not work due to faulty return values of Redis.'
  87. );
  88. err.command = 'EXEC';
  89. err.code = 'EXECABORT';
  90. return utils.reply_in_order(this._client, callback, err);
  91. }
  92. var self = this;
  93. var len = self.queue.length;
  94. self.errors = [];
  95. self.callback = callback;
  96. self._client.cork();
  97. self.wants_buffers = new Array(len);
  98. pipeline_transaction_command(self, new Command('multi', []), -1);
  99. // Drain queue, callback will catch 'QUEUED' or error
  100. for (var index = 0; index < len; index++) {
  101. // The commands may not be shifted off, since they are needed in the result handler
  102. pipeline_transaction_command(self, self.queue.get(index), index);
  103. }
  104. self._client.internal_send_command(new Command('exec', [], function (err, replies) {
  105. multi_callback(self, err, replies);
  106. }));
  107. self._client.uncork();
  108. return !self._client.should_buffer;
  109. };
  110. function batch_callback (self, cb, i) {
  111. return function batch_callback (err, res) {
  112. if (err) {
  113. self.results[i] = err;
  114. // Add the position to the error
  115. self.results[i].position = i;
  116. } else {
  117. self.results[i] = res;
  118. }
  119. cb(err, res);
  120. };
  121. }
  122. Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = function exec_batch (callback) {
  123. var self = this;
  124. var len = self.queue.length;
  125. var index = 0;
  126. var command_obj;
  127. if (len === 0) {
  128. utils.reply_in_order(self._client, callback, null, []);
  129. return !self._client.should_buffer;
  130. }
  131. self._client.cork();
  132. if (!callback) {
  133. while (command_obj = self.queue.shift()) {
  134. self._client.internal_send_command(command_obj);
  135. }
  136. self._client.uncork();
  137. return !self._client.should_buffer;
  138. }
  139. var callback_without_own_cb = function (err, res) {
  140. if (err) {
  141. self.results.push(err);
  142. // Add the position to the error
  143. var i = self.results.length - 1;
  144. self.results[i].position = i;
  145. } else {
  146. self.results.push(res);
  147. }
  148. // Do not emit an error here. Otherwise each error would result in one emit.
  149. // The errors will be returned in the result anyway
  150. };
  151. var last_callback = function (cb) {
  152. return function (err, res) {
  153. cb(err, res);
  154. callback(null, self.results);
  155. };
  156. };
  157. self.results = [];
  158. while (command_obj = self.queue.shift()) {
  159. if (typeof command_obj.callback === 'function') {
  160. command_obj.callback = batch_callback(self, command_obj.callback, index);
  161. } else {
  162. command_obj.callback = callback_without_own_cb;
  163. }
  164. if (typeof callback === 'function' && index === len - 1) {
  165. command_obj.callback = last_callback(command_obj.callback);
  166. }
  167. this._client.internal_send_command(command_obj);
  168. index++;
  169. }
  170. self._client.uncork();
  171. return !self._client.should_buffer;
  172. };
  173. module.exports = Multi;