Skip to content

Commit

Permalink
Add Queue typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ooflorent committed Apr 23, 2018
1 parent 77132ef commit 6b583e3
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/util/Queue.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
"use strict";

module.exports = class Queue {
/**
* @template T
*/
class Queue {
/**
* @param {IterableIterator<T>=} items The initial elements.
*/
constructor(items) {
/** @private @type {Set<T>} */
this.set = new Set(items);
/** @private @type {Iterator<T>} */
this.iterator = this.set[Symbol.iterator]();
}

/**
* Returns the number of elements in this queue.
* @return {number} The number of elements in this queue.
*/
get length() {
return this.set.size;
}

/**
* Appends the specified element to this queue.
* @param {T} item The element to add.
* @return {void}
*/
enqueue(item) {
this.set.add(item);
}

/**
* Retrieves and removes the head of this queue.
* @return {T | undefined} The head of the queue of `undefined` if this queue is empty.
*/
dequeue() {
const result = this.iterator.next();
if (result.done) return undefined;
this.set.delete(result.value);
return result.value;
}
};
}

module.exports = Queue;

0 comments on commit 6b583e3

Please sign in to comment.