(taskFunction: TaskRunFunction<ThreadType, any>)
| 317 | } |
| 318 | |
| 319 | public queue(taskFunction: TaskRunFunction<ThreadType, any>) { |
| 320 | const { maxQueuedJobs = Infinity } = this.options |
| 321 | |
| 322 | if (this.isClosing) { |
| 323 | throw Error(`Cannot schedule pool tasks after terminate() has been called.`) |
| 324 | } |
| 325 | if (this.initErrors.length > 0) { |
| 326 | throw this.initErrors[0] |
| 327 | } |
| 328 | |
| 329 | const taskID = this.nextTaskID++ |
| 330 | const taskCompletion = this.taskCompletion(taskID) |
| 331 | |
| 332 | taskCompletion.catch((error) => { |
| 333 | // Prevent unhandled rejections here as we assume the user will use |
| 334 | // `pool.completed()`, `pool.settled()` or `task.catch()` to handle errors |
| 335 | this.debug(`Task #${taskID} errored:`, error) |
| 336 | }) |
| 337 | |
| 338 | const task: QueuedTask<ThreadType, any> = { |
| 339 | id: taskID, |
| 340 | run: taskFunction, |
| 341 | cancel: () => { |
| 342 | if (this.taskQueue.indexOf(task) === -1) return |
| 343 | this.taskQueue = this.taskQueue.filter(someTask => someTask !== task) |
| 344 | this.eventSubject.next({ |
| 345 | type: PoolEventType.taskCanceled, |
| 346 | taskID: task.id |
| 347 | }) |
| 348 | }, |
| 349 | then: taskCompletion.then.bind(taskCompletion) |
| 350 | } |
| 351 | |
| 352 | if (this.taskQueue.length >= maxQueuedJobs) { |
| 353 | throw Error( |
| 354 | "Maximum number of pool tasks queued. Refusing to queue another one.\n" + |
| 355 | "This usually happens for one of two reasons: We are either at peak " + |
| 356 | "workload right now or some tasks just won't finish, thus blocking the pool." |
| 357 | ) |
| 358 | } |
| 359 | |
| 360 | this.debug(`Queueing task #${task.id}...`) |
| 361 | this.taskQueue.push(task) |
| 362 | |
| 363 | this.eventSubject.next({ |
| 364 | type: PoolEventType.taskQueued, |
| 365 | taskID: task.id |
| 366 | }) |
| 367 | |
| 368 | this.scheduleWork() |
| 369 | return task |
| 370 | } |
| 371 | |
| 372 | public async terminate(force?: boolean) { |
| 373 | this.isClosing = true |
nothing calls this directly
no test coverage detected