Simple JS stack with auto run for node and browsers
npm install easy-stack require('easy-stack'); for ES6 node.
require('easy-stack/es5.js'); for ES5 node and browser.
javascript
var Stack=require('easy-stack');
//create a new Stack instance
var stack=new Stack;
for(var i=0; i<50; i++){
//add a bunch of stuff to the stack
stack.add(makeRequest);
}
function makeRequest(){
//do stuff
console.log('making some request');
this.next();
}
`
$3
The only difference is including via a script tag instead of using require.
`html
`
$3
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.
`javascript
var Stack=require('easy-stack');
//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 Stack instance
var stack=new Stack;
//force stop until websocket opened
stack.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
stack.stop=false;
stack.next();
}
);
ws.on(
'error',
function(err){
//stop execution of stack if there is an error because the websocket is likely closed
stack.stop=true;
//remove remaining items in the stack
stack.clear();
throw(err);
}
);
ws.on(
'close',
function(){
//stop execution of stack when the websocket closed
stack.stop=true;
}
);
}
//simulate a lot of requests being stackd up for the websocket
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
`javascript
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
`javascript
var Stack=require('easy-stack');
//MyAwesomestack inherits from stack
MyAwesomestack.prototype = new Stack;
//Constructor will extend stack
MyAwesomestack.prototype.constructor = MyAwesomestack;
function MyAwesomestack(){
//extend with some stuff your app needs,
//maybe npm publish your extention with easy-stack 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 stack content
var list=this.contents;
//modify the stack content
list.splice(2,1);
//save the modified stack content
this.contents=list;
return this.contents;
}
}
``