Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1x 3x 3x 3x 11x 11x 3x 7x 7x 7x 3x 7x 7x 7x 3x 18x 18x 3x 7x 7x 3x 7x 7x 7x 7x 3x 3x | export class AsyncBlockingQueue<T> implements AsyncIterable<T> {
private promises: Promise<T>[] = [];
private resolvers: ((value: T) => void)[] = [];
get length(): number {
return this.promises.length - this.resolvers.length;
}
enqueue(element: T) {
if (!this.isBlocked()) this.add();
this.resolvers.shift()!(element);
}
dequeue(): Promise<T> {
if (this.isEmpty()) this.add();
return this.promises.shift()!;
}
isEmpty(): boolean {
return this.promises.length === 0;
}
isBlocked(): boolean {
return this.resolvers.length > 0;
}
private add() {
this.promises.push(new Promise(resolve => {
this.resolvers.push(resolve);
}));
}
[Symbol.asyncIterator](): AsyncIterator<T> {
return {
next: async (): Promise<IteratorResult<T>> => {
const element = await this.dequeue();
return {
done: false,
value: element,
};
},
};
}
}
|