Skip to content

Commit

Permalink
feat(pipeline): add #length to get the command count
Browse files Browse the repository at this point in the history
Closes #461
  • Loading branch information
luin committed May 18, 2017
1 parent a389f3c commit a6060cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ redis.pipeline([
]).exec(function () { /* ... */ });
```

`#length` property shows how many commands in the pipeline:

```javascript
const length = redis.pipeline().set('foo', 'bar').get('foo').length;
// length === 2
```


## Transaction
Most of the time, the transaction commands `multi` & `exec` are used together with pipeline.
Expand Down
6 changes: 6 additions & 0 deletions lib/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ function Pipeline(redis) {
_this.resolve = resolve;
_this.reject = reject;
});

Object.defineProperty(this, 'length', {
get: function () {
return _this._queue.length;
}
});
}

_.assign(Pipeline.prototype, Commander.prototype);
Expand Down
17 changes: 16 additions & 1 deletion test/functional/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('pipeline', function () {
});

it('should work', function (done) {
redis.pipeline().echo('foo', 'bar', '123', 'abc').exec(function(err, results) {
redis.pipeline().echo('foo', 'bar', '123', 'abc').exec(function (err, results) {
expect(err).to.eql(null);
expect(results).to.eql([
[null, ['foo', 'bar', '123', 'abc']]
Expand Down Expand Up @@ -207,4 +207,19 @@ describe('pipeline', function () {
});
});
});

describe('#length', function () {
it('return the command count', function () {
var redis = new Redis();

var pipeline1 = redis.pipeline().multi().set('foo', 'bar').get('foo').exec();
expect(pipeline1.length).to.eql(4);

var pipeline2 = redis.pipeline([
['set', 'foo', 'bar'],
['get', 'foo']
]);
expect(pipeline2.length).to.eql(2);
});
});
});

0 comments on commit a6060cb

Please sign in to comment.