Skip to content

Commit

Permalink
accept optional callback on stopping the interval
Browse files Browse the repository at this point in the history
  • Loading branch information
DonutEspresso committed Jan 21, 2017
1 parent 8644d15 commit 0b5896f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
40 changes: 38 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ function Reissue(opts) {
opts.args.concat(boundDone) : [boundDone];


/**
* stop callback. a callback to invoke when the final iteration of a
* task is complete.
* @type {Function}
*/
self._stopCallback = null;


//--------------------------------------------------------------------------
// internal properties
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -117,6 +125,10 @@ Reissue.prototype._execute = function _execute() {
if (self._active === true) {
self._func.apply(self._funcContext, self._funcArgs);
}
// if we're not active, call the callback and we're done.
else if (self._stopCallback) {
self._stopCallback();
}
};


Expand All @@ -141,7 +153,11 @@ Reissue.prototype._done = function _done(err) {
}

// if user called stop(), we're done! don't queue up another invocation.
// call the user callback and return.
if (self._active === false) {
if (typeof self._stopCallback === 'function') {
self._stopCallback();
}
return;
}

Expand Down Expand Up @@ -199,11 +215,31 @@ Reissue.prototype.start = function start(delay) {
* manually stop the exeuction queue.
* @public
* @method stop
* @param {Function} [callback] an optional callback fn
* @return {undefined}
*/
Reissue.prototype.stop = function stop() {
Reissue.prototype.stop = function stop(callback) {

assert.optionalFunc(callback, 'callback');

var self = this;
self._active = false;

// if not currently active, do nothing. if callback available call it.
if (self._active === false) {
if (callback) {
self._stopCallback = callback;
callback();
}
} else {
// if active, set active flag to false.
self._active = false;

// save the callback for invocation.
if (callback) {
self._stopCallback = callback;
}
}

};


Expand Down
64 changes: 64 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,68 @@ describe('Reissue module', function() {
return done();
}, 500);
});


it('should call callback if stop called when already inactive',
function(done) {

var timer = reissue.create({
func: function(callback) {
return callback();
},
interval: 100
});
timer.stop(done);
});


it('should call stop callback on completion when stop is called pre ' +
'function invocation', function(done) {

var out = [];
var i = 0;

var timer = reissue.create({
func: function(callback) {
out.push(i);
i += 1;
return callback();
},
interval: 100
});
timer.start();

setTimeout(function() {
timer.stop(function() {
assert.deepEqual(out, [0,1,2]);
return done();
});
}, 300);
});


it.only('should call stop callback on completion when stop is called mid ' +
'function invocation', function(done) {

var count = 0;

// attempt run this only through one cycle
var timer = reissue.create({
func: function(callback) {
count++;
return setTimeout(callback, 1000);
},
interval: 1000
});


setImmediate(function() {
timer.stop(function() {
assert.equal(count, 1);
return done();
});
});

timer.start();
});
});

0 comments on commit 0b5896f

Please sign in to comment.