diff --git a/lib/index.js b/lib/index.js index a7e2227..ab2ce0b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -302,9 +302,15 @@ Reissue.prototype.start = function start(delay) { // set the flag and off we go! self._active = true; - self._nextHandlerId = setTimeout(function _nextInvocation() { + // can't to truthy check since 0 is falsy. if a delay is passed in, then + // schedule it. otherwise, it's synchronous and you can't stop it. + if (typeof delay === 'number') { + self._nextHandlerId = setTimeout(function _nextInvocation() { + self._execute(); + }, realDelay); + } else { self._execute(); - }, realDelay); + } }; diff --git a/test/index.js b/test/index.js index 4f13711..fc08b10 100644 --- a/test/index.js +++ b/test/index.js @@ -341,8 +341,8 @@ describe('Reissue module', function() { }); - it('should not execute first invocation if stop was called', - function(done) { + it('should not execute first invocation if started with delay and stop ' + + 'was called', function(done) { let fired = false; @@ -364,6 +364,30 @@ describe('Reissue module', function() { }); + it('should execute first invocation synchronously if start had no delay ' + + 'and stop was called', function(done) { + + let fired = false; + + const timer = reissue.create({ + func: function(callback) { + fired = true; + return callback(); + }, + interval: 300 + }); + timer.start(); + timer.stop(); + + // because we called start synchronously, we'll have at least one + // invocation (first one). + setTimeout(function() { + assert.isTrue(fired); + return done(); + }, 500); + }); + + it('should emit stop event if reissue is inactive', function(done) { const timer = reissue.create({ func: function(callback) {