Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queue.remove() #1397

Merged
merged 3 commits into from Apr 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/internal/DoublyLinkedList.js
Expand Up @@ -23,7 +23,10 @@ DLL.prototype.removeLink = function(node) {
return node;
}

DLL.prototype.empty = DLL;
DLL.prototype.empty = function () {
while(this.head) this.shift();
return this;
};

DLL.prototype.insertAfter = function(node, newNode) {
newNode.prev = node;
Expand Down Expand Up @@ -60,3 +63,26 @@ DLL.prototype.shift = function() {
DLL.prototype.pop = function() {
return this.tail && this.removeLink(this.tail);
};

DLL.prototype.toArray = function () {
var arr = Array(this.length);
var idx = 0;
var curr = this.head;
for(idx = 0; idx < this.length; idx++) {
arr[idx] = curr.data;
curr = curr.next;
}
return arr;
}

DLL.prototype.remove = function (testFn) {
var curr = this.head;
while(!!curr) {
var next = curr.next;
if (testFn(curr)) {
this.removeLink(curr);
}
curr = next;
}
return this;
}
3 changes: 3 additions & 0 deletions lib/internal/queue.js
Expand Up @@ -102,6 +102,9 @@ export default function queue(worker, concurrency, payload) {
unshift: function (data, callback) {
_insert(data, true, callback);
},
remove: function (testFn) {
q._tasks.remove(testFn);
},
process: function () {
// Avoid trying to start too many processing operations. This can occur
// when callbacks resolve synchronously (#1267).
Expand Down
6 changes: 6 additions & 0 deletions lib/queue.js
Expand Up @@ -24,6 +24,12 @@ import wrapAsync from './internal/wrapAsync';
* task in the list. Invoke with `queue.push(task, [callback])`,
* @property {Function} unshift - add a new task to the front of the `queue`.
* Invoke with `queue.unshift(task, [callback])`.
* @property {Function} remove - remove items from the queue that match a test
* function. The test function will be passed an object with a `data` property,
* and a `priority` property, if this is a
* [priorityQueue]{@link module:ControlFlow.priorityQueue} object.
* Invoked with `queue.remove(testFn)`, where `testFn` is of the form
* `function ({data, priority}) {}` and returns a Boolean.
* @property {Function} saturated - a callback that is called when the number of
* running workers hits the `concurrency` limit, and further tasks will be
* queued.
Expand Down
83 changes: 83 additions & 0 deletions mocha_test/linked_list.js
@@ -0,0 +1,83 @@
var DLL = require('../lib/internal/DoublyLinkedList').default;
var expect = require('chai').expect;

describe('DoublyLinkedList', function () {
it('toArray', function() {
var list = new DLL();
expect(list.toArray()).to.eql([]);

for (var i = 0; i < 5; i++) {
list.push({data: i});
}
expect(list.toArray()).to.eql([0, 1, 2, 3, 4]);
});

it('remove', function() {
var list = new DLL();

for (var i = 0; i < 5; i++) {
list.push({data: i});
}

list.remove(function (node) {
return node.data === 3;
})

expect(list.toArray()).to.eql([0, 1, 2, 4]);
});

it('remove (head)', function() {
var list = new DLL();

for (var i = 0; i < 5; i++) {
list.push({data: i});
}

list.remove(function (node) {
return node.data === 0;
})

expect(list.toArray()).to.eql([1, 2, 3, 4]);
});

it('remove (tail)', function() {
var list = new DLL();

for (var i = 0; i < 5; i++) {
list.push({data: i});
}

list.remove(function (node) {
return node.data === 4;
})

expect(list.toArray()).to.eql([0, 1, 2, 3]);
});

it('remove (all)', function() {
var list = new DLL();

for (var i = 0; i < 5; i++) {
list.push({data: i});
}

list.remove(function (node) {
return node.data < 5;
})

expect(list.toArray()).to.eql([]);
});

it('empty', function() {
var list = new DLL();

for (var i = 0; i < 5; i++) {
list.push({data: i});
}

var empty = list.empty();

expect(list).to.equal(empty);
expect(list.toArray()).to.eql([]);
});
});
19 changes: 19 additions & 0 deletions mocha_test/queue.js
Expand Up @@ -761,5 +761,24 @@ describe('queue', function(){
q.push('foo4', function () {calls.push('foo4 cb');});
});
});

it('remove', function(done) {
var result = [];
var q = async.queue(function(data, cb) {
result.push(data);
async.setImmediate(cb);
});

q.push([1, 2, 3, 4, 5]);

q.remove(function (node) {
return node.data === 3;
});

q.drain = function () {
expect(result).to.eql([1, 2, 4, 5]);
done();
}
});
});