queue.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { __awaiter } from "tslib";
  2. class Queue {
  3. constructor() {
  4. this.status = "waiting";
  5. }
  6. append(item, autoRun = true) {
  7. var _a;
  8. const node = new Node(item);
  9. if (this.start) {
  10. (_a = this.start) === null || _a === void 0 ? void 0 : _a.setNext(node);
  11. }
  12. else {
  13. this.start = node;
  14. }
  15. if (autoRun) {
  16. this.startRun();
  17. }
  18. }
  19. startRun() {
  20. if (this.status === "waiting") {
  21. this.run();
  22. }
  23. }
  24. run() {
  25. return __awaiter(this, void 0, void 0, function* () {
  26. if (this.start) {
  27. this.status = "running";
  28. const self = this.start.getSelf();
  29. this.start = this.start.getNext();
  30. const res = yield self.run();
  31. if (res === false) {
  32. this.status = "waiting";
  33. }
  34. else {
  35. this.run();
  36. }
  37. }
  38. else {
  39. this.status = "waiting";
  40. }
  41. });
  42. }
  43. }
  44. class Node {
  45. constructor(self) {
  46. this.self = self;
  47. }
  48. getSelf() {
  49. return this.self;
  50. }
  51. setPrev(prev) {
  52. this.prev = prev;
  53. if (prev) {
  54. prev.next = this;
  55. }
  56. }
  57. setNext(next) {
  58. this.next = next;
  59. if (next) {
  60. next.prev = this;
  61. }
  62. }
  63. getPrev() {
  64. return this.prev;
  65. }
  66. getNext() {
  67. return this.next;
  68. }
  69. }
  70. export { Queue };