From 7cd9ce8f638848b5219f67c3374459037c5a6da1 Mon Sep 17 00:00:00 2001 From: Matthew Sanders Date: Sun, 18 May 2025 13:53:47 -0600 Subject: [PATCH 1/4] Update readme.md I added an example of how to effectively maintain the list of jobs in a way that is not obvious in the readme. --- readme.md | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 83973b2..b0f850f 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ Asynchronous function queue with adjustable concurrency. This module exports a class `Queue` that implements most of the `Array` API. Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods. Processing begins when you call `q.start()`. -## Example +## Basic Usage Example Do `npm run example` or `npm run dev` and open the example directory (and your console) to run the following program: ``` javascript import Queue from 'queue' @@ -109,6 +109,52 @@ q.start(err => { }) ``` +## Finding and Removing a Specific Job Example + +```javascript +q = new Queue({ + concurrency: 1, + timeout: null, + autostart: false, +}); + +// Fill the queue with jobs. +['job1', 'job2', 'job3', 'job4'].forEach((jobTitle) => { + // Build the job using a promise. + const job = () => { + return new Promise((resolve) => { + // Automatically resolve the job after 10 seconds. + setTimeout(resolve, 10000); + }); + }; + + // Add a custom property to the job so we can search for it later. + job.jobTitle = jobTitle; + + // Add the job to the queue. + q.push(job); +}); + +let jobIndex = null; + +// We want to remove job2. +// Go through the list of jobs to find which has the "jobTitle" property we're looking for. +Object.entries(q.jobs).some(([index, job]) => { + if (job.uid === uid) { + jobIndex = index; + + return true; + } + + return false; +}); + +// Remove this job from the queue if we found its index. +if (jobIndex !== null) { + this.queue.jobs.splice(jobIndex, 1); +} +``` + ## Install ``` From 15426647722b7ade758285e9ea3134a7f2788964 Mon Sep 17 00:00:00 2001 From: Matthew Sanders Date: Sun, 18 May 2025 13:56:15 -0600 Subject: [PATCH 2/4] I fixed a typo. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b0f850f..969ecab 100644 --- a/readme.md +++ b/readme.md @@ -151,7 +151,7 @@ Object.entries(q.jobs).some(([index, job]) => { // Remove this job from the queue if we found its index. if (jobIndex !== null) { - this.queue.jobs.splice(jobIndex, 1); + q.jobs.splice(jobIndex, 1); } ``` From abe2ecf2766112ce6099a68fbef9b5aff27cfb03 Mon Sep 17 00:00:00 2001 From: Matthew Sanders Date: Sun, 18 May 2025 14:08:12 -0600 Subject: [PATCH 3/4] I added three missing properties to the readme. --- readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/readme.md b/readme.md index 969ecab..297e839 100644 --- a/readme.md +++ b/readme.md @@ -215,6 +215,15 @@ Ensures the queue is always running if jobs are available. Useful in situations ### `q.results` An array to set job callback arguments on. +### `q.pending` +The number of jobs currently running. + +### `q.running` +Boolean indicating whether or not the queue is currently running. + +### `q.jobs` +An array of job functions queued to be ran. This does not include the currently running job. + ### `q.length` Jobs pending + jobs to process (readonly). From 5f31e33a59f7ac87dec570e549aaddf41a373b90 Mon Sep 17 00:00:00 2001 From: Matthew Sanders Date: Sun, 1 Jun 2025 11:56:44 -0600 Subject: [PATCH 4/4] I fix an example in the readme. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 297e839..1acea61 100644 --- a/readme.md +++ b/readme.md @@ -140,7 +140,7 @@ let jobIndex = null; // We want to remove job2. // Go through the list of jobs to find which has the "jobTitle" property we're looking for. Object.entries(q.jobs).some(([index, job]) => { - if (job.uid === uid) { + if (job.jobTitle === 'job2') { jobIndex = index; return true;