| 32 | * but the promisies for this will still resolve in order they were inserted in the queue |
| 33 | */ |
| 34 | export class AutoQueue<T> implements IQueue { |
| 35 | private pendingPromise = false; |
| 36 | private queue: Queue<Action<T>>; |
| 37 | private _abort = false; |
| 38 | private _resolveIdle?: () => void; |
| 39 | // private processingTasks = 0; |
| 40 | |
| 41 | private eventEmitter = new EventEmitter2(); |
| 42 | |
| 43 | private runningTasks: Promise<void | T>[] = []; |
| 44 | |
| 45 | // Completed tasks that have completed before earlier tasks |
| 46 | private outOfOrderTasks: Record<number, {action: Action<T>; result?: T; error?: unknown}> = {}; |
| 47 | // Next index assigned to a task when pushing to the queue |
| 48 | private nextIndex = 0; |
| 49 | // Next task to resolve, used to order the outOfOrderTasks |
| 50 | private nextTask = 0; |
| 51 | // Flag to ensure processOutOfOrderTasks is not re-entrant |
| 52 | private isProcessingOutOfOrder = false; |
| 53 | |
| 54 | /** |
| 55 | * @param {number} capacity - The size limit of the queue, if undefined there is no limit |
| 56 | * @param {number} [concurrency=1] - The number of parallel tasks that can be processed at any one time. |
| 57 | * @param {number} [taskTimeoutSec=900] - A timeout for tasks to complete in. Units are seconds. Align with nodeConfig process timeout. |
| 58 | * @param {string} [name] - A name for the queue to help with debugging |
| 59 | * */ |
| 60 | constructor( |
| 61 | capacity?: number, |
| 62 | public concurrency = 1, |
| 63 | private taskTimeoutSec = 900, |
| 64 | protected name = 'Auto' |
| 65 | ) { |
| 66 | this.queue = new Queue<Action<T>>(capacity); |
| 67 | } |
| 68 | |
| 69 | get size(): number { |
| 70 | return this.queue.size + this.runningTasks.length + Object.keys(this.outOfOrderTasks).length; |
| 71 | } |
| 72 | |
| 73 | get capacity(): number | undefined { |
| 74 | return this.queue.capacity; |
| 75 | } |
| 76 | |
| 77 | get freeSpace(): number | undefined { |
| 78 | if (!this.capacity) return undefined; |
| 79 | return this.capacity - this.size; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * We don't want this function to be async |
| 84 | * If it is async it will return a promise that throws rather than throwing the function |
| 85 | */ |
| 86 | // eslint-disable-next-line @typescript-eslint/promise-function-async |
| 87 | put(item: Task<T>): Promise<T> { |
| 88 | return this.putMany([item])[0]; |
| 89 | } |
| 90 | |
| 91 | // eslint-disable-next-line @typescript-eslint/promise-function-async |
nothing calls this directly
no outgoing calls
no test coverage detected