From: Jérôme Benoit Date: Sun, 7 Jul 2024 10:33:31 +0000 (+0200) Subject: refactor: move queueing code into its own directory X-Git-Tag: v4.0.17~1^2~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c6dd1aeb73ee5d5dd5bbfa9ebd9c4496a60f1252;p=poolifier.git refactor: move queueing code into its own directory Signed-off-by: Jérôme Benoit --- diff --git a/src/index.ts b/src/index.ts index 178eeed2..8bd6a4d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,9 +49,9 @@ export type { WorkerUsage, } from './pools/worker.js' export { WorkerTypes } from './pools/worker.js' -export type { PriorityQueue } from './priority-queue.js' +export type { PriorityQueue } from './queues/priority-queue.js' +export type { FixedQueueNode, IFixedQueue } from './queues/queue-types.js' export type { - IFixedQueue, MessageValue, PromiseResponseWrapper, Task, diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e11a2bbb..c009f20a 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -4,12 +4,12 @@ import { EventEmitterAsyncResource } from 'node:events' import { performance } from 'node:perf_hooks' import type { TransferListItem } from 'node:worker_threads' -import { - defaultBucketSize, - type MessageValue, - type PromiseResponseWrapper, - type Task, - type TaskFunctionProperties, +import { defaultBucketSize } from '../queues/queue-types.js' +import type { + MessageValue, + PromiseResponseWrapper, + Task, + TaskFunctionProperties, } from '../utility-types.js' import { average, diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index d29a6e32..b42d8493 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -2,7 +2,7 @@ import { EventEmitter } from 'node:events' import { MessageChannel } from 'node:worker_threads' import { CircularBuffer } from '../circular-buffer.js' -import { PriorityQueue } from '../priority-queue.js' +import { PriorityQueue } from '../queues/priority-queue.js' import type { Task } from '../utility-types.js' import { DEFAULT_TASK_NAME } from '../utils.js' import { diff --git a/src/abstract-fixed-queue.ts b/src/queues/abstract-fixed-queue.ts similarity index 98% rename from src/abstract-fixed-queue.ts rename to src/queues/abstract-fixed-queue.ts index eee066d3..44cfab9d 100644 --- a/src/abstract-fixed-queue.ts +++ b/src/queues/abstract-fixed-queue.ts @@ -2,7 +2,7 @@ import { defaultQueueSize, type FixedQueueNode, type IFixedQueue, -} from './utility-types.js' +} from './queue-types.js' /** * Base fixed queue class diff --git a/src/fixed-priority-queue.ts b/src/queues/fixed-priority-queue.ts similarity index 95% rename from src/fixed-priority-queue.ts rename to src/queues/fixed-priority-queue.ts index 6c899283..7a3976d7 100644 --- a/src/fixed-priority-queue.ts +++ b/src/queues/fixed-priority-queue.ts @@ -1,5 +1,5 @@ import { AbstractFixedQueue } from './abstract-fixed-queue.js' -import type { IFixedQueue } from './utility-types.js' +import type { IFixedQueue } from './queue-types.js' /** * Fixed priority queue. diff --git a/src/fixed-queue.ts b/src/queues/fixed-queue.ts similarity index 91% rename from src/fixed-queue.ts rename to src/queues/fixed-queue.ts index e9003f70..366e41f1 100644 --- a/src/fixed-queue.ts +++ b/src/queues/fixed-queue.ts @@ -1,5 +1,5 @@ import { AbstractFixedQueue } from './abstract-fixed-queue.js' -import type { IFixedQueue } from './utility-types.js' +import type { IFixedQueue } from './queue-types.js' /** * Fixed queue. diff --git a/src/priority-queue.ts b/src/queues/priority-queue.ts similarity index 99% rename from src/priority-queue.ts rename to src/queues/priority-queue.ts index 1e15fe2d..dcfd6fb9 100644 --- a/src/priority-queue.ts +++ b/src/queues/priority-queue.ts @@ -7,7 +7,7 @@ import { type FixedQueueNode, type IFixedQueue, type PriorityQueueNode, -} from './utility-types.js' +} from './queue-types.js' /** * Priority queue. diff --git a/src/queues/queue-types.ts b/src/queues/queue-types.ts new file mode 100644 index 00000000..83d7f7f2 --- /dev/null +++ b/src/queues/queue-types.ts @@ -0,0 +1,83 @@ +/** + * Default queue size. + * @internal + */ +export const defaultQueueSize = 2048 + +/** + * Fixed queue node. + * @typeParam T - Type of fixed queue node data. + * @internal + */ +export interface FixedQueueNode { + data: T + priority: number +} + +/** + * Fixed queue. + * @typeParam T - Type of fixed queue data. + * @internal + */ +export interface IFixedQueue { + /** The fixed queue capacity. */ + readonly capacity: number + /** The fixed queue size. */ + readonly size: number + /** The fixed queue node array. */ + nodeArray: FixedQueueNode[] + /** + * Checks if the fixed queue is empty. + * @returns `true` if the fixed queue is empty, `false` otherwise. + */ + empty: () => boolean + /** + * Checks if the fixed queue is full. + * @returns `true` if the fixed queue is full, `false` otherwise. + */ + full: () => boolean + /** + * Enqueue data into the fixed queue. + * @param data - Data to enqueue. + * @param priority - Priority of the data. Lower values have higher priority. + * @returns The new size of the fixed queue. + * @throws If the fixed queue is full. + */ + enqueue: (data: T, priority?: number) => number + /** + * Gets data from the fixed queue. + * @param index - The index of the data to get. + * @returns The data at the index or `undefined` if the fixed queue is empty or the index is out of bounds. + */ + get: (index: number) => T | undefined + /** + * Dequeue data from the fixed queue. + * @returns The dequeued data or `undefined` if the fixed queue is empty. + */ + dequeue: () => T | undefined + /** + * Clears the fixed queue. + */ + clear: () => void + /** + * Returns an iterator for the fixed queue. + * @returns An iterator for the fixed queue. + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols + */ + [Symbol.iterator]: () => Iterator +} + +/** + * Default bucket size. + * @internal + */ +export const defaultBucketSize = 2048 + +/** + * Priority queue node. + * @typeParam T - Type of priority queue node data. + * @internal + */ +export interface PriorityQueueNode extends IFixedQueue { + next?: IFixedQueue +} diff --git a/src/utility-types.ts b/src/utility-types.ts index 858dc0e6..b29f8012 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -212,87 +212,3 @@ export interface PromiseResponseWrapper { * @internal */ export type Writable = { -readonly [P in keyof T]: T[P] } - -/** - * Default queue size. - * @internal - */ -export const defaultQueueSize = 2048 - -/** - * Fixed queue node. - * @typeParam T - Type of fixed queue node data. - * @internal - */ -export interface FixedQueueNode { - data: T - priority: number -} - -/** - * Fixed queue. - * @typeParam T - Type of fixed queue data. - * @internal - */ -export interface IFixedQueue { - /** The fixed queue capacity. */ - readonly capacity: number - /** The fixed queue size. */ - readonly size: number - /** The fixed queue node array. */ - nodeArray: FixedQueueNode[] - /** - * Checks if the fixed queue is empty. - * @returns `true` if the fixed queue is empty, `false` otherwise. - */ - empty: () => boolean - /** - * Checks if the fixed queue is full. - * @returns `true` if the fixed queue is full, `false` otherwise. - */ - full: () => boolean - /** - * Enqueue data into the fixed queue. - * @param data - Data to enqueue. - * @param priority - Priority of the data. Lower values have higher priority. - * @returns The new size of the fixed queue. - * @throws If the fixed queue is full. - */ - enqueue: (data: T, priority?: number) => number - /** - * Gets data from the fixed queue. - * @param index - The index of the data to get. - * @returns The data at the index or `undefined` if the fixed queue is empty or the index is out of bounds. - */ - get: (index: number) => T | undefined - /** - * Dequeue data from the fixed queue. - * @returns The dequeued data or `undefined` if the fixed queue is empty. - */ - dequeue: () => T | undefined - /** - * Clears the fixed queue. - */ - clear: () => void - /** - * Returns an iterator for the fixed queue. - * @returns An iterator for the fixed queue. - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols - */ - [Symbol.iterator]: () => Iterator -} - -/** - * Default bucket size. - * @internal - */ -export const defaultBucketSize = 2048 - -/** - * Priority queue node. - * @typeParam T - Type of priority queue node data. - * @internal - */ -export interface PriorityQueueNode extends IFixedQueue { - next?: IFixedQueue -} diff --git a/tests/pools/abstract-pool.test.mjs b/tests/pools/abstract-pool.test.mjs index 276ecf14..1f9213f3 100644 --- a/tests/pools/abstract-pool.test.mjs +++ b/tests/pools/abstract-pool.test.mjs @@ -20,8 +20,8 @@ import { WorkerTypes, } from '../../lib/index.cjs' import { WorkerNode } from '../../lib/pools/worker-node.cjs' -import { PriorityQueue } from '../../lib/priority-queue.cjs' -import { defaultBucketSize } from '../../lib/utility-types.cjs' +import { PriorityQueue } from '../../lib/queues/priority-queue.cjs' +import { defaultBucketSize } from '../../lib/queues/queue-types.cjs' import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' import { waitPoolEvents } from '../test-utils.cjs' diff --git a/tests/pools/worker-node.test.mjs b/tests/pools/worker-node.test.mjs index f00b8061..a5411808 100644 --- a/tests/pools/worker-node.test.mjs +++ b/tests/pools/worker-node.test.mjs @@ -6,7 +6,7 @@ import { expect } from 'expect' import { CircularBuffer } from '../../lib/circular-buffer.cjs' import { WorkerTypes } from '../../lib/index.cjs' import { WorkerNode } from '../../lib/pools/worker-node.cjs' -import { PriorityQueue } from '../../lib/priority-queue.cjs' +import { PriorityQueue } from '../../lib/queues/priority-queue.cjs' import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs' describe('Worker node test suite', () => { diff --git a/tests/fixed-priority-queue.test.mjs b/tests/queues/fixed-priority-queue.test.mjs similarity index 97% rename from tests/fixed-priority-queue.test.mjs rename to tests/queues/fixed-priority-queue.test.mjs index 57947ae8..36474b0b 100644 --- a/tests/fixed-priority-queue.test.mjs +++ b/tests/queues/fixed-priority-queue.test.mjs @@ -1,7 +1,7 @@ import { expect } from 'expect' -import { FixedPriorityQueue } from '../lib/fixed-priority-queue.cjs' -import { defaultQueueSize } from '../lib/utility-types.cjs' +import { FixedPriorityQueue } from '../../lib/queues/fixed-priority-queue.cjs' +import { defaultQueueSize } from '../../lib/queues/queue-types.cjs' describe('Fixed priority queue test suite', () => { it('Verify constructor() behavior', () => { diff --git a/tests/fixed-queue.test.mjs b/tests/queues/fixed-queue.test.mjs similarity index 97% rename from tests/fixed-queue.test.mjs rename to tests/queues/fixed-queue.test.mjs index fc81e128..7fa7ac02 100644 --- a/tests/fixed-queue.test.mjs +++ b/tests/queues/fixed-queue.test.mjs @@ -1,7 +1,7 @@ import { expect } from 'expect' -import { FixedQueue } from '../lib/fixed-queue.cjs' -import { defaultQueueSize } from '../lib/utility-types.cjs' +import { FixedQueue } from '../../lib/queues/fixed-queue.cjs' +import { defaultQueueSize } from '../../lib/queues/queue-types.cjs' describe('Fixed queue test suite', () => { it('Verify constructor() behavior', () => { diff --git a/tests/priority-queue.test.mjs b/tests/queues/priority-queue.test.mjs similarity index 98% rename from tests/priority-queue.test.mjs rename to tests/queues/priority-queue.test.mjs index c9b141b2..52d18dbf 100644 --- a/tests/priority-queue.test.mjs +++ b/tests/queues/priority-queue.test.mjs @@ -1,9 +1,9 @@ import { expect } from 'expect' -import { FixedPriorityQueue } from '../lib/fixed-priority-queue.cjs' -import { FixedQueue } from '../lib/fixed-queue.cjs' -import { PriorityQueue } from '../lib/priority-queue.cjs' -import { defaultBucketSize } from '../lib/utility-types.cjs' +import { FixedPriorityQueue } from '../../lib/queues/fixed-priority-queue.cjs' +import { FixedQueue } from '../../lib/queues/fixed-queue.cjs' +import { PriorityQueue } from '../../lib/queues/priority-queue.cjs' +import { defaultBucketSize } from '../../lib/queues/queue-types.cjs' describe('Priority queue test suite', () => { it('Verify constructor() behavior', () => {