From 4da7858bcc85a55e3033b56a69802ebd0e1e0296 Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Mon, 9 Sep 2013 23:15:53 +0100 Subject: [PATCH 1/2] @aiten Allow the delay to be incremented between iterations so that the queue can be throttled --- lib/tqueue.js | 28 ++++++++++++++++++---------- test/tqueue.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/lib/tqueue.js b/lib/tqueue.js index 32e753b..020edce 100644 --- a/lib/tqueue.js +++ b/lib/tqueue.js @@ -24,22 +24,26 @@ util.inherits(TQueue, EventEmitter); TQueue.prototype.push = function(item) { var self = this; + if (self.empty()) { + setTimeout(self.curry(self), this.delay); + this.emit('start'); + } + this._items.push(item); this.emit('push', item); - if(this._intervalId == 0) { - this._intervalId = setInterval(function(){self.pop()}, this.delay); - this.emit('start'); - } }; -TQueue.prototype.pop = function() { - if(!this.empty()) { - this.emit('pop', (this.fifo ? this._items.shift() : this._items.pop())); +TQueue.prototype.curry = function(obj) { + return function() { obj.pop(obj) } +}; + +TQueue.prototype.pop = function(self) { + if(!self.empty()) { + self.emit('pop', (self.fifo ? self._items.shift() : self._items.pop())); + setTimeout(self.curry(self), self.delay); } else { - clearInterval(this._intervalId); - this._intervalId = 0; - this.emit('empty'); + self.emit('empty'); } }; @@ -51,4 +55,8 @@ TQueue.prototype.empty = function() { return (this.length() == 0); }; +TQueue.prototype.setDelay = function(delay) { + this.delay = delay; +} + module.exports = TQueue; \ No newline at end of file diff --git a/test/tqueue.js b/test/tqueue.js index 6ad038f..5b240ab 100644 --- a/test/tqueue.js +++ b/test/tqueue.js @@ -71,4 +71,40 @@ describe('tqueue', function() { } }); }); + + describe('#variable delay', function() { + it('should return variable delay in increments of ~1000', function(done) { + this.timeout(3000); + + var q = new TQueue(); + var i = 0; + var d = 0; + + q.push(1); + q.push(2); + q.push(3); + + q.on('pop', function() { + switch(i) { + case 0: + d = Date.now(); + i++; + break; + case 1: + d = Date.now() - d; + d.should.be.within(995, 1005); + done(); + break; + case 2: + d = Date.now() - d; + d.should.be.within(1995, 2005); + done(); + break; + } + }); + }); + }); + + + }); \ No newline at end of file From 23086065abbef0ac64a2ef18cb494f92a0ee2130 Mon Sep 17 00:00:00 2001 From: Antony Jones Date: Tue, 10 Sep 2013 22:04:35 +0100 Subject: [PATCH 2/2] @aiten variable unit test cleanup and re-work --- test/tqueue.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/test/tqueue.js b/test/tqueue.js index 5b240ab..f1b4d45 100644 --- a/test/tqueue.js +++ b/test/tqueue.js @@ -74,7 +74,7 @@ describe('tqueue', function() { describe('#variable delay', function() { it('should return variable delay in increments of ~1000', function(done) { - this.timeout(3000); + this.timeout(10000); var q = new TQueue(); var i = 0; @@ -83,24 +83,36 @@ describe('tqueue', function() { q.push(1); q.push(2); q.push(3); + q.push(4); q.on('pop', function() { switch(i) { case 0: d = Date.now(); - i++; + q.delay.should.equal(1000); break; case 1: - d = Date.now() - d; - d.should.be.within(995, 1005); - done(); + e = Date.now() - d; + d = Date.now(); + e.should.be.within(995, 1005); + q.delay.should.equal(1000); + q.setDelay(2000); break; case 2: - d = Date.now() - d; - d.should.be.within(1995, 2005); + e = Date.now() - d; + d = Date.now(); + e.should.be.within(1995, 2005); + q.delay.should.equal(2000); + q.setDelay(3000); + break; + case 3: + e = Date.now() - d; + e.should.be.within(2995, 3005); + q.delay.should.equal(3000); done(); break; } + i++; }); }); });