Skip to content

Commit

Permalink
Merge pull request #9 from DonutEspresso/GH-8
Browse files Browse the repository at this point in the history
GH-8: enable stopping of first invocation
  • Loading branch information
DonutEspresso authored Jun 14, 2016
2 parents 2c2d88b + d80305f commit ab7d774
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.0.1

- #8: move `active` check into execution path. Ensures that calling `stop()`
stops even the first invocation.
[donutespresso/#8](https://github.com/DonutEspresso/reissue/pull/8)

# 2.0.0

- BREAKING: Fix fuzzy behavior around calling `start(0)`. Passing a value of
Expand Down
13 changes: 7 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ util.inherits(Reissue, events.EventEmitter);
Reissue.prototype._execute = function _execute() {
var self = this;
self._startTime = Date.now();
self._func.apply(self._funcContext, self._funcArgs);

// this last invocation might have been scheduled before user called
// stop(). ensure we're still active before invocation.
if (self._active === true) {
self._func.apply(self._funcContext, self._funcArgs);
}
};


Expand Down Expand Up @@ -150,11 +155,7 @@ Reissue.prototype._done = function _done(err) {
// assign the handler to ensure that consumers can't call start() again
// while we're waiting for the next invocation.
setTimeout(function _nextInvocation() {
// this last invocation might have been scheduled before user called
// stop(). ensure we're still active before invocation.
if (self._active !== false) {
self._execute();
}
self._execute();
}, timeToInvocation);
};

Expand Down
28 changes: 27 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ describe('Reissue module', function() {
timer.start(150);
});


it('should start asynchronously after explicit 0 delay', function(done) {

var async = false;
Expand All @@ -349,12 +350,37 @@ describe('Reissue module', function() {

// if async === false, this was called synchronously
assert.equal(async, true);
callback();
return done();
// no need to call reissue's callback here, as that will just
// invoke this function again and call done() which will fail
// the test.
},
interval: 100
});
timer.start(0);
async = true;
});


it('should not execute first invocation if stop was called',
function(done) {

var fired = false;

var timer = reissue.create({
func: function(callback) {
fired = true;
return callback();
},
interval: 300
});
timer.start(300);
timer.stop();

// because we called stop, reissue should never fire
setTimeout(function() {
assert.isFalse(fired);
return done();
}, 500);
});
});

0 comments on commit ab7d774

Please sign in to comment.