chore: v2.4.12
[poolifier.git] / src / queue.ts
index 5cc13fcd27e7ea86e3bf504d5b5e3a110665b85d..ab606547b1f77fb08ab6b203e0053ad182b481e6 100644 (file)
@@ -1,25 +1,50 @@
+// Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
+
 /**
  * Queue
+ *
+ * @typeParam T - Type of queue items.
  */
 export class Queue<T> {
   private items: Record<number, T>
   private head: number
   private tail: number
 
-  constructor () {
+  public constructor () {
     this.items = {}
     this.head = 0
     this.tail = 0
   }
 
-  enqueue (item: T): number {
+  /**
+   * Get the size of the queue.
+   *
+   * @returns The size of the queue.
+   * @readonly
+   */
+  public get size (): number {
+    return this.tail - this.head
+  }
+
+  /**
+   * Enqueue an item.
+   *
+   * @param item - Item to enqueue.
+   * @returns The new size of the queue.
+   */
+  public enqueue (item: T): number {
     this.items[this.tail] = item
     this.tail++
-    return this.size()
+    return this.size
   }
 
-  dequeue (): T | undefined {
-    if (this.size() <= 0) return undefined
+  /**
+   * Dequeue an item.
+   *
+   * @returns The dequeued item or `undefined` if the queue is empty.
+   */
+  public dequeue (): T | undefined {
+    if (this.size <= 0) return undefined
     const item = this.items[this.head]
     // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
     delete this.items[this.head]
@@ -30,8 +55,4 @@ export class Queue<T> {
     }
     return item
   }
-
-  size (): number {
-    return this.tail - this.head
-  }
 }