Skip to content

Commit 6b583e3

Browse files
committed
Add Queue typing
1 parent 77132ef commit 6b583e3

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/util/Queue.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
"use strict";
22

3-
module.exports = class Queue {
3+
/**
4+
* @template T
5+
*/
6+
class Queue {
7+
/**
8+
* @param {IterableIterator<T>=} items The initial elements.
9+
*/
410
constructor(items) {
11+
/** @private @type {Set<T>} */
512
this.set = new Set(items);
13+
/** @private @type {Iterator<T>} */
614
this.iterator = this.set[Symbol.iterator]();
715
}
816

17+
/**
18+
* Returns the number of elements in this queue.
19+
* @return {number} The number of elements in this queue.
20+
*/
921
get length() {
1022
return this.set.size;
1123
}
1224

25+
/**
26+
* Appends the specified element to this queue.
27+
* @param {T} item The element to add.
28+
* @return {void}
29+
*/
1330
enqueue(item) {
1431
this.set.add(item);
1532
}
1633

34+
/**
35+
* Retrieves and removes the head of this queue.
36+
* @return {T | undefined} The head of the queue of `undefined` if this queue is empty.
37+
*/
1738
dequeue() {
1839
const result = this.iterator.next();
1940
if (result.done) return undefined;
2041
this.set.delete(result.value);
2142
return result.value;
2243
}
23-
};
44+
}
45+
46+
module.exports = Queue;

0 commit comments

Comments
 (0)