(promiseFn: () => Promise<T>)
| 19 | } |
| 20 | |
| 21 | enqueue<T>(promiseFn: () => Promise<T>): Promise<T> { |
| 22 | let taskResolve: (value: T | PromiseLike<T>) => void |
| 23 | let taskReject: (reason?: any) => void |
| 24 | |
| 25 | const taskPromise = new Promise((resolve, reject) => { |
| 26 | taskResolve = resolve |
| 27 | taskReject = reject |
| 28 | }) as Promise<T> |
| 29 | |
| 30 | const task = async () => { |
| 31 | try { |
| 32 | this.#runningCount++ |
| 33 | const result = await promiseFn() |
| 34 | taskResolve(result) |
| 35 | } catch (error) { |
| 36 | taskReject(error) |
| 37 | } finally { |
| 38 | this.#runningCount-- |
| 39 | this.#processNext() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | const enqueueResult = { promiseFn: taskPromise, task } |
| 44 | // wonder if we should take a LIFO approach here |
| 45 | this.#queue.push(enqueueResult) |
| 46 | this.#processNext() |
| 47 | |
| 48 | return taskPromise |
| 49 | } |
| 50 | |
| 51 | bump(promiseFn: Promise<any>) { |
| 52 | const index = this.#queue.findIndex((item) => item.promiseFn === promiseFn) |
no test coverage detected