@@ -110,9 +110,9 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
110
110
} ) ;
111
111
}
112
112
113
- public deleteMessage ( msg : Message ) : Promise < void > {
113
+ public async deleteMessage ( msg : Message ) : Promise < void > {
114
114
if ( ! msg . raw ) {
115
- return Promise . reject ( new Error ( 'Squiss.deleteMessage requires a Message object' ) ) ;
115
+ throw new Error ( 'Squiss.deleteMessage requires a Message object' ) ;
116
116
}
117
117
const promise = new Promise < void > ( ( resolve , reject ) => {
118
118
this . _delQueue . set ( msg . raw . MessageId ! ,
@@ -126,11 +126,11 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
126
126
clearTimeout ( this . _delTimer ) ;
127
127
this . _delTimer = undefined ;
128
128
}
129
- this . _deleteXMessages ( this . _opts . deleteBatchSize ) ;
129
+ await this . _deleteXMessages ( this . _opts . deleteBatchSize ) ;
130
130
} else if ( ! this . _delTimer ) {
131
- this . _delTimer = setTimeout ( ( ) => {
131
+ this . _delTimer = setTimeout ( async ( ) => {
132
132
this . _delTimer = undefined ;
133
- this . _deleteXMessages ( ) ;
133
+ await this . _deleteXMessages ( ) ;
134
134
} , this . _opts . deleteWaitMs ) ;
135
135
}
136
136
return promise ;
@@ -312,18 +312,19 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
312
312
return this . _startPoller ( ) ;
313
313
}
314
314
315
- public stop ( soft ?: boolean , timeout ?: number ) : Promise < boolean > {
315
+ public async stop ( soft ?: boolean , timeout ?: number ) : Promise < boolean > {
316
316
if ( ! soft && this . _activeReq ) {
317
317
this . _activeReq . abort ( ) ;
318
318
}
319
319
this . _running = this . _paused = false ;
320
320
if ( ! this . _inFlight ) {
321
- return Promise . resolve ( true ) ;
321
+ await this . _drainDeleteQueue ( ) ;
322
+ return true ;
322
323
}
323
324
let resolved = false ;
324
325
let timer : any ;
325
- return new Promise ( ( resolve ) => {
326
- this . on ( 'drained' , ( ) => {
326
+ const result = await new Promise < boolean > ( async ( resolve ) => {
327
+ this . on ( 'drained' , ( ) => {
327
328
if ( ! resolved ) {
328
329
resolved = true ;
329
330
if ( timer ) {
@@ -338,6 +339,8 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
338
339
resolve ( false ) ;
339
340
} , timeout ) : undefined ;
340
341
} ) ;
342
+ await this . _drainDeleteQueue ( ) ;
343
+ return result ;
341
344
}
342
345
343
346
public getS3 ( ) : S3 {
@@ -347,6 +350,14 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
347
350
return this . _s3 ;
348
351
}
349
352
353
+ private async _drainDeleteQueue ( ) : Promise < void > {
354
+ if ( this . _delTimer ) {
355
+ clearTimeout ( this . _delTimer ) ;
356
+ this . _delTimer = undefined ;
357
+ }
358
+ await this . _deleteXMessages ( ) ;
359
+ }
360
+
350
361
private _initS3 ( ) {
351
362
if ( this . _opts . S3 ) {
352
363
if ( typeof this . _opts . S3 === 'function' ) {
@@ -500,15 +511,15 @@ export class Squiss extends (EventEmitter as new() => SquissEmitter) {
500
511
} )
501
512
}
502
513
503
- private _deleteXMessages ( x ?: number ) {
514
+ private async _deleteXMessages ( x ?: number ) : Promise < void > {
504
515
const delQueue = this . _delQueue ;
505
516
const iterator = delQueue . entries ( ) ;
506
517
const delBatch = Array . from ( { length : x || delQueue . size } , function ( this : typeof iterator ) {
507
518
const element = this . next ( ) . value ;
508
519
delQueue . delete ( element [ 0 ] ) ;
509
520
return element [ 1 ] ;
510
521
} , iterator ) ;
511
- this . _deleteMessages ( delBatch ) ;
522
+ await this . _deleteMessages ( delBatch ) ;
512
523
}
513
524
514
525
private _isLargeMessage ( message : ISendMessageRequest , minSize ?: number ) : Promise < boolean > {
0 commit comments