Skip to content

Commit

Permalink
Fix the order of received commands. Close #91
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 7, 2015
1 parent 40419a8 commit 8eee7af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Master Branch

* Fix the order of received commands([#91](https://github.com/luin/ioredis/issues/91)).

### v1.5.11 - July 7, 2015

* Allow omitting callback in `exec`.
Expand Down
37 changes: 18 additions & 19 deletions lib/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Pipeline.prototype.sendCommand = function (command) {
var position = this._queue.length;

var _this = this;

command.promise.then(function (result) {
_this.fillResult([null, result], position);
}).catch(function (error) {
Expand Down Expand Up @@ -219,27 +220,25 @@ Pipeline.prototype.exec = function (callback) {
pipelineSlot = Math.random() * 16384 | 0;
}

var promise;
var _this = this;
if (scripts.length) {
promise = this.redis.script('exists', scripts.map(function (item) {
return item.sha;
})).then(function (results) {
var pending = [];
for (var i = 0; i < results.length; ++i) {
if (!results[i]) {
pending.push(scripts[i]);
}
}
return Promise.all(pending.map(function (script) {
return _this.redis.script('load', script.lua);
}));
});
} else {
promise = Promise.resolve();
if (!scripts.length) {
return execPipeline();
}
return this.redis.script('exists', scripts.map(function (item) {
return item.sha;
})).then(function (results) {
var pending = [];
for (var i = 0; i < results.length; ++i) {
if (!results[i]) {
pending.push(scripts[i]);
}
}
return Promise.all(pending.map(function (script) {
return _this.redis.script('load', script.lua);
}));
}).then(execPipeline);

return promise.then(function () {
function execPipeline () {
var data = '';
var writePending = _this.replyPending = _this._queue.length;

Expand Down Expand Up @@ -282,7 +281,7 @@ Pipeline.prototype.exec = function (callback) {
_this.redis.sendCommand(_this._queue[i], stream, node);
}
return _this.promise;
});
}
};

module.exports = Pipeline;

0 comments on commit 8eee7af

Please # to comment.