You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
8.1 KiB

3 months ago
  1. # easy-stack Is Great for any javascript stack (LIFO)
  2. 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.
  3. 1. socket messages
  4. 2. priority async operations
  5. 3. priority synchronous operations
  6. 4. stacks you want to start running automatically when you add new items
  7. 5. any simple or complex stack operations
  8. 6. base class to extend
  9. 7. anything else that needs a stack
  10. # Stable and easy to use
  11. 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.
  12. ` require('easy-stack'); ` for ES6 node.
  13. ` require('easy-stack/es5.js'); ` for ES5 node and browser.
  14. **npm install easy-stack**
  15. npm info : [See npm trends and stats for easy-stack](http://npm-stat.com/charts.html?package=easy-stack&author=&from=&to=)
  16. ![easy-stack npm version](https://img.shields.io/npm/v/easy-stack.svg) ![supported node version for easy-stack](https://img.shields.io/node/v/easy-stack.svg) ![total npm downloads for easy-stack](https://img.shields.io/npm/dt/easy-stack.svg) ![monthly npm downloads for easy-stack](https://img.shields.io/npm/dm/easy-stack.svg) ![npm licence for easy-stack](https://img.shields.io/npm/l/easy-stack.svg)
  17. [![RIAEvangelist](https://avatars3.githubusercontent.com/u/369041?v=3&s=100)](https://github.com/RIAEvangelist)
  18. GitHub info :
  19. ![easy-stack GitHub Release](https://img.shields.io/github/release/RIAEvangelist/easy-stack.svg) ![GitHub license easy-stack license](https://img.shields.io/github/license/RIAEvangelist/easy-stack.svg) ![open issues for easy-stack on GitHub](https://img.shields.io/github/issues/RIAEvangelist/easy-stack.svg)
  20. Package details websites :
  21. * [GitHub.io site](http://riaevangelist.github.io/easy-stack/ "easy-stack documentation"). A prettier version of this site.
  22. * [NPM Module](https://www.npmjs.org/package/easy-stack "easy-stack npm module"). The npm page for the easy-stack module.
  23. This work is licenced via the [DBAD Public Licence](http://www.dbad-license.org/).
  24. ## Exposed methods and values
  25. |key|type|parameters|default|description|
  26. |----|----|----|----|----|
  27. |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 |
  28. |next|function| | |executes next item in stack if stack is not forcibly stopped|
  29. |clear|function| | |removes remaining items in the stack|
  30. |contents|Array| | | stack instance contents |
  31. |autoRun|Bool| | true |should autoRun stack when new item added|
  32. |stop|Bool| | false |setting this to true will forcibly prevent the stack from executing|
  33. ### Basic stack use in node, react, browserify, webpack or any other commonjs implementation
  34. ```javascript
  35. var Stack=require('easy-stack');
  36. //create a new Stack instance
  37. var stack=new Stack;
  38. for(var i=0; i<50; i++){
  39. //add a bunch of stuff to the stack
  40. stack.add(makeRequest);
  41. }
  42. function makeRequest(){
  43. //do stuff
  44. console.log('making some request');
  45. this.next();
  46. }
  47. ```
  48. ### Basic browser use
  49. The only difference is including via a script tag instead of using require.
  50. ```html
  51. <html>
  52. <head>
  53. <!-- this is the only difference -->
  54. <script src='./es5.js'></script>
  55. <script>
  56. console.log('my awesome app script');
  57. var stack=new Stack;
  58. for(var i=0; i<50; i++){
  59. stack.add(makeRequest);
  60. }
  61. function makeRequest(){
  62. console.log('making some request');
  63. this.next();
  64. }
  65. </script>
  66. </head>
  67. <body>
  68. </body>
  69. </html>
  70. ```
  71. ### Basic use with websockets in node, react, browserify, webpack or any other commonjs implementation
  72. 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.
  73. ```javascript
  74. var Stack=require('easy-stack');
  75. //ws-share just makes it easier to share websocket code and ensure you don't open a websocket more than once
  76. var WS=require('ws-share');
  77. //js-message makes it easy to create and parse normalized JSON messages.
  78. var Message=require('js-message');
  79. //create a new Stack instance
  80. var stack=new Stack;
  81. //force stop until websocket opened
  82. stack.stop=true;
  83. var ws=null;
  84. function startWS(){
  85. //websocket.org rocks
  86. ws=new WS('wss://echo.websocket.org/?encoding=text');
  87. ws.on(
  88. 'open',
  89. function(){
  90. ws.on(
  91. 'message',
  92. handleResponse
  93. );
  94. //now that websocket is opened allow auto execution
  95. stack.stop=false;
  96. stack.next();
  97. }
  98. );
  99. ws.on(
  100. 'error',
  101. function(err){
  102. //stop execution of stack if there is an error because the websocket is likely closed
  103. stack.stop=true;
  104. //remove remaining items in the stack
  105. stack.clear();
  106. throw(err);
  107. }
  108. );
  109. ws.on(
  110. 'close',
  111. function(){
  112. //stop execution of stack when the websocket closed
  113. stack.stop=true;
  114. }
  115. );
  116. }
  117. //simulate a lot of requests being stackd up for the websocket
  118. for(var i=0; i<50; i++){
  119. stack.add(makeRequest);
  120. }
  121. var messageID=0;
  122. function handleResponse(e){
  123. var message=new Message;
  124. message.load(e.data);
  125. console.log(message.type,message.data);
  126. }
  127. function makeRequest(){
  128. messageID++;
  129. var message=new Message;
  130. message.type='testMessage';
  131. message.data=messageID;
  132. ws.send(message.JSON);
  133. this.next();
  134. }
  135. startWS();
  136. ```
  137. # Extending ES6 stack.js
  138. ```javascript
  139. const Stack=require('easy-stack');
  140. class MyAwesomestack extends Stack{
  141. isStopped(){
  142. return this.stop;
  143. }
  144. removeThirdItem(){
  145. this.contents.splice(2,1);
  146. return this.contents;
  147. }
  148. };
  149. ```
  150. # Extending stack node es5 or browser
  151. ```javascript
  152. var Stack=require('easy-stack');
  153. //MyAwesomestack inherits from stack
  154. MyAwesomestack.prototype = new Stack;
  155. //Constructor will extend stack
  156. MyAwesomestack.prototype.constructor = MyAwesomestack;
  157. function MyAwesomestack(){
  158. //extend with some stuff your app needs,
  159. //maybe npm publish your extention with easy-stack as a dependancy?
  160. Object.defineProperties(
  161. this,
  162. {
  163. isStopped:{
  164. enumerable:true,
  165. get:checkStopped,
  166. set:checkStopped
  167. },
  168. removeThirdItem:{
  169. enumerable:true,
  170. writable:false,
  171. value:removeThird
  172. }
  173. }
  174. );
  175. //enforce Object.assign for extending by locking down Class structure
  176. //no willy nilly cowboy coding
  177. Object.seal(this);
  178. function checkStopped(){
  179. return this.stop;
  180. }
  181. function removeThird(){
  182. //get the stack content
  183. var list=this.contents;
  184. //modify the stack content
  185. list.splice(2,1);
  186. //save the modified stack content
  187. this.contents=list;
  188. return this.contents;
  189. }
  190. }
  191. ```