From: Jérôme Benoit Date: Thu, 4 May 2023 22:34:56 +0000 (+0200) Subject: docs: add comments in queue implementation X-Git-Tag: v2.4.12~21 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a0d415448af2d5a72c5349d3911f1bcdc3ff0fd2;p=poolifier.git docs: add comments in queue implementation Signed-off-by: Jérôme Benoit --- diff --git a/.eslintrc.js b/.eslintrc.js index 58450447..d7004cc5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -49,6 +49,7 @@ module.exports = defineConfig({ 'ctx', 'deprecations', 'dequeue', + 'dequeued', 'ecma', 'enqueue', 'enum', diff --git a/src/queue.ts b/src/queue.ts index 35f9025e..a2a61381 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -2,6 +2,8 @@ /** * Queue + * + * @typeParam T - Type of queue items. */ export class Queue { private items: Record @@ -14,16 +16,34 @@ export class Queue { this.tail = 0 } + /** + * 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 } + /** + * Dequeue an item. + * + * @returns The dequeued item. + * @returns `undefined` if the queue is empty. + */ public dequeue (): T | undefined { if (this.size <= 0) return undefined const item = this.items[this.head]