(function_: Task<TaskResultType>, options: Partial<EnqueueOptionsType> = {})
| 442 | */ |
| 443 | async add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>; |
| 444 | async add<TaskResultType>(function_: Task<TaskResultType>, options: Partial<EnqueueOptionsType> = {}): Promise<TaskResultType> { |
| 445 | // Create a copy to avoid mutating the original options object |
| 446 | options = { |
| 447 | timeout: this.timeout, |
| 448 | ...options, |
| 449 | // Assign unique ID if not provided |
| 450 | id: options.id ?? (this.#idAssigner++).toString(), |
| 451 | }; |
| 452 | |
| 453 | return new Promise((resolve, reject) => { |
| 454 | // Create a unique symbol for tracking this task |
| 455 | const taskSymbol = Symbol(`task-${options.id}`); |
| 456 | |
| 457 | let cleanupQueueAbortHandler = () => undefined; |
| 458 | const run = async () => { |
| 459 | // Task is now running — remove the queued-state abort listener |
| 460 | cleanupQueueAbortHandler(); |
| 461 | |
| 462 | this.#pending++; |
| 463 | |
| 464 | // Track this running task |
| 465 | this.#runningTasks.set(taskSymbol, { |
| 466 | id: options.id, |
| 467 | priority: options.priority ?? 0, // Match priority-queue default |
| 468 | startTime: Date.now(), |
| 469 | timeout: options.timeout, |
| 470 | }); |
| 471 | |
| 472 | let eventListener: (() => void) | undefined; |
| 473 | |
| 474 | try { |
| 475 | // Check abort signal - if aborted, need to decrement the counter |
| 476 | // that was incremented in tryToStartAnother |
| 477 | try { |
| 478 | options.signal?.throwIfAborted(); |
| 479 | } catch (error) { |
| 480 | this.#rollbackIntervalConsumption(); |
| 481 | |
| 482 | // Clean up tracking before throwing |
| 483 | this.#runningTasks.delete(taskSymbol); |
| 484 | |
| 485 | throw error; |
| 486 | } |
| 487 | |
| 488 | this.#lastExecutionTime = Date.now(); |
| 489 | |
| 490 | let operation = function_({signal: options.signal}); |
| 491 | |
| 492 | if (options.timeout) { |
| 493 | operation = pTimeout(Promise.resolve(operation), { |
| 494 | milliseconds: options.timeout, |
| 495 | message: `Task timed out after ${options.timeout}ms (queue has ${this.#pending} running, ${this.#queue.size} waiting)`, |
| 496 | }); |
| 497 | } |
| 498 | |
| 499 | if (options.signal) { |
| 500 | const {signal} = options; |
| 501 |
no test coverage detected