npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requeued

v1.1.0

Published

Basic module for a Promise based FIFO queue, supports configurable concurrent workers, queue length, and promise engine. Supports push, unshift, shift, and pop, along with retries for failed workers.

Downloads

273

Readme

Overview

Basic module for creating a promise based queue, allows configurable concurrent workers, queue length, and promise engine. Supports push, unshift, shift, pop, start, stop, pause, along with retries for failed workers.

Installation

This library can be installed with npm with the following command:

npm i requeued

A simple delayed retry queue is set up and can be seen by running the test file

npm test

Usage

You can create a minimal single concurrent worker queue by simply requiring the module and instantiating an object

const Queue = require('requeued');

var queue = new Queue();

Add a worker using an old callback async function:

queue.push(() => {
   return new Promise((resolve, reject) => {
       doSomeOldAsyncStuff((err) => {
          if(err) reject(err);
          else resolve();
       });
   });
});

Add a worker to the beginning of the queue using a new async promise style function

queue.unshift(() => {
   return somePromiseEnabledFunction();
});

Add a function that is requred, and should be retried 3 times every 500 ms in case of a failure. e.g. initializing hardware before communication.

queue.push(() => {
    return new Promise((resolve, reject) => {
       initSerialDevice((err) => {
           if(err) reject(rrr);
           else resolve();
       }) ;
    });
}, 3, 500)

Adding a worker returns a Promise object that will be resolved or rejected according to the action of the function. Since the next item in the queue is processed after the current one resolves, you can set up error handler retries at this point also.

function initSomething(portNumber=10001, cb){
    queue.unshift(() => {
        return new Promise((resolve, reject) => {
            if(!we_have_an_error){
                resolve("Yeah! No errors here");
            }else{
                reject(we_have_an_error)
            }
        });
    }).then((data) => {
        //data == "Yeah! No errors here"
        //We could either wrap the queue in another promise so we can return the success to
        //the caller, or in this case, use an old style callback
        cb();
    }).catch((err) => {
        switch(err){
            case 1:
                //change some variable and add this to the queue again
                initSomething(portNumber+1, cb);
                break;
            case 2:
                //maybe the error was a communication timeout, just send it right back through
                 initSomething(portNumber, cb);
                break;
            default:
                //Who knows what this error is? Log it and quit
                console.log(err);
                process.exit();
        }
    });
}

Instance Options

var queue = new Queue(concurrentWorkers = 1, queueLimit = Infinity, promise = Promise);
  • concurrentWorkers [type = int, default = 1]

The number of queued workers allowed to run at once

  • queueLimit [type = int, default = Infinity]

The number of items that may exist in the queue, if an attempt is made to add an item to the queue that would bring the total length over the limit, a promise rejection will be sent immediately

  • promise [type = Promise engine, default = global Promise]

The Promise engine to be used, this allows the queue to be used with bluebird or a promise engine other than the native one.

Methods

queue.push(func, [retries = 0, [waitTimeout = 0]])
queue.unshift(func, [retries = 0, [waitTimeout = 0]])

Push a function to the end of the queue, or unshift to the beginning. Both of these methods will return a Promise object which will either resolve or reject after it has been processed.

  • func [type = callable, return = Promise, required]

Any callable that returns a Promise.

  • retries [type = int, default = 0]

The number of times to retry this function upon failure. The promise returned by the callable will still reject appropriately even if retries are used, but after the given number of retries have all failed.

  • waitTimeout [type = int, required = 0]

If this should be retried, the wait timeout defines how many milliseconds to pause the queue between retries.

queue.any(funcs)

Similar to the Array.prototype.some() method, when sent a list of functions, a Promise will be returned and either resolve with the first successful queued function, or reject with an array of all rejectioned messages.

Note! This method will throw an error if you attempt to call it on a non-empty queue.

  • funcs [type = Array]

Array of callables that return Promises.

queue.shift();
queue.pop();

Remove a function from the beginning or end of the queue. Both of these methods return the new length of the queue.

queue.flush();

Empty the queue. This will not prevent items from being added, or workers that are currently processing from completing.

queue.stop();
queue.start();

Stop/Start processing items in the queue.

queue.pause(t = 0, method = 'unshift');
  • t [type = int, required]

Pause the queue for t milliseconds.

  • method [type = string, default="unshift"]

Where to add the pause, by default it will happen immediately following the completion of any currently processing worker, change method to "push" to add the pause in at the end of the current queue.

Properties

  • queue.length [type = int]

The current number of workers in the queue, this does not include workers that are processing.

  • queue.processing [type = int]

The number of workers that are currently processing.

  • queue.cLimit [type = int]

The number of queued workers allowed to run at once

  • queue.qLimit [type = int]

The number of items that may exist in the queue, if an attempt is made to add an item to the queue that would bring the total length over the limit, a promise rejection will be sent immediately

  • queue.active [type = boolean]

Whether or not the queue is actively processing workers.

  • queue.next [type = callable, return = queued worker, default = Array.prototype.shift]

Advanced! Replace this with your own function (or Array.prototype.pop) to turn this into a LIFO queue. The function will receive the queued workers as the this context