@common.js/p-limit 5.0.0 → 6.1.0

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-limit](https://www.npmjs.com/package/p-limit) package exported as CommonJS modules.
4
4
 
5
- Exported from [p-limit@5.0.0](https://www.npmjs.com/package/p-limit/v/5.0.0) using https://github.com/etienne-martin/common.js.
5
+ Exported from [p-limit@6.1.0](https://www.npmjs.com/package/p-limit/v/6.1.0) using https://github.com/etienne-martin/common.js.
package/index.d.ts CHANGED
@@ -9,6 +9,11 @@ export type LimitFunction = {
9
9
  */
10
10
  readonly pendingCount: number;
11
11
 
12
+ /**
13
+ Get or set the concurrency limit.
14
+ */
15
+ concurrency: number;
16
+
12
17
  /**
13
18
  Discard pending promises that are waiting to run.
14
19
 
@@ -24,7 +29,7 @@ export type LimitFunction = {
24
29
  @returns The promise returned by calling `fn(...arguments)`.
25
30
  */
26
31
  <Arguments extends unknown[], ReturnType>(
27
- fn: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
32
+ function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
28
33
  ...arguments_: Arguments
29
34
  ): Promise<ReturnType>;
30
35
  };
package/index.js CHANGED
@@ -9,7 +9,6 @@ Object.defineProperty(exports, "default", {
9
9
  }
10
10
  });
11
11
  var _yoctoQueue = /*#__PURE__*/ _interopRequireDefault(require("yocto-queue"));
12
- var _asyncHooks = require("#async_hooks");
13
12
  function _arrayLikeToArray(arr, len) {
14
13
  if (len == null || len > arr.length) len = arr.length;
15
14
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
@@ -165,24 +164,26 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
165
164
  }
166
165
  };
167
166
  function pLimit(concurrency) {
168
- if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
169
- throw new TypeError("Expected `concurrency` to be a number from 1 and up");
170
- }
167
+ validateConcurrency(concurrency);
171
168
  var queue = new _yoctoQueue.default();
172
169
  var activeCount = 0;
173
- var next = function() {
174
- activeCount--;
175
- if (queue.size > 0) {
170
+ var resumeNext = function() {
171
+ if (activeCount < concurrency && queue.size > 0) {
176
172
  queue.dequeue()();
173
+ // Since `pendingCount` has been decreased by one, increase `activeCount` by one.
174
+ activeCount++;
177
175
  }
178
176
  };
177
+ var next = function() {
178
+ activeCount--;
179
+ resumeNext();
180
+ };
179
181
  var run = function() {
180
182
  var _ref = _asyncToGenerator(function(function_, resolve, arguments_) {
181
183
  var result, e;
182
184
  return __generator(this, function(_state) {
183
185
  switch(_state.label){
184
186
  case 0:
185
- activeCount++;
186
187
  result = _asyncToGenerator(function() {
187
188
  return __generator(this, function(_state) {
188
189
  return [
@@ -229,14 +230,18 @@ function pLimit(concurrency) {
229
230
  };
230
231
  }();
231
232
  var enqueue = function(function_, resolve, arguments_) {
232
- queue.enqueue(_asyncHooks.AsyncResource.bind(run.bind(undefined, function_, resolve, arguments_)));
233
+ // Queue `internalResolve` instead of the `run` function
234
+ // to preserve asynchronous context.
235
+ new Promise(function(internalResolve) {
236
+ queue.enqueue(internalResolve);
237
+ }).then(run.bind(undefined, function_, resolve, arguments_));
233
238
  _asyncToGenerator(function() {
234
239
  return __generator(this, function(_state) {
235
240
  switch(_state.label){
236
241
  case 0:
237
242
  // This function needs to wait until the next microtask before comparing
238
243
  // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
239
- // when the run function is dequeued and called. The comparison in the if-statement
244
+ // after the `internalResolve` function is dequeued and called. The comparison in the if-statement
240
245
  // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
241
246
  return [
242
247
  4,
@@ -244,8 +249,8 @@ function pLimit(concurrency) {
244
249
  ];
245
250
  case 1:
246
251
  _state.sent();
247
- if (activeCount < concurrency && queue.size > 0) {
248
- queue.dequeue()();
252
+ if (activeCount < concurrency) {
253
+ resumeNext();
249
254
  }
250
255
  return [
251
256
  2
@@ -277,7 +282,27 @@ function pLimit(concurrency) {
277
282
  value: function value() {
278
283
  queue.clear();
279
284
  }
285
+ },
286
+ concurrency: {
287
+ get: function() {
288
+ return concurrency;
289
+ },
290
+ set: function set(newConcurrency) {
291
+ validateConcurrency(newConcurrency);
292
+ concurrency = newConcurrency;
293
+ queueMicrotask(function() {
294
+ // eslint-disable-next-line no-unmodified-loop-condition
295
+ while(activeCount < concurrency && queue.size > 0){
296
+ resumeNext();
297
+ }
298
+ });
299
+ }
280
300
  }
281
301
  });
282
302
  return generator;
283
303
  }
304
+ function validateConcurrency(concurrency) {
305
+ if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
306
+ throw new TypeError("Expected `concurrency` to be a number from 1 and up");
307
+ }
308
+ }
package/package.json CHANGED
@@ -1,17 +1,12 @@
1
1
  {
2
2
  "name": "@common.js/p-limit",
3
- "version": "5.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "p-limit package exported as CommonJS modules",
5
5
  "license": "MIT",
6
6
  "repository": "etienne-martin/common.js",
7
7
  "funding": "https://github.com/sponsors/sindresorhus",
8
8
  "type": "commonjs",
9
- "imports": {
10
- "#async_hooks": {
11
- "node": "async_hooks",
12
- "default": "./async-hooks-stub.js"
13
- }
14
- },
9
+ "sideEffects": false,
15
10
  "engines": {
16
11
  "node": ">=18"
17
12
  },
@@ -20,20 +15,19 @@
20
15
  },
21
16
  "files": [
22
17
  "index.js",
23
- "index.d.ts",
24
- "async-hooks-stub.js"
18
+ "index.d.ts"
25
19
  ],
26
20
  "dependencies": {
27
- "@common.js/yocto-queue": "^1.0.0"
21
+ "@common.js/yocto-queue": "^1.1.1"
28
22
  },
29
23
  "devDependencies": {
30
- "ava": "^5.3.1",
24
+ "ava": "^6.1.3",
31
25
  "delay": "^6.0.0",
32
26
  "in-range": "^3.0.0",
33
27
  "random-int": "^3.0.0",
34
28
  "time-span": "^5.1.0",
35
- "tsd": "^0.29.0",
36
- "xo": "^0.56.0"
29
+ "tsd": "^0.31.1",
30
+ "xo": "^0.58.0"
37
31
  },
38
32
  "homepage": "https://github.com/etienne-martin/common.js#readme",
39
33
  "types": "./index.d.ts",
@@ -1,63 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- AsyncResource: function() {
13
- return AsyncResource;
14
- },
15
- AsyncLocalStorage: function() {
16
- return AsyncLocalStorage;
17
- }
18
- });
19
- function _classCallCheck(instance, Constructor) {
20
- if (!(instance instanceof Constructor)) {
21
- throw new TypeError("Cannot call a class as a function");
22
- }
23
- }
24
- function _defineProperties(target, props) {
25
- for(var i = 0; i < props.length; i++){
26
- var descriptor = props[i];
27
- descriptor.enumerable = descriptor.enumerable || false;
28
- descriptor.configurable = true;
29
- if ("value" in descriptor) descriptor.writable = true;
30
- Object.defineProperty(target, descriptor.key, descriptor);
31
- }
32
- }
33
- function _createClass(Constructor, protoProps, staticProps) {
34
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
35
- if (staticProps) _defineProperties(Constructor, staticProps);
36
- return Constructor;
37
- }
38
- var AsyncResource = {
39
- bind: function bind(fn, _type, thisArg) {
40
- return fn.bind(thisArg);
41
- }
42
- };
43
- var AsyncLocalStorage = /*#__PURE__*/ function() {
44
- "use strict";
45
- function AsyncLocalStorage() {
46
- _classCallCheck(this, AsyncLocalStorage);
47
- }
48
- _createClass(AsyncLocalStorage, [
49
- {
50
- key: "getStore",
51
- value: function getStore() {
52
- return undefined;
53
- }
54
- },
55
- {
56
- key: "run",
57
- value: function run(_store, callback) {
58
- return callback();
59
- }
60
- }
61
- ]);
62
- return AsyncLocalStorage;
63
- }();