MCPcopy
hub / github.com/sindresorhus/p-queue / add

Method add

source/index.ts:444–574  ·  view source on GitHub ↗
(function_: Task<TaskResultType>, options: Partial<EnqueueOptionsType> = {})

Source from the content-addressed store, hash-verified

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

Callers 14

addAllMethod · 0.95
addBenchmarkFunction · 0.80
bench.tsFile · 0.80
index.test-d.tsFile · 0.80
advanced.tsFile · 0.80
basic.tsFile · 0.80
mapperFunction · 0.80
debug.tsFile · 0.80
rate-limit.tsFile · 0.80
createTransitionsFunction · 0.80
strict.tsFile · 0.80
timeoutTaskFunction · 0.80

Calls 2

#tryToStartAnotherMethod · 0.95
enqueueMethod · 0.80

Tested by

no test coverage detected