Skip to content

Commit f91feef

Browse files
author
Victor Fernandez
committed
refactor: move the destructive iterator to a new method
1 parent e22d2a5 commit f91feef

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,20 @@ export default class Queue<ValueType> implements Iterable<ValueType> {
3333
*/
3434
constructor();
3535

36+
/**
37+
Implements the iterable protocol, allowing the queue to be used in a for...of loop.
38+
Iterates through the queue without removing elements.
39+
If you want to remove the items as you consume it, use `drain()` instead.
40+
*/
3641
[Symbol.iterator](): IterableIterator<ValueType>;
3742

43+
/**
44+
Returns an iterator that dequeues items as you consume it.
45+
This allows you to empty the queue while processing its items.
46+
If you want to *not* remove the items as you consume it, use the queue object as iterator.
47+
*/
48+
drain(): IterableIterator<ValueType>;
49+
3850
/**
3951
Add a value to the queue.
4052
*/

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ export default class Queue {
6868
}
6969

7070
* [Symbol.iterator]() {
71+
let current = this.#head;
72+
73+
while (current) {
74+
yield current.value;
75+
current = current.next;
76+
}
77+
}
78+
79+
* drain() {
7180
let current;
7281
while ((current = this.dequeue()) !== undefined) {
7382
yield current;

readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ console.log(queue.dequeue());
3939

4040
### `queue = new Queue()`
4141

42-
The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a “for…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.
42+
The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a “for…of” loop. Using the iterator will _not_ remove the items from the queue. If you want to remove the items as you consume it,
43+
use [`drain()`](#drain) instead.
44+
45+
You can also use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.
4346

4447
#### `.enqueue(value)`
4548

@@ -57,6 +60,12 @@ Get the next value in the queue without removing it.
5760

5861
Returns the value or `undefined` if the queue is empty.
5962

63+
#### `.drain()`
64+
65+
Returns an iterator that dequeues items as you consume it.
66+
67+
This allows you to empty the queue while processing its items.
68+
6069
#### `.clear()`
6170

6271
Clear the queue.

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ test('iterable', t => {
7070
t.deepEqual([...queue], ['🦄', '🌈']);
7171
});
7272

73-
test('iterable dequeues (issue 11)', t => {
73+
test('drain iterable dequeues (issue 11)', t => {
7474
const queue = new Queue();
7575
queue.enqueue('🦄');
7676
queue.enqueue('🌈');
77-
for (const _ of queue) {}
77+
for (const _ of queue.drain()) {}
7878
t.deepEqual([...queue], []);
7979
t.is(queue.size, 0);
8080
})

0 commit comments

Comments
 (0)