From 567a3a26f92ec32843dc6a788c4a6f8705d88646 Mon Sep 17 00:00:00 2001 From: MikeyBurkman Date: Thu, 21 Jul 2016 20:45:08 -0400 Subject: [PATCH] Fix issue with flush not repeating --- index.js | 4 ++-- index.test.js | 29 +++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) 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": {