diff --git a/index.js b/index.js index 463a719..646611b 100644 --- a/index.js +++ b/index.js @@ -28,9 +28,9 @@ module.exports = function(opts) { ///// function setFlushInterval() { - clearTimeout(_timeout); + clearInterval(_timeout); if (_flushInterval < Infinity) { - _timeout = setTimeout(flush, _flushInterval); + _timeout = setInterval(flush, _flushInterval); } } diff --git a/index.test.js b/index.test.js index 7379b55..2fd6b0a 100644 --- a/index.test.js +++ b/index.test.js @@ -34,6 +34,35 @@ describe(__filename, function() { }, 250); }); + + it('Should flush continuously', function(done) { + var flush = sinon.stub(); + + var x51 = mod({ + flush: flush, + flushInterval: 50 + }); + + x51.push({}); + + setTimeout(function() { + expect(flush.callCount).to.eql(1); + var items = flush.getCall(0).args[0]; + expect(items.length).to.eql(1); + + x51.push({}); + x51.push({}); + + // Should flush a second time + setTimeout(function() { + expect(flush.callCount).to.eql(2); + var items = flush.getCall(1).args[0]; + expect(items.length).to.eql(2); + + done(); + }, 60); + }, 70); + }); it('Should not lose records if flush throws an error', function() { var flush = sinon.stub(); diff --git a/package.json b/package.json index 1ee9661..b0f1a04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "x51", - "version": "2.0.0", + "version": "2.0.1", "description": "Utility for aggregating and flushing items to be processed", "main": "index.js", "scripts": {