easy-stack Is Great for any javascript stack (LIFO)
JS Stacks are different from queues because they are LIFO (last in first out) unlike a queue which is FIFO (first in first out). While a Queue executes in order, a stack executes whatever was most recently added to the stack, much like a reading a stack of papers on your desk. If you read half the papers and someone puts more on the top you start with the new ones first.
- socket messages
- priority async operations
- priority synchronous operations
- stacks you want to start running automatically when you add new items
- any simple or complex stack operations
- base class to extend
- anything else that needs a stack
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. Any time you need a JS stack easy-stack is there for you.
require('easy-stack');
for ES6 node.
require('easy-stack/es5.js');
for ES5 node and browser.
npm install easy-stack
npm info : See npm trends and stats for easy-stack
GitHub info :
Package details websites :
This work is licenced via the DBAD Public Licence.
Exposed methods and values
key | type | parameters | default | description |
---|
add | function | any number of functions | | adds all parameter functions to stack and starts execution if autoRun is true, stack is not already running and stack is not forcibly stopped |
next | function | | | executes next item in stack if stack is not forcibly stopped |
clear | function | | | removes remaining items in the stack |
contents | Array | | | stack instance contents |
autoRun | Bool | | true | should autoRun stack when new item added |
stop | Bool | | false | setting this to true will forcibly prevent the stack from executing |
Basic stack use in node, react, browserify, webpack or any other commonjs implementation
var Stack=require('easy-stack');
var stack=new Stack;
for(var i=0; i<50; i++){
stack.add(makeRequest);
}
function makeRequest(){
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>
<script src='./es5.js'></script>
<script>
console.log('my awesome app script');
var stack=new Stack;
for(var i=0; i<50; i++){
stack.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 Stack=require('easy-stack');
var WS=require('ws-share');
var Message=require('js-message');
var stack=new Stack;
stack.stop=true;
var ws=null;
function startWS(){
ws=new WS('wss://echo.websocket.org/?encoding=text');
ws.on(
'open',
function(){
ws.on(
'message',
handleResponse
);
stack.stop=false;
stack.next();
}
);
ws.on(
'error',
function(err){
stack.stop=true;
stack.clear();
throw(err);
}
);
ws.on(
'close',
function(){
stack.stop=true;
}
);
}
for(var i=0; i<50; i++){
stack.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 ES6 stack.js
const Stack=require('easy-stack');
class MyAwesomestack extends Stack{
isStopped(){
return this.stop;
}
removeThirdItem(){
this.contents.splice(2,1);
return this.contents;
}
};
Extending stack node es5 or browser
var Stack=require('easy-stack');
MyAwesomestack.prototype = new Stack;
MyAwesomestack.prototype.constructor = MyAwesomestack;
function MyAwesomestack(){
Object.defineProperties(
this,
{
isStopped:{
enumerable:true,
get:checkStopped,
set:checkStopped
},
removeThirdItem:{
enumerable:true,
writable:false,
value:removeThird
}
}
);
Object.seal(this);
function checkStopped(){
return this.stop;
}
function removeThird(){
var list=this.contents;
list.splice(2,1);
this.contents=list;
return this.contents;
}
}