@@ -332,24 +332,8 @@ export default class RedisClient<
332
332
) ;
333
333
}
334
334
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 = [ ] ;
353
337
354
338
if ( this . #options?. RESP ) {
355
339
const hello : HelloOptions = { } ;
@@ -365,43 +349,45 @@ export default class RedisClient<
365
349
hello . SETNAME = this . #options. name ;
366
350
}
367
351
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 )
373
354
) ;
374
355
} 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
+ } )
381
362
) ;
382
363
}
383
364
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 )
393
368
) ;
394
369
}
395
370
}
396
371
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 ;
398
383
}
399
384
400
385
#initiateSocket( ) : RedisSocket {
401
386
const socketInitiator = ( ) => {
402
- const promises : Array < Promise < unknown > > = [ ] ;
387
+ const promises = [ ] ,
388
+ chainId = Symbol ( 'Socket Initiator' ) ;
403
389
404
- const resubscribePromise = this . #queue. resubscribe ( ) ;
390
+ const resubscribePromise = this . #queue. resubscribe ( chainId ) ;
405
391
if ( resubscribePromise ) {
406
392
promises . push ( resubscribePromise ) ;
407
393
}
@@ -410,13 +396,24 @@ export default class RedisClient<
410
396
promises . push (
411
397
this . #queue. monitor (
412
398
this . #monitorCallback,
413
- this . _commandOptions ?. typeMapping ,
414
- true
399
+ {
400
+ typeMapping : this . _commandOptions ?. typeMapping ,
401
+ chainId,
402
+ asap : true
403
+ }
415
404
)
416
405
) ;
417
406
}
418
407
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
+ }
420
417
421
418
if ( promises . length ) {
422
419
this . #write( ) ;
@@ -885,7 +882,9 @@ export default class RedisClient<
885
882
}
886
883
887
884
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
+ } ) ;
889
888
this . _self . #scheduleWrite( ) ;
890
889
await promise ;
891
890
this . _self . #monitorCallback = callback ;
@@ -897,10 +896,20 @@ export default class RedisClient<
897
896
* Reset the client to its default state (i.e. stop PubSub, stop monitoring, select default DB, etc.)
898
897
*/
899
898
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
+ }
902
909
this . _self . #scheduleWrite( ) ;
903
910
await Promise . all ( promises ) ;
911
+ this . _self . #selectedDB = selectedDB ;
912
+ this . _self . #monitorCallback = undefined ;
904
913
}
905
914
906
915
/**
0 commit comments