@common.js/p-queue 7.3.0 β†’ 7.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  The [p-queue](https://www.npmjs.com/package/p-queue) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-queue@7.3.0](https://www.npmjs.com/package/p-queue/v/7.3.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-queue@7.3.3](https://www.npmjs.com/package/p-queue/v/7.3.3) using https://github.com/etienne-martin/common.js.
package/dist/index.d.ts CHANGED
@@ -2,13 +2,13 @@ import EventEmitter from 'eventemitter3';
2
2
  import { Queue, RunFunction } from './queue.js';
3
3
  import PriorityQueue from './priority-queue.js';
4
4
  import { QueueAddOptions, Options, TaskOptions } from './options.js';
5
- declare type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType);
5
+ type Task<TaskResultType> = ((options: TaskOptions) => PromiseLike<TaskResultType>) | ((options: TaskOptions) => TaskResultType);
6
6
  /**
7
7
  The error thrown by `queue.add()` when a job is aborted before it is run. See `signal`.
8
8
  */
9
9
  export declare class AbortError extends Error {
10
10
  }
11
- declare type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error';
11
+ type EventName = 'active' | 'idle' | 'empty' | 'add' | 'next' | 'completed' | 'error';
12
12
  /**
13
13
  Promise queue with concurrency control.
14
14
  */
@@ -26,13 +26,19 @@ export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsT
26
26
  /**
27
27
  Adds a sync or async task to the queue. Always returns a promise.
28
28
  */
29
- add<TaskResultType>(fn: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>;
29
+ add<TaskResultType>(function_: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType | void>;
30
+ add<TaskResultType>(function_: Task<TaskResultType>, options: {
31
+ throwOnTimeout: true;
32
+ } & Exclude<EnqueueOptionsType, 'throwOnTimeout'>): Promise<TaskResultType>;
30
33
  /**
31
34
  Same as `.add()`, but accepts an array of sync or async functions.
32
35
 
33
36
  @returns A promise that resolves when all functions are resolved.
34
37
  */
35
- addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: EnqueueOptionsType): Promise<TaskResultsType[]>;
38
+ addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: Partial<EnqueueOptionsType>): Promise<Array<TaskResultsType | void>>;
39
+ addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: {
40
+ throwOnTimeout: true;
41
+ } & Partial<Exclude<EnqueueOptionsType, 'throwOnTimeout'>>): Promise<TaskResultsType[]>;
36
42
  /**
37
43
  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.)
38
44
  */
package/dist/index.js CHANGED
@@ -133,6 +133,13 @@ function _inherits(subClass, superClass) {
133
133
  });
134
134
  if (superClass) _setPrototypeOf(subClass, superClass);
135
135
  }
136
+ function _instanceof(left, right) {
137
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
138
+ return !!right[Symbol.hasInstance](left);
139
+ } else {
140
+ return left instanceof right;
141
+ }
142
+ }
136
143
  function _interopRequireDefault(obj) {
137
144
  return obj && obj.__esModule ? obj : {
138
145
  default: obj
@@ -368,8 +375,7 @@ var __classPrivateFieldGet = (void 0) && (void 0).__classPrivateFieldGet || func
368
375
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
369
376
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
370
377
  };
371
- var _PQueue_instances, _PQueue_carryoverConcurrencyCount, _PQueue_isIntervalIgnored, _PQueue_intervalCount, _PQueue_intervalCap, _PQueue_interval, _PQueue_intervalEnd, _PQueue_intervalId, _PQueue_timeoutId, _PQueue_queue, _PQueue_queueClass, _PQueue_pendingCount, _PQueue_concurrency, _PQueue_isPaused, _PQueue_throwOnTimeout, _PQueue_doesIntervalAllowAnother_get, _PQueue_doesConcurrentAllowAnother_get, _PQueue_next, _PQueue_emitEvents, _PQueue_onResumeInterval, _PQueue_isIntervalPaused_get, _PQueue_tryToStartAnother, _PQueue_initializeIntervalIfNeeded, _PQueue_onInterval, _PQueue_processQueue, _PQueue_onEvent;
372
- var timeoutError = new _pTimeout.TimeoutError();
378
+ var _PQueue_instances, _PQueue_carryoverConcurrencyCount, _PQueue_isIntervalIgnored, _PQueue_intervalCount, _PQueue_intervalCap, _PQueue_interval, _PQueue_intervalEnd, _PQueue_intervalId, _PQueue_timeoutId, _PQueue_queue, _PQueue_queueClass, _PQueue_pending, _PQueue_concurrency, _PQueue_isPaused, _PQueue_throwOnTimeout, _PQueue_doesIntervalAllowAnother_get, _PQueue_doesConcurrentAllowAnother_get, _PQueue_next, _PQueue_onResumeInterval, _PQueue_isIntervalPaused_get, _PQueue_tryToStartAnother, _PQueue_initializeIntervalIfNeeded, _PQueue_onInterval, _PQueue_processQueue, _PQueue_throwOnAbort, _PQueue_onEvent;
373
379
  var AbortError = /*#__PURE__*/ function(Error1) {
374
380
  "use strict";
375
381
  _inherits(AbortError, Error1);
@@ -400,7 +406,7 @@ var PQueue = /*#__PURE__*/ function(EventEmitter) {
400
406
  _PQueue_timeoutId.set(_assertThisInitialized(_this), void 0);
401
407
  _PQueue_queue.set(_assertThisInitialized(_this), void 0);
402
408
  _PQueue_queueClass.set(_assertThisInitialized(_this), void 0);
403
- _PQueue_pendingCount.set(_assertThisInitialized(_this), 0);
409
+ _PQueue_pending.set(_assertThisInitialized(_this), 0);
404
410
  // The `!` is needed because of https://github.com/microsoft/TypeScript/issues/32194
405
411
  _PQueue_concurrency.set(_assertThisInitialized(_this), void 0);
406
412
  _PQueue_isPaused.set(_assertThisInitialized(_this), void 0);
@@ -458,84 +464,90 @@ var PQueue = /*#__PURE__*/ function(EventEmitter) {
458
464
  },
459
465
  {
460
466
  key: "add",
461
- value: /**
462
- Adds a sync or async task to the queue. Always returns a promise.
463
- */ function add(fn) {
467
+ value: function add(function_) {
464
468
  var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
465
469
  var _this = this;
466
470
  return _asyncToGenerator(function() {
467
471
  return __generator(this, function(_state) {
472
+ options = _objectSpread({
473
+ timeout: _this.timeout,
474
+ throwOnTimeout: __classPrivateFieldGet(_this, _PQueue_throwOnTimeout, "f")
475
+ }, options);
468
476
  return [
469
477
  2,
470
478
  new Promise(function(resolve, reject) {
471
- var run = function() {
472
- var _ref = _asyncToGenerator(function() {
473
- var _a, _b, _c, operation, result, error;
474
- return __generator(this, function(_state) {
475
- switch(_state.label){
476
- case 0:
477
- __classPrivateFieldSet(_this, _PQueue_pendingCount, (_b = __classPrivateFieldGet(_this, _PQueue_pendingCount, "f"), _b++, _b), "f");
478
- __classPrivateFieldSet(_this, _PQueue_intervalCount, (_c = __classPrivateFieldGet(_this, _PQueue_intervalCount, "f"), _c++, _c), "f");
479
- _state.label = 1;
480
- case 1:
481
- _state.trys.push([
482
- 1,
483
- 3,
484
- ,
485
- 4
479
+ __classPrivateFieldGet(_this, _PQueue_queue, "f").enqueue(/*#__PURE__*/ _asyncToGenerator(function() {
480
+ var _a, _b, _c, operation, result, error;
481
+ return __generator(this, function(_state) {
482
+ switch(_state.label){
483
+ case 0:
484
+ __classPrivateFieldSet(_this, _PQueue_pending, (_b = __classPrivateFieldGet(_this, _PQueue_pending, "f"), _b++, _b), "f");
485
+ __classPrivateFieldSet(_this, _PQueue_intervalCount, (_c = __classPrivateFieldGet(_this, _PQueue_intervalCount, "f"), _c++, _c), "f");
486
+ _state.label = 1;
487
+ case 1:
488
+ _state.trys.push([
489
+ 1,
490
+ 3,
491
+ 4,
492
+ 5
493
+ ]);
494
+ // TODO: Use options.signal?.throwIfAborted() when targeting Node.js 18
495
+ if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
496
+ // TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err)
497
+ throw new AbortError("The task was aborted.");
498
+ }
499
+ operation = function_({
500
+ signal: options.signal
501
+ });
502
+ if (options.timeout) {
503
+ operation = (0, _pTimeout.default)(Promise.resolve(operation), options.timeout);
504
+ }
505
+ if (options.signal) {
506
+ operation = Promise.race([
507
+ operation,
508
+ __classPrivateFieldGet(_this, _PQueue_instances, "m", _PQueue_throwOnAbort).call(_this, options.signal)
486
509
  ]);
487
- if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
488
- // TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err)
489
- reject(new AbortError("The task was aborted."));
490
- return [
491
- 2
492
- ];
493
- }
494
- operation = _this.timeout === undefined && options.timeout === undefined ? fn({
495
- signal: options.signal
496
- }) : (0, _pTimeout.default)(Promise.resolve(fn({
497
- signal: options.signal
498
- })), options.timeout === undefined ? _this.timeout : options.timeout, function() {
499
- if (options.throwOnTimeout === undefined ? __classPrivateFieldGet(_this, _PQueue_throwOnTimeout, "f") : options.throwOnTimeout) {
500
- reject(timeoutError);
501
- }
502
- return undefined;
503
- });
504
- return [
505
- 4,
506
- operation
507
- ];
508
- case 2:
509
- result = _state.sent();
510
- resolve(result);
511
- _this.emit("completed", result);
512
- return [
513
- 3,
514
- 4
515
- ];
516
- case 3:
517
- error = _state.sent();
518
- reject(error);
519
- _this.emit("error", error);
520
- return [
521
- 3,
522
- 4
523
- ];
524
- case 4:
525
- __classPrivateFieldGet(_this, _PQueue_instances, "m", _PQueue_next).call(_this);
510
+ }
511
+ return [
512
+ 4,
513
+ operation
514
+ ];
515
+ case 2:
516
+ result = _state.sent();
517
+ resolve(result);
518
+ _this.emit("completed", result);
519
+ return [
520
+ 3,
521
+ 5
522
+ ];
523
+ case 3:
524
+ error = _state.sent();
525
+ if (_instanceof(error, _pTimeout.TimeoutError) && !options.throwOnTimeout) {
526
+ resolve();
526
527
  return [
527
528
  2
528
529
  ];
529
- }
530
- });
530
+ }
531
+ reject(error);
532
+ _this.emit("error", error);
533
+ return [
534
+ 3,
535
+ 5
536
+ ];
537
+ case 4:
538
+ __classPrivateFieldGet(_this, _PQueue_instances, "m", _PQueue_next).call(_this);
539
+ return [
540
+ 7
541
+ ];
542
+ case 5:
543
+ return [
544
+ 2
545
+ ];
546
+ }
531
547
  });
532
- return function run() {
533
- return _ref.apply(this, arguments);
534
- };
535
- }();
536
- __classPrivateFieldGet(_this, _PQueue_queue, "f").enqueue(run, options);
537
- __classPrivateFieldGet(_this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(_this);
548
+ }), options);
538
549
  _this.emit("add");
550
+ __classPrivateFieldGet(_this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(_this);
539
551
  })
540
552
  ];
541
553
  });
@@ -544,11 +556,7 @@ var PQueue = /*#__PURE__*/ function(EventEmitter) {
544
556
  },
545
557
  {
546
558
  key: "addAll",
547
- value: /**
548
- Same as `.add()`, but accepts an array of sync or async functions.
549
-
550
- @returns A promise that resolves when all functions are resolved.
551
- */ function addAll(functions, options) {
559
+ value: function addAll(functions, options) {
552
560
  var _this = this;
553
561
  return _asyncToGenerator(function() {
554
562
  return __generator(this, function(_state) {
@@ -682,7 +690,7 @@ var PQueue = /*#__PURE__*/ function(EventEmitter) {
682
690
  switch(_state.label){
683
691
  case 0:
684
692
  // Instantly resolve if none pending and if nothing else is queued
685
- if (__classPrivateFieldGet(_this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(_this, _PQueue_queue, "f").size === 0) {
693
+ if (__classPrivateFieldGet(_this, _PQueue_pending, "f") === 0 && __classPrivateFieldGet(_this, _PQueue_queue, "f").size === 0) {
686
694
  return [
687
695
  2
688
696
  ];
@@ -725,7 +733,7 @@ var PQueue = /*#__PURE__*/ function(EventEmitter) {
725
733
  get: /**
726
734
  Number of running items (no longer in the queue).
727
735
  */ function get() {
728
- return __classPrivateFieldGet(this, _PQueue_pendingCount, "f");
736
+ return __classPrivateFieldGet(this, _PQueue_pending, "f");
729
737
  }
730
738
  },
731
739
  {
@@ -739,20 +747,15 @@ var PQueue = /*#__PURE__*/ function(EventEmitter) {
739
747
  ]);
740
748
  return PQueue;
741
749
  }(_eventemitter3.default);
742
- _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = new WeakMap(), _PQueue_intervalCount = new WeakMap(), _PQueue_intervalCap = new WeakMap(), _PQueue_interval = new WeakMap(), _PQueue_intervalEnd = new WeakMap(), _PQueue_intervalId = new WeakMap(), _PQueue_timeoutId = new WeakMap(), _PQueue_queue = new WeakMap(), _PQueue_queueClass = new WeakMap(), _PQueue_pendingCount = new WeakMap(), _PQueue_concurrency = new WeakMap(), _PQueue_isPaused = new WeakMap(), _PQueue_throwOnTimeout = new WeakMap(), _PQueue_instances = new WeakSet(), _PQueue_doesIntervalAllowAnother_get = function _PQueue_doesIntervalAllowAnother_get() {
750
+ _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = new WeakMap(), _PQueue_intervalCount = new WeakMap(), _PQueue_intervalCap = new WeakMap(), _PQueue_interval = new WeakMap(), _PQueue_intervalEnd = new WeakMap(), _PQueue_intervalId = new WeakMap(), _PQueue_timeoutId = new WeakMap(), _PQueue_queue = new WeakMap(), _PQueue_queueClass = new WeakMap(), _PQueue_pending = new WeakMap(), _PQueue_concurrency = new WeakMap(), _PQueue_isPaused = new WeakMap(), _PQueue_throwOnTimeout = new WeakMap(), _PQueue_instances = new WeakSet(), _PQueue_doesIntervalAllowAnother_get = function _PQueue_doesIntervalAllowAnother_get() {
743
751
  return __classPrivateFieldGet(this, _PQueue_isIntervalIgnored, "f") || __classPrivateFieldGet(this, _PQueue_intervalCount, "f") < __classPrivateFieldGet(this, _PQueue_intervalCap, "f");
744
752
  }, _PQueue_doesConcurrentAllowAnother_get = function _PQueue_doesConcurrentAllowAnother_get() {
745
- return __classPrivateFieldGet(this, _PQueue_pendingCount, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f");
753
+ return __classPrivateFieldGet(this, _PQueue_pending, "f") < __classPrivateFieldGet(this, _PQueue_concurrency, "f");
746
754
  }, _PQueue_next = function _PQueue_next() {
747
755
  var _a;
748
- __classPrivateFieldSet(this, _PQueue_pendingCount, (_a = __classPrivateFieldGet(this, _PQueue_pendingCount, "f"), _a--, _a), "f");
756
+ __classPrivateFieldSet(this, _PQueue_pending, (_a = __classPrivateFieldGet(this, _PQueue_pending, "f"), _a--, _a), "f");
749
757
  __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this);
750
758
  this.emit("next");
751
- }, _PQueue_emitEvents = function _PQueue_emitEvents() {
752
- this.emit("empty");
753
- if (__classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0) {
754
- this.emit("idle");
755
- }
756
759
  }, _PQueue_onResumeInterval = function _PQueue_onResumeInterval() {
757
760
  __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_onInterval).call(this);
758
761
  __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_initializeIntervalIfNeeded).call(this);
@@ -765,7 +768,7 @@ _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = n
765
768
  if (delay < 0) {
766
769
  // Act as the interval was done
767
770
  // We don't need to resume it here because it will be resumed on line 160
768
- __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f");
771
+ __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pending, "f") : 0, "f");
769
772
  } else {
770
773
  // Act as the interval is pending
771
774
  if (__classPrivateFieldGet(this, _PQueue_timeoutId, "f") === undefined) {
@@ -785,7 +788,10 @@ _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = n
785
788
  clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f"));
786
789
  }
787
790
  __classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f");
788
- __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_emitEvents).call(this);
791
+ this.emit("empty");
792
+ if (__classPrivateFieldGet(this, _PQueue_pending, "f") === 0) {
793
+ this.emit("idle");
794
+ }
789
795
  return false;
790
796
  }
791
797
  if (!__classPrivateFieldGet(this, _PQueue_isPaused, "f")) {
@@ -814,16 +820,37 @@ _PQueue_carryoverConcurrencyCount = new WeakMap(), _PQueue_isIntervalIgnored = n
814
820
  }, __classPrivateFieldGet(this, _PQueue_interval, "f")), "f");
815
821
  __classPrivateFieldSet(this, _PQueue_intervalEnd, Date.now() + __classPrivateFieldGet(this, _PQueue_interval, "f"), "f");
816
822
  }, _PQueue_onInterval = function _PQueue_onInterval() {
817
- if (__classPrivateFieldGet(this, _PQueue_intervalCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_pendingCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_intervalId, "f")) {
823
+ if (__classPrivateFieldGet(this, _PQueue_intervalCount, "f") === 0 && __classPrivateFieldGet(this, _PQueue_pending, "f") === 0 && __classPrivateFieldGet(this, _PQueue_intervalId, "f")) {
818
824
  clearInterval(__classPrivateFieldGet(this, _PQueue_intervalId, "f"));
819
825
  __classPrivateFieldSet(this, _PQueue_intervalId, undefined, "f");
820
826
  }
821
- __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pendingCount, "f") : 0, "f");
827
+ __classPrivateFieldSet(this, _PQueue_intervalCount, __classPrivateFieldGet(this, _PQueue_carryoverConcurrencyCount, "f") ? __classPrivateFieldGet(this, _PQueue_pending, "f") : 0, "f");
822
828
  __classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_processQueue).call(this);
823
829
  }, _PQueue_processQueue = function _PQueue_processQueue() {
824
830
  // eslint-disable-next-line no-empty
825
831
  while(__classPrivateFieldGet(this, _PQueue_instances, "m", _PQueue_tryToStartAnother).call(this)){}
826
- }, _PQueue_onEvent = function() {
832
+ }, _PQueue_throwOnAbort = function() {
833
+ var __PQueue_throwOnAbort = _asyncToGenerator(function(signal) {
834
+ return __generator(this, function(_state) {
835
+ return [
836
+ 2,
837
+ new Promise(function(_resolve, reject) {
838
+ signal.addEventListener("abort", function() {
839
+ // TODO: Reject with signal.throwIfAborted() when targeting Node.js 18
840
+ // TODO: Use ABORT_ERR code when targeting Node.js 16 (https://nodejs.org/docs/latest-v16.x/api/errors.html#abort_err)
841
+ reject(new AbortError("The task was aborted."));
842
+ }, {
843
+ once: true
844
+ });
845
+ })
846
+ ];
847
+ });
848
+ });
849
+ function _PQueue_throwOnAbort(signal) {
850
+ return __PQueue_throwOnAbort.apply(this, arguments);
851
+ }
852
+ return _PQueue_throwOnAbort;
853
+ }(), _PQueue_onEvent = function() {
827
854
  var __PQueue_onEvent = _asyncToGenerator(function(event, filter) {
828
855
  var _this;
829
856
  return __generator(this, function(_state) {
package/dist/queue.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare type RunFunction = () => Promise<unknown>;
1
+ export type RunFunction = () => Promise<unknown>;
2
2
  export interface Queue<Element, Options> {
3
3
  size: number;
4
4
  filter: (options: Partial<Options>) => Element[];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@common.js/p-queue",
3
- "version": "7.3.0",
4
- "description": "Promise queue with concurrency control",
3
+ "version": "7.3.3",
4
+ "description": "p-queue package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
@@ -10,8 +10,8 @@
10
10
  "node": ">=12"
11
11
  },
12
12
  "scripts": {
13
- "build": "del dist && tsc",
14
- "test": "xo && npm run build && nyc ava",
13
+ "build": "del-cli dist && tsc",
14
+ "test": "xo && ava",
15
15
  "bench": "node --loader=ts-node/esm bench.ts"
16
16
  },
17
17
  "files": [
@@ -26,18 +26,16 @@
26
26
  "@sindresorhus/tsconfig": "^2.0.0",
27
27
  "@types/benchmark": "^2.1.1",
28
28
  "@types/node": "^17.0.13",
29
- "ava": "^4.0.1",
29
+ "ava": "^5.1.1",
30
30
  "benchmark": "^2.1.4",
31
- "codecov": "^3.8.3",
32
- "del-cli": "^4.0.1",
31
+ "del-cli": "^5.0.0",
33
32
  "delay": "^5.0.0",
34
33
  "in-range": "^3.0.0",
35
- "nyc": "^15.1.0",
36
34
  "p-defer": "^4.0.0",
37
35
  "random-int": "^3.0.0",
38
36
  "time-span": "^5.0.0",
39
- "ts-node": "^10.4.0",
40
- "typescript": "^4.5.5",
37
+ "ts-node": "^10.9.1",
38
+ "typescript": "^4.8.4",
41
39
  "xo": "^0.44.0"
42
40
  },
43
41
  "ava": {
@@ -58,11 +56,6 @@
58
56
  "@typescript-eslint/no-invalid-void-type": "off"
59
57
  }
60
58
  },
61
- "nyc": {
62
- "extension": [
63
- ".ts"
64
- ]
65
- },
66
59
  "homepage": "https://github.com/etienne-martin/common.js#readme",
67
60
  "main": "./dist/index.js"
68
61
  }
package/readme.md DELETED
@@ -1,518 +0,0 @@
1
- # p-queue
2
-
3
- > Promise queue with concurrency control
4
-
5
- Useful for rate-limiting async (or sync) operations. For example, when interacting with a REST API or when doing CPU/memory intensive tasks.
6
-
7
- For servers, you probably want a Redis-backed [job queue](https://github.com/sindresorhus/awesome-nodejs#job-queues) instead.
8
-
9
- Note that the project is feature complete. We are happy to review pull requests, but we don't plan any further development. We are also not answering email support questions.
10
-
11
- ## Install
12
-
13
- ```sh
14
- npm install p-queue
15
- ```
16
-
17
- **Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and no longer provides a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function. Please don't open issues for questions regarding CommonJS / ESM. You can also use [version 6](https://github.com/sindresorhus/p-queue/tree/v6.6.2) instead which is pretty stable. We will backport security fixes to v6 for the foreseeable future.
18
-
19
- ## Usage
20
-
21
- Here we run only one promise at the time. For example, set `concurrency` to 4 to run four promises at the same time.
22
-
23
- ```js
24
- import PQueue from 'p-queue';
25
- import got from 'got';
26
-
27
- const queue = new PQueue({concurrency: 1});
28
-
29
- (async () => {
30
- await queue.add(() => got('https://sindresorhus.com'));
31
- console.log('Done: sindresorhus.com');
32
- })();
33
-
34
- (async () => {
35
- await queue.add(() => got('https://avajs.dev'));
36
- console.log('Done: avajs.dev');
37
- })();
38
-
39
- (async () => {
40
- const task = await getUnicornTask();
41
- await queue.add(task);
42
- console.log('Done: Unicorn task');
43
- })();
44
- ```
45
-
46
- ## API
47
-
48
- ### PQueue(options?)
49
-
50
- Returns a new `queue` instance, which is an [`EventEmitter3`](https://github.com/primus/eventemitter3) subclass.
51
-
52
- #### options
53
-
54
- Type: `object`
55
-
56
- ##### concurrency
57
-
58
- Type: `number`\
59
- Default: `Infinity`\
60
- Minimum: `1`
61
-
62
- Concurrency limit.
63
-
64
- ##### timeout
65
-
66
- Type: `number`
67
-
68
- Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.
69
-
70
- ##### throwOnTimeout
71
-
72
- Type: `boolean`\
73
- Default: `false`
74
-
75
- Whether or not a timeout is considered an exception.
76
-
77
- ##### autoStart
78
-
79
- Type: `boolean`\
80
- Default: `true`
81
-
82
- Whether queue tasks within concurrency limit, are auto-executed as soon as they're added.
83
-
84
- ##### queueClass
85
-
86
- Type: `Function`
87
-
88
- Class with a `enqueue` and `dequeue` method, and a `size` getter. See the [Custom QueueClass](#custom-queueclass) section.
89
-
90
- ##### intervalCap
91
-
92
- Type: `number`\
93
- Default: `Infinity`\
94
- Minimum: `1`
95
-
96
- The max number of runs in the given interval of time.
97
-
98
- ##### interval
99
-
100
- Type: `number`\
101
- Default: `0`\
102
- Minimum: `0`
103
-
104
- The length of time in milliseconds before the interval count resets. Must be finite.
105
-
106
- ##### carryoverConcurrencyCount
107
-
108
- Type: `boolean`\
109
- Default: `false`
110
-
111
- If `true`, specifies that any [pending](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) Promises, should be carried over into the next interval and counted against the `intervalCap`. If `false`, any of those pending Promises will not count towards the next `intervalCap`.
112
-
113
- ### queue
114
-
115
- `PQueue` instance.
116
-
117
- #### .add(fn, options?)
118
-
119
- Adds a sync or async task to the queue. Always returns a promise.
120
-
121
- Note: If your items can potentially throw an exception, you must handle those errors from the returned Promise or they may be reported as an unhandled Promise rejection and potentially cause your process to exit immediately.
122
-
123
- ##### fn
124
-
125
- Type: `Function`
126
-
127
- Promise-returning/async function. When executed, it will receive `{signal}` as the first argument.
128
-
129
- #### options
130
-
131
- Type: `object`
132
-
133
- ##### priority
134
-
135
- Type: `number`\
136
- Default: `0`
137
-
138
- Priority of operation. Operations with greater priority will be scheduled first.
139
-
140
- ##### signal
141
-
142
- *Requires Node.js 16 or later.*
143
-
144
- [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an `AbortError`. If the operation is already running, the signal will need to be handled by the operation itself.
145
-
146
- ```js
147
- import PQueue, {AbortError} from 'p-queue';
148
- import got, {CancelError} from 'got';
149
-
150
- const queue = new PQueue();
151
-
152
- const controller = new AbortController();
153
-
154
- try {
155
- await queue.add(({signal}) => {
156
- const request = got('https://sindresorhus.com');
157
-
158
- signal.addEventListener('abort', () => {
159
- request.cancel();
160
- });
161
-
162
- try {
163
- return await request;
164
- } catch (error) {
165
- if (!(error instanceof CancelError)) {
166
- throw error;
167
- }
168
- }
169
- }, {signal: controller.signal});
170
- } catch (error) {
171
- if (!(error instanceof AbortError)) {
172
- throw error;
173
- }
174
- }
175
- ```
176
-
177
- #### .addAll(fns, options?)
178
-
179
- Same as `.add()`, but accepts an array of sync or async functions and returns a promise that resolves when all functions are resolved.
180
-
181
- #### .pause()
182
-
183
- Put queue execution on hold.
184
-
185
- #### .start()
186
-
187
- 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.)
188
-
189
- Returns `this` (the instance).
190
-
191
- #### .onEmpty()
192
-
193
- Returns a promise that settles when the queue becomes empty.
194
-
195
- Can be called multiple times. Useful if you for example add additional items at a later time.
196
-
197
- #### .onIdle()
198
-
199
- Returns a promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
200
-
201
- 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.
202
-
203
- #### .onSizeLessThan(limit)
204
-
205
- Returns a promise that settles when the queue size is less than the given limit: `queue.size < limit`.
206
-
207
- If you want to avoid having the queue grow beyond a certain size you can `await queue.onSizeLessThan()` before adding a new item.
208
-
209
- Note that this only limits the number of items waiting to start. There could still be up to `concurrency` jobs already running that this call does not include in its calculation.
210
-
211
- #### .clear()
212
-
213
- Clear the queue.
214
-
215
- #### .size
216
-
217
- Size of the queue, the number of queued items waiting to run.
218
-
219
- #### .sizeBy(options)
220
-
221
- Size of the queue, filtered by the given options.
222
-
223
- For example, this can be used to find the number of items remaining in the queue with a specific priority level.
224
-
225
- ```js
226
- import PQueue from 'p-queue';
227
-
228
- const queue = new PQueue();
229
-
230
- queue.add(async () => 'πŸ¦„', {priority: 1});
231
- queue.add(async () => 'πŸ¦„', {priority: 0});
232
- queue.add(async () => 'πŸ¦„', {priority: 1});
233
-
234
- console.log(queue.sizeBy({priority: 1}));
235
- //=> 2
236
-
237
- console.log(queue.sizeBy({priority: 0}));
238
- //=> 1
239
- ```
240
-
241
- #### .pending
242
-
243
- Number of running items (no longer in the queue).
244
-
245
- #### [.timeout](#timeout)
246
-
247
- #### [.concurrency](#concurrency)
248
-
249
- #### .isPaused
250
-
251
- Whether the queue is currently paused.
252
-
253
- ## Events
254
-
255
- #### active
256
-
257
- Emitted as each item is processed in the queue for the purpose of tracking progress.
258
-
259
- ```js
260
- import delay from 'delay';
261
- import PQueue from 'p-queue';
262
-
263
- const queue = new PQueue({concurrency: 2});
264
-
265
- let count = 0;
266
- queue.on('active', () => {
267
- console.log(`Working on item #${++count}. Size: ${queue.size} Pending: ${queue.pending}`);
268
- });
269
-
270
- queue.add(() => Promise.resolve());
271
- queue.add(() => delay(2000));
272
- queue.add(() => Promise.resolve());
273
- queue.add(() => Promise.resolve());
274
- queue.add(() => delay(500));
275
- ```
276
-
277
- #### completed
278
-
279
- Emitted when an item completes without error.
280
-
281
- ```js
282
- import delay from 'delay';
283
- import PQueue from 'p-queue';
284
-
285
- const queue = new PQueue({concurrency: 2});
286
-
287
- queue.on('completed', result => {
288
- console.log(result);
289
- });
290
-
291
- queue.add(() => Promise.resolve('hello, world!'));
292
- ```
293
-
294
- #### error
295
-
296
- Emitted if an item throws an error.
297
-
298
- ```js
299
- import delay from 'delay';
300
- import PQueue from 'p-queue';
301
-
302
- const queue = new PQueue({concurrency: 2});
303
-
304
- queue.on('error', error => {
305
- console.error(error);
306
- });
307
-
308
- queue.add(() => Promise.reject(new Error('error')));
309
- ```
310
-
311
- #### empty
312
-
313
- Emitted every time the queue becomes empty.
314
-
315
- Useful if you for example add additional items at a later time.
316
-
317
- #### idle
318
-
319
- Emitted every time the queue becomes empty and all promises have completed; `queue.size === 0 && queue.pending === 0`.
320
-
321
- The difference with `empty` is that `idle` guarantees that all work from the queue has finished. `empty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
322
-
323
- ```js
324
- import delay from 'delay';
325
- import PQueue from 'p-queue';
326
-
327
- const queue = new PQueue();
328
-
329
- queue.on('idle', () => {
330
- console.log(`Queue is idle. Size: ${queue.size} Pending: ${queue.pending}`);
331
- });
332
-
333
- const job1 = queue.add(() => delay(2000));
334
- const job2 = queue.add(() => delay(500));
335
-
336
- await job1;
337
- await job2;
338
- // => 'Queue is idle. Size: 0 Pending: 0'
339
-
340
- await queue.add(() => delay(600));
341
- // => 'Queue is idle. Size: 0 Pending: 0'
342
- ```
343
-
344
- The `idle` event is emitted every time the queue reaches an idle state. On the other hand, the promise the `onIdle()` function returns resolves once the queue becomes idle instead of every time the queue is idle.
345
-
346
- #### add
347
-
348
- Emitted every time the add method is called and the number of pending or queued tasks is increased.
349
-
350
- #### next
351
-
352
- Emitted every time a task is completed and the number of pending or queued tasks is decreased. This is emitted regardless of whether the task completed normally or with an error.
353
-
354
- ```js
355
- import delay from 'delay';
356
- import PQueue from 'p-queue';
357
-
358
- const queue = new PQueue();
359
-
360
- queue.on('add', () => {
361
- console.log(`Task is added. Size: ${queue.size} Pending: ${queue.pending}`);
362
- });
363
-
364
- queue.on('next', () => {
365
- console.log(`Task is completed. Size: ${queue.size} Pending: ${queue.pending}`);
366
- });
367
-
368
- const job1 = queue.add(() => delay(2000));
369
- const job2 = queue.add(() => delay(500));
370
-
371
- await job1;
372
- await job2;
373
- //=> 'Task is added. Size: 0 Pending: 1'
374
- //=> 'Task is added. Size: 0 Pending: 2'
375
-
376
- await queue.add(() => delay(600));
377
- //=> 'Task is completed. Size: 0 Pending: 1'
378
- //=> 'Task is completed. Size: 0 Pending: 0'
379
- ```
380
-
381
- ### AbortError
382
-
383
- The error thrown by `queue.add()` when a job is aborted before it is run. See [`signal`](#signal).
384
-
385
- ## Advanced example
386
-
387
- A more advanced example to help you understand the flow.
388
-
389
- ```js
390
- import delay from 'delay';
391
- import PQueue from 'p-queue';
392
-
393
- const queue = new PQueue({concurrency: 1});
394
-
395
- (async () => {
396
- await delay(200);
397
-
398
- console.log(`8. Pending promises: ${queue.pending}`);
399
- //=> '8. Pending promises: 0'
400
-
401
- (async () => {
402
- await queue.add(async () => 'πŸ™');
403
- console.log('11. Resolved')
404
- })();
405
-
406
- console.log('9. Added πŸ™');
407
-
408
- console.log(`10. Pending promises: ${queue.pending}`);
409
- //=> '10. Pending promises: 1'
410
-
411
- await queue.onIdle();
412
- console.log('12. All work is done');
413
- })();
414
-
415
- (async () => {
416
- await queue.add(async () => 'πŸ¦„');
417
- console.log('5. Resolved')
418
- })();
419
- console.log('1. Added πŸ¦„');
420
-
421
- (async () => {
422
- await queue.add(async () => '🐴');
423
- console.log('6. Resolved')
424
- })();
425
- console.log('2. Added 🐴');
426
-
427
- (async () => {
428
- await queue.onEmpty();
429
- console.log('7. Queue is empty');
430
- })();
431
-
432
- console.log(`3. Queue size: ${queue.size}`);
433
- //=> '3. Queue size: 1`
434
-
435
- console.log(`4. Pending promises: ${queue.pending}`);
436
- //=> '4. Pending promises: 1'
437
- ```
438
-
439
- ```
440
- $ node example.js
441
- 1. Added πŸ¦„
442
- 2. Added 🐴
443
- 3. Queue size: 1
444
- 4. Pending promises: 1
445
- 5. Resolved πŸ¦„
446
- 6. Resolved 🐴
447
- 7. Queue is empty
448
- 8. Pending promises: 0
449
- 9. Added πŸ™
450
- 10. Pending promises: 1
451
- 11. Resolved πŸ™
452
- 12. All work is done
453
- ```
454
-
455
- ## Custom QueueClass
456
-
457
- For implementing more complex scheduling policies, you can provide a QueueClass in the options:
458
-
459
- ```js
460
- import PQueue from 'p-queue';
461
-
462
- class QueueClass {
463
- constructor() {
464
- this._queue = [];
465
- }
466
-
467
- enqueue(run, options) {
468
- this._queue.push(run);
469
- }
470
-
471
- dequeue() {
472
- return this._queue.shift();
473
- }
474
-
475
- get size() {
476
- return this._queue.length;
477
- }
478
-
479
- filter(options) {
480
- return this._queue;
481
- }
482
- }
483
-
484
- const queue = new PQueue({queueClass: QueueClass});
485
- ```
486
-
487
- `p-queue` will call corresponding methods to put and get operations from this queue.
488
-
489
- ## FAQ
490
-
491
- #### How do the `concurrency` and `intervalCap` options affect each other?
492
-
493
- They are just different constraints. The `concurrency` option limits how many things run at the same time. The `intervalCap` option limits how many things run in total during the interval (over time).
494
-
495
- ## Maintainers
496
-
497
- - [Sindre Sorhus](https://github.com/sindresorhus)
498
- - [Richie Bendall](https://github.com/Richienb)
499
-
500
- ## Related
501
-
502
- - [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency
503
- - [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
504
- - [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
505
- - [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
506
- - [More…](https://github.com/sindresorhus/promise-fun)
507
-
508
- ---
509
-
510
- <div align="center">
511
- <b>
512
- <a href="https://tidelift.com/subscription/pkg/npm-p-queue?utm_source=npm-p-queue&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
513
- </b>
514
- <br>
515
- <sub>
516
- Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
517
- </sub>
518
- </div>