Skip to content

Commit d631ea8

Browse files
Add .drain() method (#12)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 0ac610d commit d631ea8

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

index.d.ts

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

36+
/**
37+
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 that, use `drain()` instead.
38+
39+
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.
40+
*/
3641
[Symbol.iterator](): IterableIterator<ValueType>;
3742

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

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ export default class Queue {
7575
current = current.next;
7676
}
7777
}
78+
79+
* drain() {
80+
let current;
81+
while ((current = this.dequeue()) !== undefined) {
82+
yield current;
83+
}
84+
}
7885
}

readme.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ 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 that, use [`drain()`](#drain) instead.
43+
44+
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.
4345

4446
#### `.enqueue(value)`
4547

@@ -57,6 +59,14 @@ Get the next value in the queue without removing it.
5759

5860
Returns the value or `undefined` if the queue is empty.
5961

62+
#### `.drain()`
63+
64+
Returns an iterator that dequeues items as you consume it.
65+
66+
This allows you to empty the queue while processing its items.
67+
68+
If you want to not remove items as you consume it, use the `Queue` object as an iterator.
69+
6070
#### `.clear()`
6171

6272
Clear the queue.

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ test('iterable', t => {
6969
queue.enqueue('🌈');
7070
t.deepEqual([...queue], ['🦄', '🌈']);
7171
});
72+
73+
test('.drain()', t => {
74+
const queue = new Queue();
75+
queue.enqueue('🦄');
76+
queue.enqueue('🌈');
77+
for (const _ of queue.drain()) {}
78+
t.deepEqual([...queue], []);
79+
t.is(queue.size, 0);
80+
})

0 commit comments

Comments
 (0)