X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fqueue.ts;h=7682f65f952136743c1f81724a26e53264ac18ed;hb=027c221543826da4df348775499148557d3d437e;hp=35f9025e121bb861729df62a0064a3aa0176387c;hpb=9f8f5811bfbaae8f44976880045faef15bd0387f;p=poolifier.git diff --git a/src/queue.ts b/src/queue.ts index 35f9025e..7682f65f 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,33 @@ 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 or `undefined` if the queue is empty. + */ public dequeue (): T | undefined { if (this.size <= 0) return undefined const item = this.items[this.head] @@ -36,4 +55,12 @@ export class Queue { } return item } + + /** + * Peek at the first item. + */ + public peek (): T | undefined { + if (this.size <= 0) return undefined + return this.items[this.head] + } }