@@ -455,7 +455,7 @@ export default class RedisClient<
455
455
this . #validateOptions( options )
456
456
this . #options = this . #initiateOptions( options ) ;
457
457
this . #queue = this . #initiateQueue( ) ;
458
- this . #socket = this . #initiateSocket ( ) ;
458
+ this . #socket = this . #createSocket ( this . #options ) ;
459
459
460
460
if ( options ?. clientSideCache ) {
461
461
if ( options . clientSideCache instanceof ClientSideCacheProvider ) {
@@ -688,38 +688,10 @@ export default class RedisClient<
688
688
return commands ;
689
689
}
690
690
691
- #initiateSocket( ) : RedisSocket {
692
- const socketInitiator = async ( ) => {
693
- const promises = [ ] ,
694
- chainId = Symbol ( 'Socket Initiator' ) ;
691
+ async #initiateSocket( ) : Promise < void > {
692
+ await this . #socket. waitForReady ( ) ;
695
693
696
- const resubscribePromise = this . #queue. resubscribe ( chainId ) ;
697
- if ( resubscribePromise ) {
698
- promises . push ( resubscribePromise ) ;
699
- }
700
-
701
- if ( this . #monitorCallback) {
702
- promises . push (
703
- this . #queue. monitor (
704
- this . #monitorCallback,
705
- {
706
- typeMapping : this . _commandOptions ?. typeMapping ,
707
- chainId,
708
- asap : true
709
- }
710
- )
711
- ) ;
712
- }
713
-
714
- promises . push ( ...( await this . #handshake( chainId , true ) ) ) ;
715
-
716
- if ( promises . length ) {
717
- this . #write( ) ;
718
- return Promise . all ( promises ) ;
719
- }
720
- } ;
721
-
722
- return new RedisSocket ( socketInitiator , this . #options?. socket )
694
+ this . #socket
723
695
. on ( 'data' , chunk => {
724
696
try {
725
697
this . #queue. decoder . write ( chunk ) ;
@@ -737,15 +709,47 @@ export default class RedisClient<
737
709
this . #queue. flushAll ( err ) ;
738
710
}
739
711
} )
740
- . on ( 'connect' , ( ) => this . emit ( 'connect' ) )
741
- . on ( 'ready' , ( ) => {
742
- this . emit ( 'ready' ) ;
743
- this . #setPingTimer( ) ;
744
- this . #maybeScheduleWrite( ) ;
745
- } )
746
712
. on ( 'reconnecting' , ( ) => this . emit ( 'reconnecting' ) )
747
713
. on ( 'drain' , ( ) => this . #maybeScheduleWrite( ) )
748
714
. on ( 'end' , ( ) => this . emit ( 'end' ) ) ;
715
+
716
+ const promises = [ ] ;
717
+ const chainId = Symbol ( 'Socket Initiator' ) ;
718
+
719
+ const resubscribePromise = this . #queue. resubscribe ( chainId ) ;
720
+ if ( resubscribePromise ) {
721
+ promises . push ( resubscribePromise ) ;
722
+ }
723
+
724
+ if ( this . #monitorCallback) {
725
+ promises . push (
726
+ this . #queue. monitor (
727
+ this . #monitorCallback,
728
+ {
729
+ typeMapping : this . _commandOptions ?. typeMapping ,
730
+ chainId,
731
+ asap : true
732
+ }
733
+ )
734
+ ) ;
735
+ }
736
+
737
+ promises . push ( ...( await this . #handshake( chainId , true ) ) ) ;
738
+
739
+ this . #setPingTimer( ) ;
740
+
741
+ if ( promises . length ) {
742
+ this . #write( ) ;
743
+ await Promise . all ( promises ) ;
744
+ }
745
+ }
746
+
747
+ #createSocket( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) : RedisSocket {
748
+ return new RedisSocket ( options ?. socket )
749
+ . on ( 'connect' , ( ) => this . emit ( 'connect' ) )
750
+ . on ( 'ready' , ( ) => {
751
+ this . emit ( 'ready' ) ;
752
+ } ) ;
749
753
}
750
754
751
755
#pingTimer?: NodeJS . Timeout ;
@@ -854,6 +858,7 @@ export default class RedisClient<
854
858
855
859
async connect ( ) {
856
860
await this . _self . #socket. connect ( ) ;
861
+ await this . _self . #initiateSocket( ) ;
857
862
return this as unknown as RedisClientType < M , F , S , RESP , TYPE_MAPPING > ;
858
863
}
859
864
0 commit comments