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

js-queue

v2.0.2

Published

Simple JS queue with auto run for node and browsers

Downloads

2,386,775

Readme

js-queue Is Great for any queue

  1. socket message queuing
  2. async operations
  3. synchronous operations
  4. atomic operations
  5. code with requirements before executing
  6. queues you want to start running any time you add new items
  7. any simple or complex queue operations
  8. base class to extend
  9. anything else that needs a queue
  10. Anything which needs a stack instead of a queue.

Stable and easy to use

Works great in node.js, webpack, browserify, or any other commonjs loader or compiler. To use in plain old vanilla browser javascript without common js just replace the requires in the examples with script tags. We show that below too.

js-queue also exposes the easy-stack stack via require('js-queue/stack.js') this file exposes an ES6 stack which allows for Last In First Out (LIFO) queuing. This can come in handy depending on your application needs, check out the easy-stack javascript documentation it follows the js-queue interface but is node 6 or greater as it uses ES6 classes.

npm install js-queue

npm info : See npm trends and stats for js-queue
js-queue npm version supported node version for js-queue total npm downloads for js-queue monthly npm downloads for js-queue npm licence for js-queue

RIAEvangelist

GitHub info :
js-queue GitHub Release GitHub license js-queue license open issues for js-queue on GitHub

Package details websites :

This work is licenced via the MIT Licence.

Exposed methods and values

|key|type|paramaters|default|description| |----|----|----|----|----| |add|function|any number of functions| |adds all parameter functions to queue and starts execution if autoRun is true, queue is not already running and queue is not forcibly stopped | |next|function| | |executes next item in queue if queue is not forcibly stopped| |clear|function| | |removes remaining items in the queue| |contents|Array| | | Queue instance contents | |autoRun|Bool| | true |should autoRun queue when new item added| |stop|Bool| | false |setting this to true will forcibly prevent the queue from executing|

Basic queue use in node, react, browserify, webpack or any other commonjs implementation


    var Queue=require('js-queue');
    //create a new queue instance
    var queue=new Queue;

    for(var i=0; i<50; i++){
        //add a bunch of stuff to the queue
        queue.add(makeRequest);
    }

    function makeRequest(){
        //do stuff
        console.log('making some request');

        this.next();
    }

Basic browser use

The only difference is including via a script tag instead of using require.


    <html>
            <head>
                    <!-- this is the only difference -->
                    <script src='./queue-vanilla.js'></script>
                    <script>
                            console.log('my awesome app script');
                            var queue=new Queue;

                            for(var i=0; i<50; i++){
                                queue.add(makeRequest);
                            }

                            function makeRequest(){
                                console.log('making some request');

                                this.next();
                            }
                    </script>
            </head>
            <body>
            </body>
    </html>

Basic use with websockets in node, react, browserify, webpack or any other commonjs implementation

This allows you to start adding requests immediately and only execute if the websocket is connected. To use in plain browser based JS without webpack or browserify just replace the requires with the script tag.


    var Queue=require('js-queue');

    //ws-share just makes it easier to share websocket code and ensure you don't open a websocket more than once
    var WS=require('ws-share');

    //js-message makes it easy to create and parse normalized JSON messages.
    var Message=require('js-message');

    //create a new queue instance
    var queue=new Queue;

    //force stop until websocket opened
    queue.stop=true;

    var ws=null;

    function startWS(){
        //websocket.org rocks
        ws=new WS('wss://echo.websocket.org/?encoding=text');

        ws.on(
            'open',
            function(){
                ws.on(
                    'message',
                    handleResponse
                );

                //now that websocket is opened allow auto execution
                queue.stop=false;
                queue.next();
            }
        );

        ws.on(
            'error',
            function(err){
                //stop execution of queue if there is an error because the websocket is likely closed
                queue.stop=true;
                //remove remaining items in the queue
                queue.clear();
                throw(err);
            }
        );

        ws.on(
            'close',
            function(){
                //stop execution of queue when the websocket closed
                queue.stop=true;
            }
        );
    }

    //simulate a lot of requests being queued up for the websocket
    for(var i=0; i<50; i++){
        queue.add(makeRequest);
    }

    var messageID=0;

    function handleResponse(e){
        var message=new Message;
        message.load(e.data);

        console.log(message.type,message.data);
    }

    function makeRequest(){
        messageID++;
        var message=new Message;
        message.type='testMessage';
        message.data=messageID;

        ws.send(message.JSON);

        this.next();
    }

    startWS();

Extending Queue


    var Queue=require('js-queue');

    //MyAwesomeQueue inherits from Queue
    MyAwesomeQueue.prototype = new Queue;
    //Constructor will extend Queue
    MyAwesomeQueue.prototype.constructor = MyAwesomeQueue;

    function MyAwesomeQueue(){
        //extend with some stuff your app needs,
        //maybe npm publish your extention with js-queue as a dependancy?
        Object.defineProperties(
            this,
            {
                isStopped:{
                    enumerable:true,
                    get:checkStopped,
                    set:checkStopped
                },
                removeThirdItem:{
                    enumerable:true,
                    writable:false,
                    value:removeThird
                }
            }
        );

        //enforce Object.assign for extending by locking down Class structure
        //no willy nilly cowboy coding
        Object.seal(this);

        function checkStopped(){
            return this.stop;
        }

        function removeThird(){
            //get the queue content
            var list=this.contents;
            //modify the queue content
            list.splice(2,1);
            //save the modified queue content
            this.contents=list;

            return this.contents;
        }
    }