@@ -332,24 +332,8 @@ export default class RedisClient<
332332 ) ;
333333 }
334334
335- #handshake( asap = false , promises : Array < Promise < unknown > > = [ ] ) {
336- if ( this . #selectedDB !== 0 ) {
337- promises . push (
338- this . #queue. addCommand (
339- [ 'SELECT' , this . #selectedDB. toString ( ) ] ,
340- { asap }
341- )
342- ) ;
343- }
344-
345- if ( this . #options?. readonly ) {
346- promises . push (
347- this . #queue. addCommand (
348- COMMANDS . READONLY . transformArguments ( ) ,
349- { asap }
350- )
351- ) ;
352- }
335+ #handshake( selectedDB : number ) {
336+ const commands = [ ] ;
353337
354338 if ( this . #options?. RESP ) {
355339 const hello : HelloOptions = { } ;
@@ -365,43 +349,45 @@ export default class RedisClient<
365349 hello . SETNAME = this . #options. name ;
366350 }
367351
368- promises . push (
369- this . #queue. addCommand (
370- HELLO . transformArguments ( this . #options. RESP , hello ) ,
371- { asap }
372- )
352+ commands . push (
353+ HELLO . transformArguments ( this . #options. RESP , hello )
373354 ) ;
374355 } else {
375- if ( this . #options?. name ) {
376- promises . push (
377- this . #queue . addCommand (
378- COMMANDS . CLIENT_SETNAME . transformArguments ( this . #options. name ) ,
379- { asap }
380- )
356+ if ( this . #options?. username || this . #options ?. password ) {
357+ commands . push (
358+ COMMANDS . AUTH . transformArguments ( {
359+ username : this . #options. username ,
360+ password : this . #options . password ?? ''
361+ } )
381362 ) ;
382363 }
383364
384- if ( this . #options?. username || this . #options?. password ) {
385- promises . push (
386- this . #queue. addCommand (
387- COMMANDS . AUTH . transformArguments ( {
388- username : this . #options. username ,
389- password : this . #options. password ?? ''
390- } ) ,
391- { asap }
392- )
365+ if ( this . #options?. name ) {
366+ commands . push (
367+ COMMANDS . CLIENT_SETNAME . transformArguments ( this . #options. name )
393368 ) ;
394369 }
395370 }
396371
397- return promises ;
372+ if ( selectedDB !== 0 ) {
373+ commands . push ( [ 'SELECT' , this . #selectedDB. toString ( ) ] ) ;
374+ }
375+
376+ if ( this . #options?. readonly ) {
377+ commands . push (
378+ COMMANDS . READONLY . transformArguments ( )
379+ ) ;
380+ }
381+
382+ return commands ;
398383 }
399384
400385 #initiateSocket( ) : RedisSocket {
401386 const socketInitiator = ( ) => {
402- const promises : Array < Promise < unknown > > = [ ] ;
387+ const promises = [ ] ,
388+ chainId = Symbol ( 'Socket Initiator' ) ;
403389
404- const resubscribePromise = this . #queue. resubscribe ( ) ;
390+ const resubscribePromise = this . #queue. resubscribe ( chainId ) ;
405391 if ( resubscribePromise ) {
406392 promises . push ( resubscribePromise ) ;
407393 }
@@ -410,13 +396,24 @@ export default class RedisClient<
410396 promises . push (
411397 this . #queue. monitor (
412398 this . #monitorCallback,
413- this . _commandOptions ?. typeMapping ,
414- true
399+ {
400+ typeMapping : this . _commandOptions ?. typeMapping ,
401+ chainId,
402+ asap : true
403+ }
415404 )
416405 ) ;
417406 }
418407
419- this . #handshake( true , promises ) ;
408+ const commands = this . #handshake( this . #selectedDB) ;
409+ for ( let i = commands . length - 1 ; i >= 0 ; -- i ) {
410+ promises . push (
411+ this . #queue. addCommand ( commands [ i ] , {
412+ chainId,
413+ asap : true
414+ } )
415+ ) ;
416+ }
420417
421418 if ( promises . length ) {
422419 this . #write( ) ;
@@ -885,7 +882,9 @@ export default class RedisClient<
885882 }
886883
887884 async MONITOR ( callback : MonitorCallback < TYPE_MAPPING > ) {
888- const promise = this . _self . #queue. monitor ( callback , this . _commandOptions ?. typeMapping ) ;
885+ const promise = this . _self . #queue. monitor ( callback , {
886+ typeMapping : this . _commandOptions ?. typeMapping
887+ } ) ;
889888 this . _self . #scheduleWrite( ) ;
890889 await promise ;
891890 this . _self . #monitorCallback = callback ;
@@ -897,10 +896,20 @@ export default class RedisClient<
897896 * Reset the client to its default state (i.e. stop PubSub, stop monitoring, select default DB, etc.)
898897 */
899898 async reset ( ) {
900- const promises = [ this . _self . #queue. reset ( ) ] ;
901- this . _self . #handshake( false , promises ) ;
899+ const chainId = Symbol ( 'Reset Chain' ) ,
900+ promises = [ this . _self . #queue. reset ( chainId ) ] ,
901+ selectedDB = this . _self . #options?. database ?? 0 ;
902+ for ( const command of this . _self . #handshake( selectedDB ) ) {
903+ promises . push (
904+ this . _self . #queue. addCommand ( command , {
905+ chainId
906+ } )
907+ ) ;
908+ }
902909 this . _self . #scheduleWrite( ) ;
903910 await Promise . all ( promises ) ;
911+ this . _self . #selectedDB = selectedDB ;
912+ this . _self . #monitorCallback = undefined ;
904913 }
905914
906915 /**
0 commit comments