From 291c81138258cd22be3a9bc9b2f1990eacaba634 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 6 Apr 2017 22:34:11 -0700 Subject: add queue.remove --- lib/internal/queue.js | 5 +++++ lib/queue.js | 6 ++++++ mocha_test/queue.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/lib/internal/queue.js b/lib/internal/queue.js index 479eaf8..87d49f4 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -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); + }) + }, process: function () { // Avoid trying to start too many processing operations. This can occur // when callbacks resolve synchronously (#1267). diff --git a/lib/queue.js b/lib/queue.js index 906467c..2d0abb7 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -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. diff --git a/mocha_test/queue.js b/mocha_test/queue.js index cc72c52..77d4710 100644 --- a/mocha_test/queue.js +++ b/mocha_test/queue.js @@ -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(); + } + }); }); -- cgit v1.2.1