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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion lib/internal/DoublyLinkedList.js
Original file line number Diff line number Diff line change
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.filter = function (testFn) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this destructive? If so I'd rather not call it filter, but remove

Copy link
Collaborator Author

@aearly aearly Apr 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to call it filter so that if someone wants to swap out the implementation of _tasks with an array, they could. e.g.:

var q = async.queue(worker);
q._tasks = [];

I remember we got an issue a while back after 2.0 because some people were doing custom splicing in the queue, which they couldn't do with the linked list.

It is strange that it doesn't return a new list (destructive) even though it's called filter, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually after more thought, remove is a better name. If someone is using an array implementation, they're likely doing their own array splicing somehow anyway.

var curr = this.head;
while(!!curr) {
var next = curr.next;
if (!testFn(curr)) {
this.removeLink(curr);
}
curr = next;
}
return this;
}
5 changes: 5 additions & 0 deletions lib/internal/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export default function queue(worker, concurrency, payload) {
unshift: function (data, callback) {
_insert(data, true, callback);
},
remove: function (testFn) {
q._tasks = q._tasks.filter(function (node) {
return !testFn(node);
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing semi

},
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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('filter', function() {
var list = new DLL();

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

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

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

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

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

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

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

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

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

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

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

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

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

list.filter(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
Original file line number Diff line number Diff line change
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();
}
});
});