()
| 135 | } |
| 136 | |
| 137 | private async take(): Promise<void> { |
| 138 | if (this.pendingPromise) return; |
| 139 | if (this._abort) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | while (!this._abort) { |
| 144 | const action = this.queue.take(); |
| 145 | |
| 146 | // No more actions to start, take will be called again when new items are pushed |
| 147 | if (!action) break; |
| 148 | |
| 149 | this.pendingPromise = true; |
| 150 | |
| 151 | const p = timeout( |
| 152 | Promise.resolve(action.task()), |
| 153 | this.taskTimeoutSec, |
| 154 | `${this.name} Queue process task timeout in ${this.taskTimeoutSec} seconds. Please increase --timeout` |
| 155 | ) |
| 156 | .then((result) => { |
| 157 | // Queue was flushed while task was running, we ned to discard now |
| 158 | if (this.nextTask > action.index) { |
| 159 | action.reject(new TaskFlushedError(this.name)); |
| 160 | return; |
| 161 | } |
| 162 | this.outOfOrderTasks[action.index] = {action, result}; |
| 163 | }) |
| 164 | .catch((error) => { |
| 165 | // Queue was flushed while task was running, we ned to discard now |
| 166 | if (this.nextTask > action.index) { |
| 167 | action.reject(new TaskFlushedError(this.name)); |
| 168 | return; |
| 169 | } |
| 170 | this.outOfOrderTasks[action.index] = {action, error}; |
| 171 | }) |
| 172 | .finally(() => { |
| 173 | const index = this.runningTasks.indexOf(p); |
| 174 | // If the index is -1 then the queue will have been flushed |
| 175 | if (index >= 0) { |
| 176 | this.processOutOfOrderTasks(); |
| 177 | this.runningTasks.splice(index, 1); |
| 178 | } |
| 179 | }); |
| 180 | |
| 181 | this.runningTasks.push(p); |
| 182 | |
| 183 | if (this.runningTasks.length >= this.concurrency) { |
| 184 | // Load up more when any task completes |
| 185 | await Promise.any(this.runningTasks); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Processed all items in the queue |
| 190 | this._resolveIdle?.(); |
| 191 | this._resolveIdle = undefined; |
| 192 | this.pendingPromise = false; |
| 193 | } |
| 194 |
no test coverage detected