Răsfoiți Sursa

feature: 代码优化

xiongxt 1 an în urmă
părinte
comite
7ddfffd135
4 a modificat fișierele cu 48 adăugiri și 16 ștergeri
  1. 21 7
      lib/queue.js
  2. 1 1
      package.json
  3. 24 7
      src/queue.ts
  4. 2 1
      types/queue.d.ts

+ 21 - 7
lib/queue.js

@@ -4,13 +4,13 @@ class Queue {
         this.status = "waiting";
     }
     append(item, autoRun = true) {
-        var _a;
         const node = new Node(item);
-        if (this.start) {
-            (_a = this.start) === null || _a === void 0 ? void 0 : _a.setNext(node);
+        if (!this.current || !this.end) {
+            this.current = this.end = node;
         }
         else {
-            this.start = node;
+            this.end.setNext(node);
+            this.end = node;
         }
         if (autoRun) {
             this.startRun();
@@ -23,11 +23,13 @@ class Queue {
     }
     run() {
         return __awaiter(this, void 0, void 0, function* () {
-            if (this.start) {
+            if (this.current) {
                 this.status = "running";
-                const self = this.start.getSelf();
-                this.start = this.start.getNext();
+                const current = this.current;
+                const self = current.getSelf();
+                this.current = this.current.getNext();
                 const res = yield self.run();
+                current.destroy();
                 if (res === false) {
                     this.status = "waiting";
                 }
@@ -36,6 +38,7 @@ class Queue {
                 }
             }
             else {
+                this.end = undefined;
                 this.status = "waiting";
             }
         });
@@ -66,5 +69,16 @@ class Node {
     getNext() {
         return this.next;
     }
+    destroy() {
+        if (this.prev) {
+            this.prev.setNext(null);
+        }
+        if (this.next) {
+            this.next.setPrev(null);
+        }
+        this.prev = null;
+        this.next = null;
+        this.self = undefined;
+    }
 }
 export { Queue };

+ 1 - 1
package.json

@@ -3,7 +3,7 @@
   "version": "1.0.0",
   "description": "toolkit",
   "main": "lib/index.js",
-  "typings": "type/index.d.ts",
+  "typings": "types/index.d.ts",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "lint": "eslint . --ext .ts --fix",

+ 24 - 7
src/queue.ts

@@ -1,13 +1,15 @@
 class Queue<T extends { run: () => Promise<any> }> {
-  private start?: Node<T>;
+  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.start) {
-      this.start?.setNext(node);
+    if (!this.current || !this.end) {
+      this.current = this.end = node;
     } else {
-      this.start = node;
+      this.end.setNext(node);
+      this.end = node;
     }
 
     if (autoRun) {
@@ -22,17 +24,20 @@ class Queue<T extends { run: () => Promise<any> }> {
   }
 
   async run() {
-    if (this.start) {
+    if (this.current) {
       this.status = "running";
-      const self = this.start.getSelf();
-      this.start = this.start.getNext();
+      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";
     }
   }
@@ -72,6 +77,18 @@ class Node<T = any> {
   getNext() {
     return this.next;
   }
+
+  destroy() {
+    if (this.prev) {
+      this.prev.setNext(null);
+    }
+    if (this.next) {
+      this.next.setPrev(null);
+    }
+    this.prev = null;
+    this.next = null;
+    this.self = undefined;
+  }
 }
 
 export { Queue };

+ 2 - 1
types/queue.d.ts

@@ -1,7 +1,8 @@
 declare class Queue<T extends {
     run: () => Promise<any>;
 }> {
-    private start?;
+    private current?;
+    private end?;
     private status;
     append(item: T, autoRun?: boolean): void;
     startRun(): void;