@@ -127,7 +127,7 @@ function Subscription(pubsub, options) {
127
127
this . autoAck = util . is ( options . autoAck , 'boolean' ) ? options . autoAck : false ;
128
128
this . closed = false ;
129
129
this . interval = util . is ( options . interval , 'number' ) ? options . interval : 10 ;
130
- this . inProgress = 0 ;
130
+ this . inProgressAckIds = { } ;
131
131
this . maxInProgress =
132
132
util . is ( options . maxInProgress , 'number' ) ? options . maxInProgress : Infinity ;
133
133
this . messageListeners = 0 ;
@@ -243,7 +243,7 @@ Subscription.prototype.startPulling_ = function() {
243
243
var maxResults ;
244
244
245
245
if ( this . maxInProgress < Infinity ) {
246
- maxResults = this . maxInProgress - this . inProgress ;
246
+ maxResults = this . maxInProgress - Object . keys ( this . inProgressAckIds ) . length ;
247
247
}
248
248
249
249
this . pull ( {
@@ -284,16 +284,17 @@ Subscription.prototype.ack = function(ackIds, callback) {
284
284
'At least one ID must be specified before it can be acknowledged.' ) ;
285
285
}
286
286
287
+ ackIds = util . arrayize ( ackIds ) ;
288
+
287
289
var body = {
288
- ackIds : util . arrayize ( ackIds )
290
+ ackIds : ackIds
289
291
} ;
290
292
291
293
callback = callback || util . noop ;
292
294
293
295
var path = this . name + ':acknowledge' ;
294
296
295
297
this . makeReq_ ( 'POST' , path , null , body , function ( ) {
296
- self . inProgress -- ;
297
298
self . refreshPausedStatus_ ( ) ;
298
299
callback . apply ( self , arguments ) ;
299
300
} ) ;
@@ -391,9 +392,10 @@ Subscription.prototype.pull = function(options, callback) {
391
392
}
392
393
393
394
var messages = response . receivedMessages || [ ] ;
394
- messages = messages . map ( Subscription . formatMessage_ ) ;
395
+ messages = messages
396
+ . map ( Subscription . formatMessage_ )
397
+ . map ( self . decorateMessage_ . bind ( self ) ) ;
395
398
396
- self . inProgress += messages . length ;
397
399
self . refreshPausedStatus_ ( ) ;
398
400
399
401
if ( self . autoAck && messages . length !== 0 ) {
@@ -441,9 +443,25 @@ Subscription.prototype.setAckDeadline = function(options, callback) {
441
443
this . makeReq_ ( 'POST' , path , null , body , callback ) ;
442
444
} ;
443
445
446
+ Subscription . prototype . decorateMessage_ = function ( message ) {
447
+ var self = this ;
448
+
449
+ this . inProgressAckIds [ message . ackId ] = true ;
450
+
451
+ message . ack = self . ack . bind ( self , message . ackId ) ;
452
+
453
+ message . skip = function ( ) {
454
+ delete self . inProgressAckIds [ message . ackId ] ;
455
+ self . refreshPausedStatus_ ( ) ;
456
+ } ;
457
+
458
+ return message ;
459
+ } ;
460
+
444
461
Subscription . prototype . refreshPausedStatus_ = function ( ) {
445
462
var isCurrentlyPaused = this . paused ;
446
- this . paused = this . inProgress >= this . maxInProgress ;
463
+ var inProgress = Object . keys ( this . inProgressAckIds ) . length ;
464
+ this . paused = inProgress >= this . maxInProgress ;
447
465
448
466
if ( isCurrentlyPaused && ! this . paused && this . messageListeners > 0 ) {
449
467
this . startPulling_ ( ) ;
0 commit comments