()
| 186 | } |
| 187 | |
| 188 | async cancel(): Promise<void> { |
| 189 | // Force exit if previous cancel is still on-going |
| 190 | // for example when user does 'CTRL+c' twice in row |
| 191 | const force = this._isCancelling |
| 192 | |
| 193 | // Set flag to prevent new tasks from being queued |
| 194 | this._isCancelling = true |
| 195 | |
| 196 | const pendingTasks = this.queue.splice(0) |
| 197 | |
| 198 | if (pendingTasks.length) { |
| 199 | const error = new Error('Cancelled') |
| 200 | pendingTasks.forEach(task => task.resolver.reject(error)) |
| 201 | } |
| 202 | |
| 203 | await Promise.all(this.activeTasks.map(task => task.cancelTask({ force }))) |
| 204 | this.activeTasks = [] |
| 205 | |
| 206 | await Promise.all(this.sharedRunners.map(runner => runner.stop())) |
| 207 | this.sharedRunners = [] |
| 208 | |
| 209 | await Promise.all(this.exitPromises) |
| 210 | this.exitPromises = [] |
| 211 | |
| 212 | this.workerIds.forEach((_, id) => this.freeWorkerId(id)) |
| 213 | |
| 214 | // Reset flag after cancellation completes |
| 215 | this._isCancelling = false |
| 216 | } |
| 217 | |
| 218 | async close(): Promise<void> { |
| 219 | await this.cancel() |
no test coverage detected