diff --git a/lib/index.js b/lib/index.js index 2a96170..7c384dc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -76,15 +76,6 @@ function Reissue(opts) { self._funcArgs = (opts.args) ? 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 //-------------------------------------------------------------------------- @@ -126,8 +117,8 @@ Reissue.prototype._execute = function _execute() { 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(); + else { + self.emit('stop'); } }; @@ -155,9 +146,7 @@ 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(); - } + self.emit('stop'); return; } @@ -215,31 +204,19 @@ 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(callback) { - - assert.optionalFunc(callback, 'callback'); +Reissue.prototype.stop = function stop() { var self = this; - // if not currently active, do nothing. if callback available call it. + // if not currently active, emit stop event right away. if (self._active === false) { - if (callback) { - self._stopCallback = callback; - callback(); - } + self.emit('stop'); } else { - // if active, set active flag to false. + // if currently active, set active flag to false. self._active = false; - - // save the callback for invocation. - if (callback) { - self._stopCallback = callback; - } } - }; diff --git a/test/index.js b/test/index.js index 7e5603e..b568f1c 100644 --- a/test/index.js +++ b/test/index.js @@ -385,8 +385,7 @@ describe('Reissue module', function() { }); - it('should call callback if stop called when already inactive', - function(done) { + it('should emit stop event if reissue is inactive', function(done) { var timer = reissue.create({ func: function(callback) { @@ -394,11 +393,12 @@ describe('Reissue module', function() { }, interval: 100 }); - timer.stop(done); + timer.on('stop', done); + timer.stop(); }); - it('should call stop callback on completion when stop is called pre ' + + it('should emit stop on completion when stop() is called pre ' + 'function invocation', function(done) { var out = []; @@ -412,18 +412,21 @@ describe('Reissue module', function() { }, interval: 100 }); + + timer.on('stop', function() { + assert.deepEqual(out, [0,1,2]); + return done(); + }); + timer.start(); setTimeout(function() { - timer.stop(function() { - assert.deepEqual(out, [0,1,2]); - return done(); - }); + timer.stop(); }, 300); }); - it('should call stop callback on completion when stop is called mid ' + + it('should emit stop event on completion when stop() is called mid ' + 'function invocation', function(done) { var count = 0; @@ -437,12 +440,13 @@ describe('Reissue module', function() { interval: 1000 }); + timer.on('stop', function() { + assert.equal(count, 1); + return done(); + }); setImmediate(function() { - timer.stop(function() { - assert.equal(count, 1); - return done(); - }); + timer.stop(); }); timer.start();