@@ -44,18 +44,18 @@ const RESP2_PUSH_TYPE_MAPPING = {
4444} ;
4545
4646export default class RedisCommandsQueue {
47- private readonly _maxLength : number | null | undefined ;
48- private readonly _toWrite = new DoublyLinkedList < CommandToWrite > ( ) ;
49- private readonly _waitingForReply = new SinglyLinkedList < CommandWaitingForReply > ( ) ;
50- private readonly _onShardedChannelMoved : OnShardedChannelMoved ;
47+ readonly #maxLength : number | null | undefined ;
48+ readonly #toWrite = new DoublyLinkedList < CommandToWrite > ( ) ;
49+ readonly #waitingForReply = new SinglyLinkedList < CommandWaitingForReply > ( ) ;
50+ readonly #onShardedChannelMoved : OnShardedChannelMoved ;
5151
52- private readonly _pubSub = new PubSub ( ) ;
52+ readonly #pubSub = new PubSub ( ) ;
5353
5454 get isPubSubActive ( ) {
55- return this . _pubSub . isActive ;
55+ return this . #pubSub . isActive ;
5656 }
5757
58- private _chainInExecution : symbol | undefined ;
58+ #chainInExecution : symbol | undefined ;
5959
6060 decoder : Decoder ;
6161
@@ -64,92 +64,92 @@ export default class RedisCommandsQueue {
6464 maxLength : number | null | undefined ,
6565 onShardedChannelMoved : EventEmitter [ 'emit' ]
6666 ) {
67- this . decoder = this . _initiateDecoder ( respVersion ) ;
68- this . _maxLength = maxLength ;
69- this . _onShardedChannelMoved = onShardedChannelMoved ;
67+ this . decoder = this . #initiateDecoder ( respVersion ) ;
68+ this . #maxLength = maxLength ;
69+ this . #onShardedChannelMoved = onShardedChannelMoved ;
7070 }
7171
72- private _initiateDecoder ( respVersion : RespVersions | null | undefined ) {
72+ #initiateDecoder ( respVersion : RespVersions | null | undefined ) {
7373 return respVersion === 3 ?
74- this . _initiateResp3Decoder ( ) :
75- this . _initiateResp2Decoder ( ) ;
74+ this . #initiateResp3Decoder ( ) :
75+ this . #initiateResp2Decoder ( ) ;
7676 }
7777
78- private _onReply ( reply : ReplyUnion ) {
79- this . _waitingForReply . shift ( ) ! . resolve ( reply ) ;
78+ #onReply ( reply : ReplyUnion ) {
79+ this . #waitingForReply . shift ( ) ! . resolve ( reply ) ;
8080 }
8181
82- private _onErrorReply ( err : ErrorReply ) {
83- this . _waitingForReply . shift ( ) ! . reject ( err ) ;
82+ #onErrorReply ( err : ErrorReply ) {
83+ this . #waitingForReply . shift ( ) ! . reject ( err ) ;
8484 }
8585
86- private _onPush ( push : Array < any > ) {
86+ #onPush ( push : Array < any > ) {
8787 // TODO: type
88- if ( this . _pubSub . handleMessageReply ( push ) ) return true ;
88+ if ( this . #pubSub . handleMessageReply ( push ) ) return true ;
8989
9090 const isShardedUnsubscribe = PubSub . isShardedUnsubscribe ( push ) ;
91- if ( isShardedUnsubscribe && ! this . _waitingForReply . length ) {
91+ if ( isShardedUnsubscribe && ! this . #waitingForReply . length ) {
9292 const channel = push [ 1 ] . toString ( ) ;
93- this . _onShardedChannelMoved (
93+ this . #onShardedChannelMoved (
9494 channel ,
95- this . _pubSub . removeShardedListeners ( channel )
95+ this . #pubSub . removeShardedListeners ( channel )
9696 ) ;
9797 return true ;
9898 } else if ( isShardedUnsubscribe || PubSub . isStatusReply ( push ) ) {
99- const head = this . _waitingForReply . head ! . value ;
99+ const head = this . #waitingForReply . head ! . value ;
100100 if (
101101 ( Number . isNaN ( head . channelsCounter ! ) && push [ 2 ] === 0 ) ||
102102 -- head . channelsCounter ! === 0
103103 ) {
104- this . _waitingForReply . shift ( ) ! . resolve ( ) ;
104+ this . #waitingForReply . shift ( ) ! . resolve ( ) ;
105105 }
106106 return true ;
107107 }
108108 }
109109
110- private _getTypeMapping ( ) {
111- return this . _waitingForReply . head ! . value . typeMapping ?? { } ;
110+ #getTypeMapping ( ) {
111+ return this . #waitingForReply . head ! . value . typeMapping ?? { } ;
112112 }
113113
114- private _initiateResp3Decoder ( ) {
114+ #initiateResp3Decoder ( ) {
115115 return new Decoder ( {
116- onReply : reply => this . _onReply ( reply ) ,
117- onErrorReply : err => this . _onErrorReply ( err ) ,
116+ onReply : reply => this . #onReply ( reply ) ,
117+ onErrorReply : err => this . #onErrorReply ( err ) ,
118118 onPush : push => {
119- if ( ! this . _onPush ( push ) ) {
119+ if ( ! this . #onPush ( push ) ) {
120120
121121 }
122122 } ,
123- getTypeMapping : ( ) => this . _getTypeMapping ( )
123+ getTypeMapping : ( ) => this . #getTypeMapping ( )
124124 } ) ;
125125 }
126126
127- private _initiateResp2Decoder ( ) {
127+ #initiateResp2Decoder ( ) {
128128 return new Decoder ( {
129129 onReply : reply => {
130- if ( this . _pubSub . isActive && Array . isArray ( reply ) ) {
131- if ( this . _onPush ( reply ) ) return ;
130+ if ( this . #pubSub . isActive && Array . isArray ( reply ) ) {
131+ if ( this . #onPush ( reply ) ) return ;
132132
133133 if ( PONG . equals ( reply [ 0 ] as Buffer ) ) {
134- const { resolve, typeMapping } = this . _waitingForReply . shift ( ) ! ,
134+ const { resolve, typeMapping } = this . #waitingForReply . shift ( ) ! ,
135135 buffer = ( ( reply [ 1 ] as Buffer ) . length === 0 ? reply [ 0 ] : reply [ 1 ] ) as Buffer ;
136136 resolve ( typeMapping ?. [ RESP_TYPES . SIMPLE_STRING ] === Buffer ? buffer : buffer . toString ( ) ) ;
137137 return ;
138138 }
139139 }
140140
141- this . _onReply ( reply ) ;
141+ this . #onReply ( reply ) ;
142142 } ,
143- onErrorReply : err => this . _onErrorReply ( err ) ,
143+ onErrorReply : err => this . #onErrorReply ( err ) ,
144144 // PUSH type does not exist in RESP2
145145 // PubSub is handled in onReply
146146 // @ts -expect-error
147147 onPush : undefined ,
148148 getTypeMapping : ( ) => {
149149 // PubSub push is an Array in RESP2
150- return this . _pubSub . isActive ?
150+ return this . #pubSub . isActive ?
151151 RESP2_PUSH_TYPE_MAPPING :
152- this . _getTypeMapping ( ) ;
152+ this . #getTypeMapping ( ) ;
153153 }
154154 } ) ;
155155 }
@@ -180,7 +180,7 @@ export default class RedisCommandsQueue {
180180 options ?: CommandOptions ,
181181 resolveOnWrite ?: boolean
182182 ) : Promise < T > {
183- if ( this . _maxLength && this . _toWrite . length + this . _waitingForReply . length >= this . _maxLength ) {
183+ if ( this . #maxLength && this . #toWrite . length + this . #waitingForReply . length >= this . #maxLength ) {
184184 return Promise . reject ( new Error ( 'The queue is full' ) ) ;
185185 } else if ( options ?. abortSignal ?. aborted ) {
186186 return Promise . reject ( new AbortError ( ) ) ;
@@ -204,14 +204,14 @@ export default class RedisCommandsQueue {
204204 value . abort = {
205205 signal,
206206 listener : ( ) => {
207- this . _toWrite . remove ( node ) ;
207+ this . #toWrite . remove ( node ) ;
208208 value . reject ( new AbortError ( ) ) ;
209209 }
210210 } ;
211211 signal . addEventListener ( 'abort' , value . abort . listener , { once : true } ) ;
212212 }
213213
214- node = this . _toWrite . add ( value , options ?. asap ) ;
214+ node = this . #toWrite . add ( value , options ?. asap ) ;
215215 } ) ;
216216 }
217217
@@ -221,8 +221,8 @@ export default class RedisCommandsQueue {
221221 listener : PubSubListener < T > ,
222222 returnBuffers ?: T
223223 ) {
224- return this . _addPubSubCommand (
225- this . _pubSub . subscribe ( type , channels , listener , returnBuffers )
224+ return this . #addPubSubCommand (
225+ this . #pubSub . subscribe ( type , channels , listener , returnBuffers )
226226 ) ;
227227 }
228228
@@ -232,17 +232,17 @@ export default class RedisCommandsQueue {
232232 listener ?: PubSubListener < T > ,
233233 returnBuffers ?: T
234234 ) {
235- return this . _addPubSubCommand (
236- this . _pubSub . unsubscribe ( type , channels , listener , returnBuffers )
235+ return this . #addPubSubCommand (
236+ this . #pubSub . unsubscribe ( type , channels , listener , returnBuffers )
237237 ) ;
238238 }
239239
240240 resubscribe ( ) : Promise < any > | undefined {
241- const commands = this . _pubSub . resubscribe ( ) ;
241+ const commands = this . #pubSub . resubscribe ( ) ;
242242 if ( ! commands . length ) return ;
243243
244244 return Promise . all (
245- commands . map ( command => this . _addPubSubCommand ( command , true ) )
245+ commands . map ( command => this . #addPubSubCommand ( command , true ) )
246246 ) ;
247247 }
248248
@@ -251,26 +251,26 @@ export default class RedisCommandsQueue {
251251 channel : string ,
252252 listeners : ChannelListeners
253253 ) {
254- return this . _addPubSubCommand (
255- this . _pubSub . extendChannelListeners ( type , channel , listeners )
254+ return this . #addPubSubCommand (
255+ this . #pubSub . extendChannelListeners ( type , channel , listeners )
256256 ) ;
257257 }
258258
259259 extendPubSubListeners ( type : PubSubType , listeners : PubSubTypeListeners ) {
260- return this . _addPubSubCommand (
261- this . _pubSub . extendTypeListeners ( type , listeners )
260+ return this . #addPubSubCommand (
261+ this . #pubSub . extendTypeListeners ( type , listeners )
262262 ) ;
263263 }
264264
265265 getPubSubListeners ( type : PubSubType ) {
266- return this . _pubSub . getTypeListeners ( type ) ;
266+ return this . #pubSub . getTypeListeners ( type ) ;
267267 }
268268
269- private _addPubSubCommand ( command : PubSubCommand , asap = false ) {
269+ #addPubSubCommand ( command : PubSubCommand , asap = false ) {
270270 if ( command === undefined ) return ;
271271
272272 return new Promise < void > ( ( resolve , reject ) => {
273- this . _toWrite . add ( {
273+ this . #toWrite . add ( {
274274 args : command . args ,
275275 chainId : undefined ,
276276 abort : undefined ,
@@ -290,23 +290,23 @@ export default class RedisCommandsQueue {
290290 }
291291
292292 isWaitingToWrite ( ) {
293- return this . _toWrite . length > 0 ;
293+ return this . #toWrite . length > 0 ;
294294 }
295295
296296 * commandsToWrite ( ) {
297- let toSend = this . _toWrite . shift ( ) ;
297+ let toSend = this . #toWrite . shift ( ) ;
298298 while ( toSend ) {
299299 let encoded : CommandArguments ;
300300 try {
301301 encoded = encodeCommand ( toSend . args ) ;
302302 } catch ( err ) {
303303 toSend . reject ( err ) ;
304- toSend = this . _toWrite . shift ( ) ;
304+ toSend = this . #toWrite . shift ( ) ;
305305 continue ;
306306 }
307307
308308 if ( toSend . abort ) {
309- RedisCommandsQueue . _removeAbortListener ( toSend ) ;
309+ RedisCommandsQueue . #removeAbortListener ( toSend ) ;
310310 toSend . abort = undefined ;
311311 }
312312
@@ -316,68 +316,68 @@ export default class RedisCommandsQueue {
316316 // TODO reuse `toSend` or create new object?
317317 ( toSend as any ) . args = undefined ;
318318
319- this . _chainInExecution = toSend . chainId ;
319+ this . #chainInExecution = toSend . chainId ;
320320 toSend . chainId = undefined ;
321321
322- this . _waitingForReply . push ( toSend ) ;
322+ this . #waitingForReply . push ( toSend ) ;
323323 }
324324
325325 yield encoded ;
326- toSend = this . _toWrite . shift ( ) ;
326+ toSend = this . #toWrite . shift ( ) ;
327327 }
328328 }
329329
330- private _flushWaitingForReply ( err : Error ) : void {
331- for ( const node of this . _waitingForReply ) {
330+ #flushWaitingForReply ( err : Error ) : void {
331+ for ( const node of this . #waitingForReply ) {
332332 node . reject ( err ) ;
333333 }
334- this . _waitingForReply . reset ( ) ;
334+ this . #waitingForReply . reset ( ) ;
335335 }
336336
337- private static _removeAbortListener ( command : CommandToWrite ) {
337+ static #removeAbortListener ( command : CommandToWrite ) {
338338 command . abort ! . signal . removeEventListener ( 'abort' , command . abort ! . listener ) ;
339339 }
340340
341- private static _flushToWrite ( toBeSent : CommandToWrite , err : Error ) {
341+ static #flushToWrite ( toBeSent : CommandToWrite , err : Error ) {
342342 if ( toBeSent . abort ) {
343- RedisCommandsQueue . _removeAbortListener ( toBeSent ) ;
343+ RedisCommandsQueue . #removeAbortListener ( toBeSent ) ;
344344 }
345345
346346 toBeSent . reject ( err ) ;
347347 }
348348
349349 flushWaitingForReply ( err : Error ) : void {
350350 this . decoder . reset ( ) ;
351- this . _pubSub . reset ( ) ;
351+ this . #pubSub . reset ( ) ;
352352
353- this . _flushWaitingForReply ( err ) ;
353+ this . #flushWaitingForReply ( err ) ;
354354
355- if ( ! this . _chainInExecution ) return ;
355+ if ( ! this . #chainInExecution ) return ;
356356
357- while ( this . _toWrite . head ?. value . chainId === this . _chainInExecution ) {
358- RedisCommandsQueue . _flushToWrite (
359- this . _toWrite . shift ( ) ! ,
357+ while ( this . #toWrite . head ?. value . chainId === this . #chainInExecution ) {
358+ RedisCommandsQueue . #flushToWrite (
359+ this . #toWrite . shift ( ) ! ,
360360 err
361361 ) ;
362362 }
363363
364- this . _chainInExecution = undefined ;
364+ this . #chainInExecution = undefined ;
365365 }
366366
367367 flushAll ( err : Error ) : void {
368368 this . decoder . reset ( ) ;
369- this . _pubSub . reset ( ) ;
370- this . _flushWaitingForReply ( err ) ;
371- for ( const node of this . _toWrite ) {
372- RedisCommandsQueue . _flushToWrite ( node , err ) ;
369+ this . #pubSub . reset ( ) ;
370+ this . #flushWaitingForReply ( err ) ;
371+ for ( const node of this . #toWrite ) {
372+ RedisCommandsQueue . #flushToWrite ( node , err ) ;
373373 }
374- this . _toWrite . reset ( ) ;
374+ this . #toWrite . reset ( ) ;
375375 }
376376
377377 isEmpty ( ) {
378378 return (
379- this . _toWrite . length === 0 &&
380- this . _waitingForReply . length === 0
379+ this . #toWrite . length === 0 &&
380+ this . #waitingForReply . length === 0
381381 ) ;
382382 }
383383}
0 commit comments