|
@@ -1,94 +1,94 @@
|
|
-class Queue<T extends { run: () => Promise<any> }> {
|
|
|
|
- private current?: Node<T>;
|
|
|
|
- private end?: Node<T>;
|
|
|
|
- private status: "waiting" | "running" = "waiting";
|
|
|
|
-
|
|
|
|
- append(item: T, autoRun = true) {
|
|
|
|
- const node = new Node(item);
|
|
|
|
- if (!this.current || !this.end) {
|
|
|
|
- this.current = this.end = node;
|
|
|
|
- } else {
|
|
|
|
- this.end.setNext(node);
|
|
|
|
- this.end = node;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (autoRun) {
|
|
|
|
- this.startRun();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- startRun() {
|
|
|
|
- if (this.status === "waiting") {
|
|
|
|
- this.run();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- async run() {
|
|
|
|
- if (this.current) {
|
|
|
|
- this.status = "running";
|
|
|
|
- const current = this.current;
|
|
|
|
- const self = current.getSelf();
|
|
|
|
- this.current = this.current.getNext();
|
|
|
|
- const res = await self.run();
|
|
|
|
- current.destroy();
|
|
|
|
- if (res === false) {
|
|
|
|
- this.status = "waiting";
|
|
|
|
- } else {
|
|
|
|
- this.run();
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- this.end = undefined;
|
|
|
|
- this.status = "waiting";
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-class Node<T = any> {
|
|
|
|
- private prev?: Node<T>;
|
|
|
|
- private next?: Node<T>;
|
|
|
|
- private self: T;
|
|
|
|
-
|
|
|
|
- constructor(self: T) {
|
|
|
|
- this.self = self;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- getSelf() {
|
|
|
|
- return this.self;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- setPrev(prev?: Node<T>) {
|
|
|
|
- this.prev = prev;
|
|
|
|
- if (prev) {
|
|
|
|
- prev.next = this;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- setNext(next?: Node<T>) {
|
|
|
|
- this.next = next;
|
|
|
|
- if (next) {
|
|
|
|
- next.prev = this;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- getPrev() {
|
|
|
|
- return this.prev;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- getNext() {
|
|
|
|
- return this.next;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- destroy() {
|
|
|
|
- if (this.prev) {
|
|
|
|
- this.prev.setNext(undefined);
|
|
|
|
- }
|
|
|
|
- if (this.next) {
|
|
|
|
- this.next.setPrev(undefined);
|
|
|
|
- }
|
|
|
|
- this.prev = undefined;
|
|
|
|
- this.next = undefined;
|
|
|
|
- this.self = undefined;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-export { Queue };
|
|
|
|
|
|
+class Queue<T extends { run: () => Promise<any> }> {
|
|
|
|
+ private current?: Node<T>;
|
|
|
|
+ private end?: Node<T>;
|
|
|
|
+ private status: "waiting" | "running" = "waiting";
|
|
|
|
+
|
|
|
|
+ append(item: T, autoRun = true) {
|
|
|
|
+ const node = new Node(item);
|
|
|
|
+ if (!this.current || !this.end) {
|
|
|
|
+ this.current = this.end = node;
|
|
|
|
+ } else {
|
|
|
|
+ this.end.setNext(node);
|
|
|
|
+ this.end = node;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (autoRun) {
|
|
|
|
+ this.startRun();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ startRun() {
|
|
|
|
+ if (this.status === "waiting") {
|
|
|
|
+ this.run();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ async run() {
|
|
|
|
+ if (this.current) {
|
|
|
|
+ this.status = "running";
|
|
|
|
+ const current = this.current;
|
|
|
|
+ const self = current.getSelf();
|
|
|
|
+ this.current = this.current.getNext();
|
|
|
|
+ const res = await self.run();
|
|
|
|
+ current.destroy();
|
|
|
|
+ if (res === false) {
|
|
|
|
+ this.status = "waiting";
|
|
|
|
+ } else {
|
|
|
|
+ this.run();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ this.end = undefined;
|
|
|
|
+ this.status = "waiting";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+class Node<T = any> {
|
|
|
|
+ private prev?: Node<T>;
|
|
|
|
+ private next?: Node<T>;
|
|
|
|
+ private self: T;
|
|
|
|
+
|
|
|
|
+ constructor(self: T) {
|
|
|
|
+ this.self = self;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getSelf() {
|
|
|
|
+ return this.self;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setPrev(prev?: Node<T>) {
|
|
|
|
+ this.prev = prev;
|
|
|
|
+ if (prev) {
|
|
|
|
+ prev.next = this;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setNext(next?: Node<T>) {
|
|
|
|
+ this.next = next;
|
|
|
|
+ if (next) {
|
|
|
|
+ next.prev = this;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getPrev() {
|
|
|
|
+ return this.prev;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getNext() {
|
|
|
|
+ return this.next;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ destroy() {
|
|
|
|
+ if (this.prev) {
|
|
|
|
+ this.prev.setNext(undefined);
|
|
|
|
+ }
|
|
|
|
+ if (this.next) {
|
|
|
|
+ this.next.setPrev(undefined);
|
|
|
|
+ }
|
|
|
|
+ this.prev = undefined;
|
|
|
|
+ this.next = undefined;
|
|
|
|
+ this.self = undefined;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export { Queue };
|