Migrate to private class properties/methods

This commit is contained in:
Jamie Kyle
2025-01-14 11:11:52 -08:00
committed by GitHub
parent 7dbe57084b
commit aa9f53df57
100 changed files with 3795 additions and 3944 deletions

View File

@@ -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);
});
}
}