Skip to content

Commit

Permalink
Use events instead of passing in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leanderlee committed Mar 6, 2016
1 parent 813b546 commit c0ad8e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
15 changes: 4 additions & 11 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ function Queue(process, opts) {
}

opts = opts || {};

self.empty = opts.empty || function(){};
self.drain = opts.drain || function(){};

self.process = opts.process || function (task, cb) { cb(null, {}) };
self.filter = opts.filter || function (input, cb) { cb(null, input) };
Expand Down Expand Up @@ -198,19 +195,15 @@ Queue.prototype._queueTask = function (taskId, task, ticket) {

Queue.prototype._emptied = function () {
if (this._calledEmpty) return;
if (typeof this.empty === 'function') {
this._calledEmpty = true;
this.empty();
}
this._calledEmpty = true;
this.emit('empty');
}

Queue.prototype._drained = function () {
this._emptied();
if (this._calledDrain) return;
if (typeof this.drain === 'function') {
this._calledDrain = true;
this.drain();
}
this._calledDrain = true;
this.emit('drain');
}

Queue.prototype._getNextBatch = function (cb) {
Expand Down
15 changes: 7 additions & 8 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ describe('Basic Queue', function() {

it('should drain and empty', function (done) {
var emptied = false;
var q = new Queue(function (n, cb) { cb() }, {
empty: function () {
emptied = true;
},
drain: function () {
assert.ok(emptied);
done();
}
var q = new Queue(function (n, cb) { cb() })
q.on('empty', function () {
emptied = true;
})
q.on('drain', function () {
assert.ok(emptied);
done();
});
q.push(1)
q.push(2)
q.push(3)
Expand Down

0 comments on commit c0ad8e0

Please sign in to comment.