fix: fix tasks usage accounting initializer
[poolifier.git] / src / queue.ts
index e7c9dd11fc0332ccbba845e9bd663d4bc682c523..dab9f04c2783f5a5aa039a3af894780f1b5c639f 100644 (file)
@@ -51,7 +51,7 @@ export class Queue<T> {
   }
 
   /**
-   * Peek at the first item.
+   * Peeks at the first item.
    *
    * @returns The first item or `undefined` if the queue is empty.
    */
@@ -63,7 +63,7 @@ export class Queue<T> {
   }
 
   /**
-   * Clear the queue.
+   * Clears the queue.
    */
   public clear (): void {
     this.items = []
@@ -71,4 +71,32 @@ export class Queue<T> {
     this.size = 0
     this.maxSize = 0
   }
+
+  /**
+   * Returns an iterator for the queue.
+   *
+   * @returns An iterator for the queue.
+   * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
+   */
+  [Symbol.iterator] (): Iterator<T> {
+    const items = this.items
+    let i = this.offset
+
+    return {
+      next: () => {
+        if (i >= items.length) {
+          return {
+            value: undefined,
+            done: true
+          }
+        }
+        const value = items[i]
+        i++
+        return {
+          value,
+          done: false
+        }
+      }
+    }
+  }
 }