Skip to content

Commit

Permalink
support synchronous start again
Browse files Browse the repository at this point in the history
  • Loading branch information
DonutEspresso committed Jan 24, 2017
1 parent bdf3412 commit f55d3c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/index.js
Expand Up @@ -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);
}
};


Expand Down
28 changes: 26 additions & 2 deletions test/index.js
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down

0 comments on commit f55d3c9

Please sign in to comment.