Migrate to private class properties/methods
This commit is contained in:
@@ -16,32 +16,30 @@ import { once, noop } from 'lodash';
|
||||
* See the tests to see how this works.
|
||||
*/
|
||||
export class AsyncQueue<T> implements AsyncIterable<T> {
|
||||
private onAdd: () => void = noop;
|
||||
|
||||
private queue: Array<T> = [];
|
||||
|
||||
private isReading = false;
|
||||
#onAdd: () => void = noop;
|
||||
#queue: Array<T> = [];
|
||||
#isReading = false;
|
||||
|
||||
add(value: Readonly<T>): void {
|
||||
this.queue.push(value);
|
||||
this.onAdd();
|
||||
this.#queue.push(value);
|
||||
this.#onAdd();
|
||||
}
|
||||
|
||||
async *[Symbol.asyncIterator](): AsyncIterator<T> {
|
||||
if (this.isReading) {
|
||||
if (this.#isReading) {
|
||||
throw new Error('Cannot iterate over a queue more than once');
|
||||
}
|
||||
this.isReading = true;
|
||||
this.#isReading = true;
|
||||
|
||||
while (true) {
|
||||
yield* this.queue;
|
||||
yield* this.#queue;
|
||||
|
||||
this.queue = [];
|
||||
this.#queue = [];
|
||||
|
||||
// We want to iterate over the queue in series.
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise<void>(resolve => {
|
||||
this.onAdd = once(resolve);
|
||||
this.#onAdd = once(resolve);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user