stack.js 736 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class Stack{
  2. constructor(){
  3. this.stack=[];
  4. this.autoRun=true;
  5. this.running=false;
  6. this.stop=false;
  7. }
  8. clear(){
  9. this.stack=[];
  10. return this.stack;
  11. }
  12. contents(val){
  13. if(val){
  14. this.stack=val;
  15. }
  16. return this.stack;
  17. }
  18. add(...callbacks){
  19. this.stack.push(...callbacks);
  20. if(!this.running && !this.stop && this.autoRun){
  21. this.next();
  22. }
  23. }
  24. next(){
  25. this.running=true;
  26. if(this.stack.length<1 || this.stop){
  27. this.running=false;
  28. return;
  29. }
  30. this.stack.pop().bind(this)();
  31. }
  32. }
  33. module.exports=Stack;