From d90d11ce3c9c763d2a2868bca8c54db3d1148d2a Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Fri, 3 Jun 2016 03:22:57 +0200 Subject: [PATCH] Add `queue.drain` method --- README.md | 6 ++++++ lib/index.js | 8 ++++++++ src/index.js | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 757e187..fd191b0 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,12 @@ Adds a task to the queue for the given `type`. Note that this function will __return a Promise__, passing through the result from the task handler. If the Promise from the task handler resolves, then the one returned from this function will resolve with the same value. If the Promise from the task handler is rejected, this one will also reject, with the same error. +### queue.drain(type) + +Drains (ie. empties) the queue for the given `type`. Note that this __will not__ try to stop or 'cancel' *running* tasks; it will simply remove the *upcoming* tasks that are still in the queue. + +* __type__: The name of the task type, as specified in `queue.define`. + ### queue.awaitDrained(type) Returns a Promise that will resolve when the task queue has run out of tasks for a given `type`. Some of the previously queued tasks may still be running, however - this simply signals that there are no *upcoming* tasks scheduled anymore. diff --git a/lib/index.js b/lib/index.js index de73081..9609f13 100644 --- a/lib/index.js +++ b/lib/index.js @@ -177,6 +177,14 @@ module.exports = function createTaskQueue(options) { return deferredPromise; }); }, + drain: function drain(type) { + if (handlers[type] == null) { + throw new TaskQueueError("No such task type exists."); + } + + debugTasks("Draining tasks for '" + type + "'"); + tasks[type] = []; + }, awaitDrained: function awaitDrained(type) { var _this = this; diff --git a/src/index.js b/src/index.js index db45383..caaab59 100644 --- a/src/index.js +++ b/src/index.js @@ -176,6 +176,14 @@ module.exports = function createTaskQueue(options) { return deferredPromise; }) }, + drain: function(type) { + if (handlers[type] == null) { + throw new TaskQueueError("No such task type exists.") + } + + debugTasks(`Draining tasks for '${type}'`); + tasks[type] = []; + }, awaitDrained: function(type) { return Promise.try(() => { if (handlers[type] == null) {