forked from akai/readest
Initial implementation of TTS with Web Speech API (#120)
* Initial implementation of TTS with Web Speech API * Add translations for annotator tools
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
export class AsyncQueue<T> {
|
||||
private queue: T[] = [];
|
||||
private resolves: ((value: void) => void)[] = [];
|
||||
private done = false;
|
||||
|
||||
enqueue(item: T) {
|
||||
this.queue.push(item);
|
||||
if (this.resolves.length > 0) {
|
||||
const resolve = this.resolves.shift();
|
||||
resolve?.();
|
||||
}
|
||||
}
|
||||
|
||||
finish() {
|
||||
this.done = true;
|
||||
while (this.resolves.length > 0) {
|
||||
const resolve = this.resolves.shift();
|
||||
resolve?.();
|
||||
}
|
||||
}
|
||||
|
||||
async dequeue(): Promise<T | null> {
|
||||
if (this.queue.length > 0) {
|
||||
return this.queue.shift()!;
|
||||
}
|
||||
if (this.done) {
|
||||
return null;
|
||||
}
|
||||
return new Promise<T | null>((resolve) => {
|
||||
this.resolves.push(() => {
|
||||
if (this.queue.length > 0) {
|
||||
resolve(this.queue.shift()!);
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user