1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Refactor command parsing

This commit is contained in:
Ruben Bridgewater
2016-05-27 17:32:42 +02:00
parent 899f9b7fe4
commit 8b6f2dd35e
10 changed files with 106 additions and 123 deletions

View File

@@ -3,6 +3,7 @@
var utils = require('./utils');
var debug = require('./debug');
var Multi = require('./multi');
var Command = require('./command');
var no_password_is_set = /no password is set/;
var loading = /LOADING/;
var RedisClient = require('../').RedisClient;
@@ -42,33 +43,34 @@ function select_callback (self, db, callback) {
}
RedisClient.prototype.select = RedisClient.prototype.SELECT = function select (db, callback) {
return this.internal_send_command('select', [db], select_callback(this, db, callback));
return this.internal_send_command(new Command('select', [db], select_callback(this, db, callback)));
};
Multi.prototype.select = Multi.prototype.SELECT = function select (db, callback) {
this.queue.push(['select', [db], select_callback(this._client, db, callback)]);
this.queue.push(new Command('select', [db], select_callback(this._client, db, callback)));
return this;
};
function monitor_callback (self, callback) {
return function (err, res) {
if (err === null) {
self.monitoring = true;
}
utils.callback_or_emit(self, callback, err, res);
};
}
RedisClient.prototype.monitor = RedisClient.prototype.MONITOR = function monitor (callback) {
// Use a individual command, as this is a special case that does not has to be checked for any other command
return this.internal_send_command('monitor', [], monitor_callback(this, callback));
var self = this;
var call_on_write = function () {
// Activating monitor mode has to happen before Redis returned the callback,
// as the client could receive monitoring commands before the callback returned through a race condition
self.monitoring = true;
};
return this.internal_send_command(new Command('monitor', [], callback, call_on_write));
};
// Only works with batch, not in a transaction
Multi.prototype.monitor = Multi.prototype.MONITOR = function monitor (callback) {
// Use a individual command, as this is a special case that does not has to be checked for any other command
if (this.exec !== this.exec_transaction) {
this.queue.push(['monitor', [], monitor_callback(this._client, callback)]);
var self = this;
var call_on_write = function () {
self._client.monitoring = true;
};
this.queue.push(new Command('monitor', [], callback, call_on_write));
return this;
}
// Set multi monitoring to indicate the exec that it should abort
@@ -100,7 +102,7 @@ RedisClient.prototype.QUIT = RedisClient.prototype.quit = function quit (callbac
// TODO: Consider this for v.3
// Allow the quit command to be fired as soon as possible to prevent it landing in the offline queue.
// this.ready = this.offline_queue.length === 0;
var backpressure_indicator = this.internal_send_command('quit', [], quit_callback(this, callback));
var backpressure_indicator = this.internal_send_command(new Command('quit', [], quit_callback(this, callback)));
// Calling quit should always end the connection, no matter if there's a connection or not
this.closing = true;
this.ready = false;
@@ -115,7 +117,7 @@ Multi.prototype.QUIT = Multi.prototype.quit = function quit (callback) {
self.closing = true;
self.ready = false;
};
this.queue.push(['quit', [], quit_callback(self, callback), call_on_write]);
this.queue.push(new Command('quit', [], quit_callback(self, callback), call_on_write));
return this;
};
@@ -164,7 +166,7 @@ RedisClient.prototype.info = RedisClient.prototype.INFO = function info (section
} else if (section !== undefined) {
args = Array.isArray(section) ? section : [section];
}
return this.internal_send_command('info', args, info_callback(this, callback));
return this.internal_send_command(new Command('info', args, info_callback(this, callback)));
};
Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) {
@@ -174,7 +176,7 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback)
} else if (section !== undefined) {
args = Array.isArray(section) ? section : [section];
}
this.queue.push(['info', args, info_callback(this._client, callback)]);
this.queue.push(new Command('info', args, info_callback(this._client, callback)));
return this;
};
@@ -205,7 +207,7 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
this.auth_pass = pass;
var ready = this.ready;
this.ready = ready || this.offline_queue.length === 0;
var tmp = this.internal_send_command('auth', [pass], auth_callback(this, pass, callback));
var tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback)));
this.ready = ready;
return tmp;
};
@@ -216,7 +218,7 @@ Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, callback) {
// Stash auth for connect and reconnect.
this.auth_pass = pass;
this.queue.push(['auth', [pass], auth_callback(this._client, callback)]);
this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback)));
return this;
};
@@ -262,7 +264,7 @@ RedisClient.prototype.client = RedisClient.prototype.CLIENT = function client ()
};
}
}
return this.internal_send_command('client', arr, callback, call_on_write);
return this.internal_send_command(new Command('client', arr, callback, call_on_write));
};
Multi.prototype.client = Multi.prototype.CLIENT = function client () {
@@ -307,7 +309,7 @@ Multi.prototype.client = Multi.prototype.CLIENT = function client () {
};
}
}
this.queue.push(['client', arr, callback, call_on_write]);
this.queue.push(new Command('client', arr, callback, call_on_write));
return this;
};
@@ -347,7 +349,7 @@ RedisClient.prototype.hmset = RedisClient.prototype.HMSET = function hmset () {
arr[i] = arguments[i];
}
}
return this.internal_send_command('hmset', arr, callback);
return this.internal_send_command(new Command('hmset', arr, callback));
};
Multi.prototype.hmset = Multi.prototype.HMSET = function hmset () {
@@ -386,7 +388,7 @@ Multi.prototype.hmset = Multi.prototype.HMSET = function hmset () {
arr[i] = arguments[i];
}
}
this.queue.push(['hmset', arr, callback]);
this.queue.push(new Command('hmset', arr, callback));
return this;
};
@@ -414,7 +416,7 @@ RedisClient.prototype.subscribe = RedisClient.prototype.SUBSCRIBE = function sub
var call_on_write = function () {
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
return this.internal_send_command('subscribe', arr, callback, call_on_write);
return this.internal_send_command(new Command('subscribe', arr, callback, call_on_write));
};
Multi.prototype.subscribe = Multi.prototype.SUBSCRIBE = function subscribe () {
@@ -441,7 +443,7 @@ Multi.prototype.subscribe = Multi.prototype.SUBSCRIBE = function subscribe () {
var call_on_write = function () {
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
this.queue.push(['subscribe', arr, callback, call_on_write]);
this.queue.push(new Command('subscribe', arr, callback, call_on_write));
return this;
};
@@ -470,7 +472,7 @@ RedisClient.prototype.unsubscribe = RedisClient.prototype.UNSUBSCRIBE = function
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
return this.internal_send_command('unsubscribe', arr, callback, call_on_write);
return this.internal_send_command(new Command('unsubscribe', arr, callback, call_on_write));
};
Multi.prototype.unsubscribe = Multi.prototype.UNSUBSCRIBE = function unsubscribe () {
@@ -498,7 +500,7 @@ Multi.prototype.unsubscribe = Multi.prototype.UNSUBSCRIBE = function unsubscribe
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
this.queue.push(['unsubscribe', arr, callback, call_on_write]);
this.queue.push(new Command('unsubscribe', arr, callback, call_on_write));
return this;
};
@@ -526,7 +528,7 @@ RedisClient.prototype.psubscribe = RedisClient.prototype.PSUBSCRIBE = function p
var call_on_write = function () {
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
return this.internal_send_command('psubscribe', arr, callback, call_on_write);
return this.internal_send_command(new Command('psubscribe', arr, callback, call_on_write));
};
Multi.prototype.psubscribe = Multi.prototype.PSUBSCRIBE = function psubscribe () {
@@ -553,7 +555,7 @@ Multi.prototype.psubscribe = Multi.prototype.PSUBSCRIBE = function psubscribe ()
var call_on_write = function () {
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
this.queue.push(['psubscribe', arr, callback, call_on_write]);
this.queue.push(new Command('psubscribe', arr, callback, call_on_write));
return this;
};
@@ -582,7 +584,7 @@ RedisClient.prototype.punsubscribe = RedisClient.prototype.PUNSUBSCRIBE = functi
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
return this.internal_send_command('punsubscribe', arr, callback, call_on_write);
return this.internal_send_command(new Command('punsubscribe', arr, callback, call_on_write));
};
Multi.prototype.punsubscribe = Multi.prototype.PUNSUBSCRIBE = function punsubscribe () {
@@ -610,6 +612,6 @@ Multi.prototype.punsubscribe = Multi.prototype.PUNSUBSCRIBE = function punsubscr
// Pub sub has to be activated even if not in pub sub mode, as the return value is manipulated in the callback
self.pub_sub_mode = self.pub_sub_mode || self.command_queue.length + 1;
};
this.queue.push(['punsubscribe', arr, callback, call_on_write]);
this.queue.push(new Command('punsubscribe', arr, callback, call_on_write));
return this;
};