@@ -245,12 +245,7 @@ export class MatrixScheduler<T = ISendEventResponse> {
245
245
// get head of queue
246
246
const obj = this . peekNextEvent ( queueName ) ;
247
247
if ( ! obj ) {
248
- // queue is empty. Mark as inactive and stop recursing.
249
- const index = this . activeQueues . indexOf ( queueName ) ;
250
- if ( index >= 0 ) {
251
- this . activeQueues . splice ( index , 1 ) ;
252
- }
253
- debuglog ( "Stopping queue '%s' as it is now empty" , queueName ) ;
248
+ this . disableQueue ( queueName ) ;
254
249
return ;
255
250
}
256
251
debuglog ( "Queue '%s' has %s pending events" , queueName , this . queues [ queueName ] . length ) ;
@@ -289,17 +284,32 @@ export class MatrixScheduler<T = ISendEventResponse> {
289
284
// give up (you quitter!)
290
285
debuglog ( "Queue '%s' giving up on event %s" , queueName , obj . event . getId ( ) ) ;
291
286
// remove this from the queue
292
- this . removeNextEvent ( queueName ) ;
293
- obj . defer . reject ( err ) ;
294
- // process next event
295
- this . processQueue ( queueName ) ;
287
+ this . clearQueue ( queueName , err ) ;
296
288
} else {
297
289
setTimeout ( this . processQueue , waitTimeMs , queueName ) ;
298
290
}
299
291
} ,
300
292
) ;
301
293
} ;
302
294
295
+ private disableQueue ( queueName : string ) : void {
296
+ // queue is empty. Mark as inactive and stop recursing.
297
+ const index = this . activeQueues . indexOf ( queueName ) ;
298
+ if ( index >= 0 ) {
299
+ this . activeQueues . splice ( index , 1 ) ;
300
+ }
301
+ debuglog ( "Stopping queue '%s' as it is now empty" , queueName ) ;
302
+ }
303
+
304
+ private clearQueue ( queueName : string , err : unknown ) : void {
305
+ debuglog ( "clearing queue '%s'" , queueName ) ;
306
+ let obj : IQueueEntry < T > | undefined ;
307
+ while ( ( obj = this . removeNextEvent ( queueName ) ) ) {
308
+ obj . defer . reject ( err ) ;
309
+ }
310
+ this . disableQueue ( queueName ) ;
311
+ }
312
+
303
313
private peekNextEvent ( queueName : string ) : IQueueEntry < T > | undefined {
304
314
const queue = this . queues [ queueName ] ;
305
315
if ( ! Array . isArray ( queue ) ) {
0 commit comments