X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fqueue.ts;h=dab9f04c2783f5a5aa039a3af894780f1b5c639f;hb=b25a42cd89afca28b53dbecbec422595155ddfbc;hp=e7c9dd11fc0332ccbba845e9bd663d4bc682c523;hpb=e102732c0e3966b81834b2c0bdd087eb051162ad;p=poolifier.git diff --git a/src/queue.ts b/src/queue.ts index e7c9dd11..dab9f04c 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 + } + } + } + } }