@@ -6,8 +6,6 @@ var util = require('util');
6
6
var utils = require ( './lib/utils' ) ;
7
7
var Queue = require ( 'double-ended-queue' ) ;
8
8
var errorClasses = require ( './lib/customErrors' ) ;
9
- var Command = require ( './lib/command' ) . Command ;
10
- var OfflineCommand = require ( './lib/command' ) . OfflineCommand ;
11
9
var EventEmitter = require ( 'events' ) ;
12
10
var Parser = require ( 'redis-parser' ) ;
13
11
var commands = require ( 'redis-commands' ) ;
@@ -156,6 +154,7 @@ function RedisClient (options, stream) {
156
154
this . times_connected = 0 ;
157
155
this . buffers = options . return_buffers || options . detect_buffers ;
158
156
this . options = options ;
157
+ this . old_state = { } ;
159
158
this . reply = 'ON' ; // Returning replies is the default
160
159
// Init parser
161
160
this . reply_parser = create_parser ( this ) ;
@@ -443,14 +442,10 @@ RedisClient.prototype.on_ready = function () {
443
442
444
443
// Restore modal commands from previous connection. The order of the commands is important
445
444
if ( this . selected_db !== undefined ) {
446
- this . internal_send_command ( ' select' , [ this . selected_db ] ) ;
445
+ this . select ( this . selected_db ) ;
447
446
}
448
- if ( this . old_state !== null ) {
449
- this . monitoring = this . old_state . monitoring ;
450
- this . pub_sub_mode = this . old_state . pub_sub_mode ;
451
- }
452
- if ( this . monitoring ) { // Monitor has to be fired before pub sub commands
453
- this . internal_send_command ( 'monitor' , [ ] ) ; // The state is still set
447
+ if ( this . old_state . monitoring ) { // Monitor has to be fired before pub sub commands
448
+ this . monitor ( ) ;
454
449
}
455
450
var callback_count = Object . keys ( this . subscription_set ) . length ;
456
451
if ( ! this . options . disable_resubscribing && callback_count ) {
@@ -466,8 +461,8 @@ RedisClient.prototype.on_ready = function () {
466
461
debug ( 'Sending pub/sub on_ready commands' ) ;
467
462
for ( var key in this . subscription_set ) {
468
463
var command = key . slice ( 0 , key . indexOf ( '_' ) ) ;
469
- var args = self . subscription_set [ key ] ;
470
- self . internal_send_command ( command , [ args ] , callback ) ;
464
+ var args = this . subscription_set [ key ] ;
465
+ this [ command ] ( [ args ] , callback ) ;
471
466
}
472
467
this . send_offline_queue ( ) ;
473
468
return ;
@@ -530,7 +525,7 @@ RedisClient.prototype.ready_check = function () {
530
525
RedisClient . prototype . send_offline_queue = function ( ) {
531
526
for ( var command_obj = this . offline_queue . shift ( ) ; command_obj ; command_obj = this . offline_queue . shift ( ) ) {
532
527
debug ( 'Sending offline command: ' + command_obj . command ) ;
533
- this . internal_send_command ( command_obj . command , command_obj . args , command_obj . callback , command_obj . call_on_write ) ;
528
+ this . internal_send_command ( command_obj ) ;
534
529
}
535
530
this . drain ( ) ;
536
531
} ;
@@ -575,8 +570,7 @@ RedisClient.prototype.connection_gone = function (why, error) {
575
570
this . pipeline = false ;
576
571
577
572
var state = {
578
- monitoring : this . monitoring ,
579
- pub_sub_mode : this . pub_sub_mode
573
+ monitoring : this . monitoring
580
574
} ;
581
575
this . old_state = state ;
582
576
this . monitoring = false ;
@@ -834,7 +828,6 @@ RedisClient.prototype.return_reply = function (reply) {
834
828
835
829
function handle_offline_command ( self , command_obj ) {
836
830
var command = command_obj . command ;
837
- var callback = command_obj . callback ;
838
831
var err , msg ;
839
832
if ( self . closing || ! self . enable_offline_queue ) {
840
833
command = command . toUpperCase ( ) ;
@@ -852,10 +845,10 @@ function handle_offline_command (self, command_obj) {
852
845
code : 'NR_CLOSED' ,
853
846
command : command
854
847
} ) ;
855
- if ( command_obj . args && command_obj . args . length ) {
848
+ if ( command_obj . args . length ) {
856
849
err . args = command_obj . args ;
857
850
}
858
- utils . reply_in_order ( self , callback , err ) ;
851
+ utils . reply_in_order ( self , command_obj . callback , err ) ;
859
852
} else {
860
853
debug ( 'Queueing ' + command + ' for next server connection.' ) ;
861
854
self . offline_queue . push ( command_obj ) ;
@@ -865,22 +858,23 @@ function handle_offline_command (self, command_obj) {
865
858
866
859
// Do not call internal_send_command directly, if you are not absolutly certain it handles everything properly
867
860
// e.g. monitor / info does not work with internal_send_command only
868
- RedisClient . prototype . internal_send_command = function ( command , args , callback , call_on_write ) {
869
- var arg , prefix_keys , command_obj ;
861
+ RedisClient . prototype . internal_send_command = function ( command_obj ) {
862
+ var arg , prefix_keys ;
870
863
var i = 0 ;
871
864
var command_str = '' ;
865
+ var args = command_obj . args ;
866
+ var command = command_obj . command ;
872
867
var len = args . length ;
873
868
var big_data = false ;
874
- var buffer_args = false ;
875
869
var args_copy = new Array ( len ) ;
876
870
877
- if ( process . domain && callback ) {
878
- callback = process . domain . bind ( callback ) ;
871
+ if ( process . domain && command_obj . callback ) {
872
+ command_obj . callback = process . domain . bind ( command_obj . callback ) ;
879
873
}
880
874
881
875
if ( this . ready === false || this . stream . writable === false ) {
882
876
// Handle offline commands right away
883
- handle_offline_command ( this , new OfflineCommand ( command , args , callback , call_on_write ) ) ;
877
+ handle_offline_command ( this , command_obj ) ;
884
878
return false ; // Indicate buffering
885
879
}
886
880
@@ -905,7 +899,7 @@ RedisClient.prototype.internal_send_command = function (command, args, callback,
905
899
args_copy [ i ] = 'null' ; // Backwards compatible :/
906
900
} else if ( Buffer . isBuffer ( args [ i ] ) ) {
907
901
args_copy [ i ] = args [ i ] ;
908
- buffer_args = true ;
902
+ command_obj . buffer_args = true ;
909
903
big_data = true ;
910
904
} else {
911
905
this . warn (
@@ -927,8 +921,6 @@ RedisClient.prototype.internal_send_command = function (command, args, callback,
927
921
args_copy [ i ] = '' + args [ i ] ;
928
922
}
929
923
}
930
- // Pass the original args to make sure in error cases the original arguments are returned
931
- command_obj = new Command ( command , args , buffer_args , callback ) ;
932
924
933
925
if ( this . options . prefix ) {
934
926
prefix_keys = commands . getKeyIndexes ( command , args_copy ) ;
@@ -967,8 +959,8 @@ RedisClient.prototype.internal_send_command = function (command, args, callback,
967
959
debug ( 'send_command: buffer send ' + arg . length + ' bytes' ) ;
968
960
}
969
961
}
970
- if ( call_on_write ) {
971
- call_on_write ( ) ;
962
+ if ( command_obj . call_on_write ) {
963
+ command_obj . call_on_write ( ) ;
972
964
}
973
965
// Handle `CLIENT REPLY ON|OFF|SKIP`
974
966
// This has to be checked after call_on_write
@@ -978,8 +970,8 @@ RedisClient.prototype.internal_send_command = function (command, args, callback,
978
970
} else {
979
971
// Do not expect a reply
980
972
// Does this work in combination with the pub sub mode?
981
- if ( callback ) {
982
- utils . reply_in_order ( this , callback , null , undefined , this . command_queue ) ;
973
+ if ( command_obj . callback ) {
974
+ utils . reply_in_order ( this , command_obj . callback , null , undefined , this . command_queue ) ;
983
975
}
984
976
if ( this . reply === 'SKIP' ) {
985
977
this . reply = 'SKIP_ONE_MORE' ;
0 commit comments