refactor: remove unused queue code
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 23 Aug 2023 10:29:40 +0000 (12:29 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 23 Aug 2023 10:29:40 +0000 (12:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/index.ts
src/pools/abstract-pool.ts
src/queue.ts [deleted file]
tests/queue.test.js [deleted file]

index a4e22756afc61c8bfe9bd0ff58b088d2031b06c3..6f6c53849162e84377d7c6380c1c29b3e499d127 100644 (file)
@@ -70,5 +70,5 @@ export type {
   WorkerStatistics
 } from './utility-types'
 export type { CircularArray } from './circular-array'
-export type { Queue } from './queue'
+export type { Deque } from './deque'
 export { availableParallelism } from './utils'
index f0119f4ed3c8135e596d171a0f6ec093d7b29935..f0304364e8770f729f7dbb4100069e146f8dda72 100644 (file)
@@ -1205,12 +1205,12 @@ export abstract class AbstractPool<
       if (executeTask) {
         this.executeTask(
           targetWorkerNodeKey,
-          this.popTask(workerNodeKey) as Task<Data>
+          this.dequeueTask(workerNodeKey) as Task<Data>
         )
       } else {
         this.enqueueTask(
           targetWorkerNodeKey,
-          this.popTask(workerNodeKey) as Task<Data>
+          this.dequeueTask(workerNodeKey) as Task<Data>
         )
       }
     }
@@ -1426,10 +1426,6 @@ export abstract class AbstractPool<
     return this.workerNodes[workerNodeKey].dequeueTask()
   }
 
-  private popTask (workerNodeKey: number): Task<Data> | undefined {
-    return this.workerNodes[workerNodeKey].popTask()
-  }
-
   private tasksQueueSize (workerNodeKey: number): number {
     return this.workerNodes[workerNodeKey].tasksQueueSize()
   }
diff --git a/src/queue.ts b/src/queue.ts
deleted file mode 100644 (file)
index d38296b..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-// Copyright Jerome Benoit. 2022-2023. All Rights Reserved.
-
-/**
- * Queue.
- *
- * @typeParam T - Type of queue items.
- */
-export class Queue<T> {
-  private items!: T[]
-  private offset!: number
-  /** The size of the queue. */
-  public size!: number
-  /** The maximum size of the queue. */
-  public maxSize!: number
-
-  public constructor () {
-    this.clear()
-  }
-
-  /**
-   * Enqueue an item.
-   *
-   * @param item - Item to enqueue.
-   * @returns The new size of the queue.
-   */
-  public enqueue (item: T): number {
-    this.items.push(item)
-    ++this.size
-    if (this.size > this.maxSize) {
-      this.maxSize = this.size
-    }
-    return this.size
-  }
-
-  /**
-   * 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.offset]
-    if (++this.offset * 2 >= this.items.length) {
-      this.items = this.items.slice(this.offset)
-      this.offset = 0
-    }
-    --this.size
-    return item
-  }
-
-  /**
-   * Peeks at the first item.
-   *
-   * @returns The first item or `undefined` if the queue is empty.
-   */
-  public peek (): T | undefined {
-    if (this.size <= 0) {
-      return undefined
-    }
-    return this.items[this.offset]
-  }
-
-  /**
-   * Clears the queue.
-   */
-  public clear (): void {
-    this.items = []
-    this.offset = 0
-    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
-        }
-      }
-    }
-  }
-}
diff --git a/tests/queue.test.js b/tests/queue.test.js
deleted file mode 100644 (file)
index f123a05..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-// const { expect } = require('expect')
-// const { Queue } = require('../lib/queue')
-
-// describe('Queue test suite', () => {
-//   it('Verify enqueue() behavior', () => {
-//     const queue = new Queue()
-//     let rtSize = queue.enqueue(1)
-//     expect(queue.size).toBe(1)
-//     expect(rtSize).toBe(queue.size)
-//     expect(queue.offset).toBe(0)
-//     expect(queue.maxSize).toBe(1)
-//     expect(queue.items).toStrictEqual([1])
-//     rtSize = queue.enqueue(2)
-//     expect(queue.size).toBe(2)
-//     expect(rtSize).toBe(queue.size)
-//     expect(queue.offset).toBe(0)
-//     expect(queue.maxSize).toBe(2)
-//     expect(queue.items).toStrictEqual([1, 2])
-//     rtSize = queue.enqueue(3)
-//     expect(queue.size).toBe(3)
-//     expect(rtSize).toBe(queue.size)
-//     expect(queue.offset).toBe(0)
-//     expect(queue.maxSize).toBe(3)
-//     expect(queue.items).toStrictEqual([1, 2, 3])
-//   })
-
-//   it('Verify dequeue() behavior', () => {
-//     const queue = new Queue()
-//     queue.enqueue(1)
-//     queue.enqueue(2)
-//     queue.enqueue(3)
-//     let rtItem = queue.dequeue()
-//     expect(queue.size).toBe(2)
-//     expect(rtItem).toBe(1)
-//     expect(queue.offset).toBe(1)
-//     expect(queue.maxSize).toBe(3)
-//     expect(queue.items).toStrictEqual([1, 2, 3])
-//     rtItem = queue.dequeue()
-//     expect(queue.size).toBe(1)
-//     expect(rtItem).toBe(2)
-//     expect(queue.offset).toBe(0)
-//     expect(queue.maxSize).toBe(3)
-//     expect(queue.items).toStrictEqual([3])
-//     rtItem = queue.dequeue()
-//     expect(queue.size).toBe(0)
-//     expect(rtItem).toBe(3)
-//     expect(queue.offset).toBe(0)
-//     expect(queue.maxSize).toBe(3)
-//     expect(queue.items).toStrictEqual([])
-//   })
-
-//   it('Verify clear() behavior', () => {
-//     const queue = new Queue()
-//     queue.enqueue(1)
-//     queue.enqueue(2)
-//     queue.enqueue(3)
-//     expect(queue.size).toBe(3)
-//     expect(queue.maxSize).toBe(3)
-//     expect(queue.items).toStrictEqual([1, 2, 3])
-//     expect(queue.offset).toBe(0)
-//     queue.clear()
-//     expect(queue.size).toBe(0)
-//     expect(queue.maxSize).toBe(0)
-//     expect(queue.items).toStrictEqual([])
-//     expect(queue.offset).toBe(0)
-//   })
-
-//   it('Verify iterator behavior', () => {
-//     const queue = new Queue()
-//     queue.enqueue(1)
-//     queue.enqueue(2)
-//     queue.enqueue(3)
-//     let i = 1
-//     for (const item of queue) {
-//       expect(item).toBe(i)
-//       ++i
-//     }
-//   })
-// })