@@ -80,9 +80,9 @@ type PubSubNode<
80
80
RESP extends RespVersions ,
81
81
TYPE_MAPPING extends TypeMapping
82
82
> = (
83
- Omit < Node < M , F , S , RESP , TYPE_MAPPING > , 'client' > &
84
- Required < Pick < Node < M , F , S , RESP , TYPE_MAPPING > , 'client' > >
85
- ) ;
83
+ Omit < Node < M , F , S , RESP , TYPE_MAPPING > , 'client' > &
84
+ Required < Pick < Node < M , F , S , RESP , TYPE_MAPPING > , 'client' > >
85
+ ) ;
86
86
87
87
type PubSubToResubscribe = Record <
88
88
PUBSUB_TYPE [ 'CHANNELS' ] | PUBSUB_TYPE [ 'PATTERNS' ] ,
@@ -153,6 +153,7 @@ export default class RedisClusterSlots<
153
153
this . #isOpen = true ;
154
154
try {
155
155
await this . #discoverWithRootNodes( ) ;
156
+ this . #emit( 'connect' ) ;
156
157
} catch ( err ) {
157
158
this . #isOpen = false ;
158
159
throw err ;
@@ -333,17 +334,26 @@ export default class RedisClusterSlots<
333
334
}
334
335
335
336
#createClient( node : ShardNode < M , F , S , RESP , TYPE_MAPPING > , readonly = node . readonly ) {
337
+ const socket =
338
+ this . #getNodeAddress( node . address ) ??
339
+ { host : node . host , port : node . port , } ;
340
+ const client = Object . freeze ( {
341
+ host : socket . host ,
342
+ port : socket . port ,
343
+ } ) ;
344
+ const emit = this . #emit;
336
345
return this . #clientFactory(
337
346
this . #clientOptionsDefaults( {
338
347
clientSideCache : this . clientSideCache ,
339
348
RESP : this . #options. RESP ,
340
- socket : this . #getNodeAddress( node . address ) ?? {
341
- host : node . host ,
342
- port : node . port
343
- } ,
344
- readonly
345
- } )
346
- ) . on ( 'error' , err => console . error ( err ) ) ;
349
+ socket,
350
+ readonly,
351
+ } ) )
352
+ . on ( 'error' , error => emit ( 'node-error' , error , client ) )
353
+ . on ( 'reconnecting' , ( ) => emit ( 'node-reconnecting' , client ) )
354
+ . once ( 'ready' , ( ) => emit ( 'node-ready' , client ) )
355
+ . once ( 'connect' , ( ) => emit ( 'node-connect' , client ) )
356
+ . once ( 'end' , ( ) => emit ( 'node-disconnect' , client ) ) ;
347
357
}
348
358
349
359
#createNodeClient( node : ShardNode < M , F , S , RESP , TYPE_MAPPING > , readonly ?: boolean ) {
@@ -443,6 +453,7 @@ export default class RedisClusterSlots<
443
453
this . nodeByAddress . clear ( ) ;
444
454
445
455
await Promise . allSettled ( promises ) ;
456
+ this . #emit( 'disconnect' ) ;
446
457
}
447
458
448
459
getClient (
@@ -542,7 +553,7 @@ export default class RedisClusterSlots<
542
553
node = index < this . masters . length ?
543
554
this . masters [ index ] :
544
555
this . replicas [ index - this . masters . length ] ,
545
- client = this . #createClient( node , false ) ;
556
+ client = this . #createClient( node , false ) ;
546
557
547
558
this . pubSubNode = {
548
559
address : node . address ,
0 commit comments