refactor: refine circular buffer error message
[poolifier.git] / src / fixed-priority-queue.ts
index 60096ce04e9c77da42639420ba4a4a9675b9d2ea..fadaee750515b3de72bcc6c5a9f6cd70de0b68cd 100644 (file)
@@ -14,6 +14,12 @@ export interface PriorityQueueNode<T> {
   priority: number
 }
 
+/**
+ * Fixed priority queue.
+ *
+ * @typeParam T - Type of fixed priority queue data.
+ * @internal
+ */
 export class FixedPriorityQueue<T> {
   private start!: number
   private readonly nodeArray: Array<PriorityQueueNode<T>>
@@ -39,18 +45,21 @@ export class FixedPriorityQueue<T> {
       throw new Error('Priority queue is full')
     }
     priority = priority ?? 0
+    const nodeArrayLength = this.nodeArray.length
     let inserted = false
-    for (let index = this.start; index < this.nodeArray.length; index++) {
+    for (let index = this.start; index < nodeArrayLength; index++) {
       if (this.nodeArray[index]?.priority > priority) {
         this.nodeArray.splice(index, 0, { data, priority })
         inserted = true
         break
       }
     }
+    this.nodeArray.length !== nodeArrayLength &&
+      (this.nodeArray.length = nodeArrayLength)
     if (!inserted) {
       let index = this.start + this.size
-      if (index >= this.nodeArray.length) {
-        index -= this.nodeArray.length
+      if (index >= nodeArrayLength) {
+        index -= nodeArrayLength
       }
       this.nodeArray[index] = { data, priority }
     }
@@ -121,7 +130,7 @@ export class FixedPriorityQueue<T> {
   private checkSize (size: number): void {
     if (!Number.isSafeInteger(size)) {
       throw new TypeError(
-        `Invalid fixed priority queue size: ${size} is not an integer`
+        `Invalid fixed priority queue size: '${size}' is not an integer`
       )
     }
     if (size < 0) {