@@ -18,6 +18,10 @@ function stringify(obj: Record<string, any>) {
1818 return str
1919}
2020
21+ export type PubsubOptions = {
22+ debug ?: boolean
23+ }
24+
2125/**
2226 * A class for managing Redis Publish/Subscribe operations.
2327 *
@@ -52,6 +56,7 @@ export class RedisPubSub extends EventEmitter {
5256 private redisCloudUrl : string
5357 private publisher ! : Redis
5458 private subscriber ! : Redis
59+ private options : PubsubOptions
5560
5661 private channelRefCount = new Map < string , number > ( )
5762 private patternRefCount = new Map < string , number > ( )
@@ -123,9 +128,10 @@ export class RedisPubSub extends EventEmitter {
123128 } )
124129 }
125130
126- constructor ( redisCloudUrl : string ) {
131+ constructor ( redisCloudUrl : string , options : PubsubOptions = { } ) {
127132 super ( )
128133 this . redisCloudUrl = redisCloudUrl
134+ this . options = options
129135 }
130136
131137 /**
@@ -158,8 +164,6 @@ export class RedisPubSub extends EventEmitter {
158164 // numberOfKeys: -1,
159165 // lua: 'return redis.call("del", unpack(KEYS))',
160166 // })
161-
162- console . log ( 'Connecting to redis pubsub' )
163167 // await this.client.connect()
164168 // await this.subscriber.connect()
165169 this . connectEventListeners ( this . publisher , 'publisher' )
@@ -175,42 +179,45 @@ export class RedisPubSub extends EventEmitter {
175179
176180 // "wait" | "reconnecting" | "connecting" | "connect" | "ready" | "close" | "end";
177181
178- client . on ( 'connecting' , ( ) => {
179- console . log ( `Connecting to Redis ${ clientType } ...` )
180- } )
182+ if ( this . options . debug ) {
183+ client . on ( 'connecting' , ( ) => {
184+ console . log ( `Connecting to Redis ${ clientType } ...` )
185+ } )
181186
182- client . on ( 'connect' , ( ) => {
183- console . log ( `Redis ${ clientType } connected successfully.` )
184- } )
187+ client . on ( 'connect' , ( ) => {
188+ console . log ( `Redis ${ clientType } connected successfully.` )
189+ } )
185190
186- client . on ( 'close' , ( ) => {
187- console . log ( `Redis ${ clientType } connection closed.` )
188- } )
191+ client . on ( 'close' , ( ) => {
192+ console . log ( `Redis ${ clientType } connection closed.` )
193+ } )
189194
190- client . on ( 'reconnecting' , ( ) => {
191- console . warn ( `Reconnecting to Redis ${ clientType } ...` )
192- } )
195+ client . on ( 'reconnecting' , ( ) => {
196+ console . warn ( `Reconnecting to Redis ${ clientType } ...` )
197+ } )
193198
194- client . on ( 'end' , ( ) => {
195- console . log ( `Redis ${ clientType } connection ended.` )
196- } )
199+ client . on ( 'end' , ( ) => {
200+ console . log ( `Redis ${ clientType } connection ended.` )
201+ } )
197202
198- client . on ( 'ready' , ( ) => {
199- console . log ( `Redis ${ clientType } is ready.` )
200- } )
203+ client . on ( 'ready' , ( ) => {
204+ console . log ( `Redis ${ clientType } is ready.` )
205+ } )
201206
202- client . on ( 'reconnecting' , ( ) => {
203- console . log ( `Redis ${ clientType } reconnecing.` )
204- } )
207+ client . on ( 'reconnecting' , ( ) => {
208+ console . log ( `Redis ${ clientType } reconnecing.` )
209+ } )
205210
206- client . on ( 'wait' , ( time : string ) => {
207- console . log ( `Redis ${ clientType } is waiting:` , time )
208- } )
211+ client . on ( 'wait' , ( time : string ) => {
212+ console . log ( `Redis ${ clientType } is waiting:` , time )
213+ } )
214+ }
209215
210216 // handle subscriber reconnection
211217
212218 if ( clientType === 'subscriber' ) {
213- console . log ( 'Setting up subscriber event listeners...' )
219+ if ( this . options . debug )
220+ console . log ( 'Setting up subscriber event listeners...' )
214221 client . on ( 'message' , ( channel , message ) => {
215222 this . emit ( channel , message )
216223 } )
0 commit comments