Skip to content

Commit

Permalink
Wrap the detections in a function
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Oct 12, 2015
1 parent 41df162 commit e46872a
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,7 @@ Limiter.prototype.get = function (fn) {

// If the request has failed, it means the values already
// exist in which case we need to get the latest values.
if (!res[0]) return mget();
if (Array.isArray(res[0])) {
// ioredis
if (!res[0][1]) return mget();
} else {
// node_redis
if (!res[0]) return mget();
}
if (isFirstReplyNull(res)) return mget();

fn(null, {
total: max,
Expand Down Expand Up @@ -113,7 +106,7 @@ Limiter.prototype.get = function (fn) {
.set([count, n - 1, 'PX', ex * 1000 - Date.now(), 'XX'])
.exec(function (err, res) {
if (err) return fn(err);
if (!res || !res[0]) return mget();
if (isFirstReplyNull(res)) return mget();
n = n - 1;
done();
});
Expand All @@ -133,3 +126,24 @@ Limiter.prototype.get = function (fn) {

mget();
};

/**
* Check whether the first item of multi replies is null,
* works with ioredis and node_redis
*
* @param {Array} replies
* @return {Boolean}
* @api private
*/

function isFirstReplyNull(replies) {
if (!replies) {
return true;
}

return Array.isArray(replies[0]) ?
// ioredis
!replies[0][1] :
// node_redis
!replies[0];
}

0 comments on commit e46872a

Please sign in to comment.