Download apps/readest-app/src/utils/queue.ts from echodict/readest: direct link, hf CLI and curl.
- Browser
- Download file 833 Bytes
-
https://huggingface.co/datasets/echodict/readest/resolve/main/apps/readest-app/src/utils/queue.ts
- Command line
-
hf download hf://datasets/echodict/readest/apps/readest-app/src/utils/queue.ts
-
curl -L -o queue.ts https://huggingface.co/datasets/echodict/readest/resolve/main/apps/readest-app/src/utils/queue.ts
833 Bytes
| 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); | |
| } | |
| }); | |
| }); | |
| } | |
| } | |