Skip to content

Commit

Permalink
use eventemitter instead of callback
Browse files Browse the repository at this point in the history
  • Loading branch information
DonutEspresso committed Jan 21, 2017
1 parent 4b31b43 commit df06f77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
37 changes: 7 additions & 30 deletions lib/index.js
Expand Up @@ -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
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -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');
}
};

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}

};


Expand Down
30 changes: 17 additions & 13 deletions test/index.js
Expand Up @@ -385,20 +385,20 @@ 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) {
return callback();
},
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 = [];
Expand All @@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit df06f77

Please sign in to comment.