queue.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. class Queue<T extends { run: () => Promise<any> }> {
  2. private current?: Node<T>;
  3. private end?: Node<T>;
  4. private status: "waiting" | "running" = "waiting";
  5. private _myResolve?: (v: "finished" | "stopped") => void;
  6. private _promise?: Promise<"finished" | "stopped">;
  7. append(item: T, autoRun = false) {
  8. const node = new Node(item);
  9. if (!this.current || !this.end) {
  10. this.current = this.end = node;
  11. } else {
  12. this.end.setNext(node);
  13. this.end = node;
  14. }
  15. if (autoRun) {
  16. this.startRun();
  17. }
  18. }
  19. startRun() {
  20. if (this.status === "waiting") {
  21. this.run();
  22. this._promise = new Promise<"finished" | "stopped">((resolve) => {
  23. this._myResolve = resolve;
  24. });
  25. }
  26. return this._promise;
  27. }
  28. async run() {
  29. if (this.current) {
  30. this.status = "running";
  31. const current = this.current;
  32. const self = current.getSelf();
  33. this.current = this.current.getNext();
  34. const res = await self.run();
  35. current.destroy();
  36. if (res === false) {
  37. this.status = "waiting";
  38. this._myResolve("stopped");
  39. this._promise = undefined;
  40. this._myResolve = undefined;
  41. } else {
  42. this.run();
  43. }
  44. } else {
  45. this.end = undefined;
  46. this.status = "waiting";
  47. this._myResolve("finished");
  48. this._promise = undefined;
  49. this._myResolve = undefined;
  50. }
  51. }
  52. }
  53. class Node<T = any> {
  54. private prev?: Node<T>;
  55. private next?: Node<T>;
  56. private self: T;
  57. constructor(self: T) {
  58. this.self = self;
  59. }
  60. getSelf() {
  61. return this.self;
  62. }
  63. setPrev(prev?: Node<T>) {
  64. this.prev = prev;
  65. if (prev) {
  66. prev.next = this;
  67. }
  68. }
  69. setNext(next?: Node<T>) {
  70. this.next = next;
  71. if (next) {
  72. next.prev = this;
  73. }
  74. }
  75. getPrev() {
  76. return this.prev;
  77. }
  78. getNext() {
  79. return this.next;
  80. }
  81. destroy() {
  82. if (this.prev) {
  83. this.prev.setNext(undefined);
  84. }
  85. if (this.next) {
  86. this.next.setPrev(undefined);
  87. }
  88. this.prev = undefined;
  89. this.next = undefined;
  90. this.self = undefined;
  91. }
  92. }
  93. export { Queue };