@@ -2,7 +2,7 @@ import { RedisClusterClientOptions, RedisClusterOptions } from '.';
22import { RootNodesUnavailableError } from '../errors' ;
33import RedisClient , { RedisClientOptions , RedisClientType } from '../client' ;
44import { EventEmitter } from 'node:stream' ;
5- import { ChannelListeners , PUBSUB_TYPE , PubSubTypeListeners } from '../client/pub-sub' ;
5+ import { ChannelListeners , PUBSUB_TYPE , PubSubListeners , PubSubTypeListeners } from '../client/pub-sub' ;
66import { RedisArgument , RedisFunctions , RedisModules , RedisScripts , RespVersions , TypeMapping } from '../RESP/types' ;
77import calculateSlot from 'cluster-key-slot' ;
88import { RedisSocketOptions } from '../client/socket' ;
@@ -186,21 +186,6 @@ export default class RedisClusterSlots<
186186 this . clientSideCache ?. clear ( ) ;
187187 this . clientSideCache ?. disable ( ) ;
188188
189-
190- const allChannelListeners = new Map < string , ChannelListeners > ( ) ;
191-
192- for ( const master of this . masters ) {
193- const shardedClient = master . pubSub ?. client ;
194- if ( ! shardedClient ) continue ;
195- for ( const channel of shardedClient . getShardedChannels ( ) ) {
196- const listeners = shardedClient . removeShardedListeners ( channel ) ;
197- if ( allChannelListeners . get ( channel ) ) {
198- console . warn ( `Found existing listeners, will be overwritten...` ) ;
199- }
200- allChannelListeners . set ( channel , listeners ) ;
201- }
202- }
203-
204189 try {
205190 const addressesInUse = new Set < string > ( ) ,
206191 promises : Array < Promise < unknown > > = [ ] ,
@@ -256,9 +241,6 @@ export default class RedisClusterSlots<
256241 this . nodeByAddress . delete ( address ) ;
257242 }
258243
259- this . #emit( '__refreshShardedChannels' , allChannelListeners ) ;
260-
261-
262244 await Promise . all ( promises ) ;
263245 this . clientSideCache ?. enable ( ) ;
264246
@@ -357,26 +339,29 @@ export default class RedisClusterSlots<
357339 const socket =
358340 this . #getNodeAddress( node . address ) ??
359341 { host : node . host , port : node . port , } ;
360- const client = Object . freeze ( {
342+ const clientInfo = Object . freeze ( {
361343 host : socket . host ,
362344 port : socket . port ,
363345 } ) ;
364346 const emit = this . #emit;
365- return this . #clientFactory(
347+ const client = this . #clientFactory(
366348 this . #clientOptionsDefaults( {
367349 clientSideCache : this . clientSideCache ,
368350 RESP : this . #options. RESP ,
369351 socket,
370352 readonly,
371353 } ) )
372- . on ( 'error' , error => emit ( 'node-error' , error , client ) )
373- . on ( 'reconnecting' , ( ) => emit ( 'node-reconnecting' , client ) )
374- . once ( 'ready' , ( ) => emit ( 'node-ready' , client ) )
375- . once ( 'connect' , ( ) => emit ( 'node-connect' , client ) )
376- . once ( 'end' , ( ) => emit ( 'node-disconnect' , client ) ) ;
377- . on ( '__MOVED' , ( ) => {
378- this . rediscover ( client ) ;
379- } )
354+ . on ( 'error' , error => emit ( 'node-error' , error , clientInfo ) )
355+ . on ( 'reconnecting' , ( ) => emit ( 'node-reconnecting' , clientInfo ) )
356+ . once ( 'ready' , ( ) => emit ( 'node-ready' , clientInfo ) )
357+ . once ( 'connect' , ( ) => emit ( 'node-connect' , clientInfo ) )
358+ . once ( 'end' , ( ) => emit ( 'node-disconnect' , clientInfo ) )
359+ . on ( '__MOVED' , async ( allPubSubListeners : PubSubListeners ) => {
360+ await this . rediscover ( client ) ;
361+ this . #emit( '__resubscribeAllPubSubListeners' , allPubSubListeners ) ;
362+ } ) ;
363+
364+ return client ;
380365 }
381366
382367 #createNodeClient( node : ShardNode < M , F , S , RESP , TYPE_MAPPING > , readonly ?: boolean ) {
0 commit comments