index.d.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import EventEmitter = require('eventemitter3');
  2. import { Queue, RunFunction } from './queue';
  3. import PriorityQueue from './priority-queue';
  4. import { QueueAddOptions, DefaultAddOptions, Options } from './options';
  5. declare type Task<TaskResultType> = (() => PromiseLike<TaskResultType>) | (() => TaskResultType);
  6. /**
  7. Promise queue with concurrency control.
  8. */
  9. export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions> extends EventEmitter<'active' | 'idle' | 'add' | 'next'> {
  10. private readonly _carryoverConcurrencyCount;
  11. private readonly _isIntervalIgnored;
  12. private _intervalCount;
  13. private readonly _intervalCap;
  14. private readonly _interval;
  15. private _intervalEnd;
  16. private _intervalId?;
  17. private _timeoutId?;
  18. private _queue;
  19. private readonly _queueClass;
  20. private _pendingCount;
  21. private _concurrency;
  22. private _isPaused;
  23. private _resolveEmpty;
  24. private _resolveIdle;
  25. private _timeout?;
  26. private readonly _throwOnTimeout;
  27. constructor(options?: Options<QueueType, EnqueueOptionsType>);
  28. private get _doesIntervalAllowAnother();
  29. private get _doesConcurrentAllowAnother();
  30. private _next;
  31. private _resolvePromises;
  32. private _onResumeInterval;
  33. private _isIntervalPaused;
  34. private _tryToStartAnother;
  35. private _initializeIntervalIfNeeded;
  36. private _onInterval;
  37. /**
  38. Executes all queued functions until it reaches the limit.
  39. */
  40. private _processQueue;
  41. get concurrency(): number;
  42. set concurrency(newConcurrency: number);
  43. /**
  44. Adds a sync or async task to the queue. Always returns a promise.
  45. */
  46. add<TaskResultType>(fn: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
  47. /**
  48. Same as `.add()`, but accepts an array of sync or async functions.
  49. @returns A promise that resolves when all functions are resolved.
  50. */
  51. addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: EnqueueOptionsType): Promise<TaskResultsType[]>;
  52. /**
  53. Start (or resume) executing enqueued tasks within concurrency limit. No need to call this if queue is not paused (via `options.autoStart = false` or by `.pause()` method.)
  54. */
  55. start(): this;
  56. /**
  57. Put queue execution on hold.
  58. */
  59. pause(): void;
  60. /**
  61. Clear the queue.
  62. */
  63. clear(): void;
  64. /**
  65. Can be called multiple times. Useful if you for example add additional items at a later time.
  66. @returns A promise that settles when the queue becomes empty.
  67. */
  68. onEmpty(): Promise<void>;
  69. /**
  70. The difference with `.onEmpty` is that `.onIdle` guarantees that all work from the queue has finished. `.onEmpty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
  71. @returns A promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
  72. */
  73. onIdle(): Promise<void>;
  74. /**
  75. Size of the queue.
  76. */
  77. get size(): number;
  78. /**
  79. Size of the queue, filtered by the given options.
  80. For example, this can be used to find the number of items remaining in the queue with a specific priority level.
  81. */
  82. sizeBy(options: Readonly<Partial<EnqueueOptionsType>>): number;
  83. /**
  84. Number of pending promises.
  85. */
  86. get pending(): number;
  87. /**
  88. Whether the queue is currently paused.
  89. */
  90. get isPaused(): boolean;
  91. get timeout(): number | undefined;
  92. /**
  93. Set the timeout for future operations.
  94. */
  95. set timeout(milliseconds: number | undefined);
  96. }
  97. export { Queue, QueueAddOptions, DefaultAddOptions, Options };