X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fqueue.ts;h=dba55f1cc90467ab5d27e55a9b37e9004da983b4;hb=23a6c28d3cc6edb778653a4d9d9a4d36f9a961c3;hp=e7c9dd11fc0332ccbba845e9bd663d4bc682c523;hpb=e102732c0e3966b81834b2c0bdd087eb051162ad;p=poolifier.git diff --git a/src/queue.ts b/src/queue.ts index e7c9dd11..dba55f1c 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -51,7 +51,7 @@ export class Queue { } /** - * Peek at the first item. + * Peeks at the first item. * * @returns The first item or `undefined` if the queue is empty. */ @@ -63,7 +63,7 @@ export class Queue { } /** - * Clear the queue. + * Clears the queue. */ public clear (): void { this.items = [] @@ -71,4 +71,32 @@ export class Queue { 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 { + 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 + } + } + } + } }