js-queue Is Great for any queue
- socket message queuing
- async operations
- synchronous operations
- atomic operations
- code with requirements before executing
- queues you want to start running any time you add new items
- any simple or complex queue operations
- base class to extend
- anything else that needs a queue
- 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
GitHub info :
Package details websites :
- GitHub.io site. A prettier version of this site.
- NPM Module. The npm page for the js-queue module.
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=; //create a new queue instance var queue=; forvar i=0; i<50; i++ //add a bunch of stuff to the queue queue; { //do stuff console; thisnext; }
Basic browser use
The only difference is including via a script tag instead of using require.
<!-- this is the only difference -->
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=; //ws-share just makes it easier to share websocket code and ensure you don't open a websocket more than once var WS=; //js-message makes it easy to create and parse normalized JSON messages. var Message=; //create a new queue instance var queue=; //force stop until websocket opened queuestop=true; var ws=null; { //websocket.org rocks ws='wss://echo.websocket.org/?encoding=text'; ws; ws; ws; } //simulate a lot of requests being queued up for the websocket forvar i=0; i<50; i++ queue; var messageID=0; { var message=; message; console; } { messageID++; var message=; messagetype='testMessage'; messagedata=messageID; ws; thisnext; } ;
Extending Queue
var Queue=; //MyAwesomeQueue inherits from Queue MyAwesomeQueueprototype = ; //Constructor will extend Queue MyAwesomeQueueprototypeconstructor = MyAwesomeQueue; { //extend with some stuff your app needs, //maybe npm publish your extention with js-queue as a dependancy? Object; //enforce Object.assign for extending by locking down Class structure //no willy nilly cowboy coding Object; { return thisstop; } { //get the queue content var list=thiscontents; //modify the queue content list; //save the modified queue content thiscontents=list; return thiscontents; } }