1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _standardAsCallback = require('standard-as-callback');
|
10 |
|
11 | var _standardAsCallback2 = _interopRequireDefault(_standardAsCallback);
|
12 |
|
13 | var _commands = require('./commands');
|
14 |
|
15 | var commands = _interopRequireWildcard(_commands);
|
16 |
|
17 | var _command = require('./command');
|
18 |
|
19 | var _promiseContainer = require('./promise-container');
|
20 |
|
21 | var _promiseContainer2 = _interopRequireDefault(_promiseContainer);
|
22 |
|
23 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
24 |
|
25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
26 |
|
27 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
28 |
|
29 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
30 |
|
31 | var Pipeline = function () {
|
32 | function Pipeline(redis) {
|
33 | var _this = this;
|
34 |
|
35 | _classCallCheck(this, Pipeline);
|
36 |
|
37 | this.batch = [];
|
38 | this.redis = redis;
|
39 | this._transactions = 0;
|
40 |
|
41 | Object.keys(commands).forEach(function (command) {
|
42 | _this[command] = _this._createCommand(command);
|
43 | });
|
44 | }
|
45 |
|
46 | _createClass(Pipeline, [{
|
47 | key: '_createCommand',
|
48 | value: function _createCommand(commandName) {
|
49 | var _this2 = this;
|
50 |
|
51 | return function () {
|
52 | for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
53 | args[_key] = arguments[_key];
|
54 | }
|
55 |
|
56 | var lastArgIndex = args.length - 1;
|
57 | var callback = args[lastArgIndex];
|
58 | if (typeof callback !== 'function') {
|
59 | callback = undefined;
|
60 | } else {
|
61 |
|
62 | args.length = lastArgIndex;
|
63 | }
|
64 | var commandEmulator = commands[commandName].bind(_this2.redis);
|
65 | var commandArgs = (0, _command.processArguments)(args, commandName, _this2.redis);
|
66 |
|
67 | _this2._addTransaction(commandEmulator, commandName, commandArgs, callback);
|
68 | return _this2;
|
69 | };
|
70 | }
|
71 | }, {
|
72 | key: '_addTransaction',
|
73 | value: function _addTransaction(commandEmulator, commandName, commandArgs, callback) {
|
74 | var _this3 = this;
|
75 |
|
76 | var Promise = _promiseContainer2.default.get();
|
77 | this.batch.push(function () {
|
78 | return (0, _standardAsCallback2.default)(new Promise(function (resolve) {
|
79 | return resolve(_command.safelyExecuteCommand.apply(undefined, [commandEmulator, commandName, _this3.redis].concat(_toConsumableArray(commandArgs))));
|
80 | }), callback);
|
81 | });
|
82 | this._transactions += 1;
|
83 | }
|
84 | }, {
|
85 | key: 'exec',
|
86 | value: function exec(callback) {
|
87 |
|
88 | var batch = this.batch;
|
89 | var Promise = _promiseContainer2.default.get();
|
90 |
|
91 | this.batch = [];
|
92 | return (0, _standardAsCallback2.default)(Promise.all(batch.map(function (cmd) {
|
93 | return cmd();
|
94 | })).then(function (replies) {
|
95 | return replies.map(function (reply) {
|
96 | return [null, reply];
|
97 | });
|
98 | }), callback);
|
99 | }
|
100 | }]);
|
101 |
|
102 | return Pipeline;
|
103 | }();
|
104 |
|
105 | exports.default = Pipeline; |
\ | No newline at end of file |