@common.js/p-limit 4.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 +1 -1
- package/index.d.ts +9 -6
- package/index.js +46 -20
- package/package.json +11 -9
- package/readme.md +0 -99
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
|
+
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
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export interface LimitFunction {
|
|
1
|
+
export type LimitFunction = {
|
|
4
2
|
/**
|
|
5
3
|
The number of promises that are currently running.
|
|
6
4
|
*/
|
|
@@ -11,6 +9,11 @@ export interface LimitFunction {
|
|
|
11
9
|
*/
|
|
12
10
|
readonly pendingCount: number;
|
|
13
11
|
|
|
12
|
+
/**
|
|
13
|
+
Get or set the concurrency limit.
|
|
14
|
+
*/
|
|
15
|
+
concurrency: number;
|
|
16
|
+
|
|
14
17
|
/**
|
|
15
18
|
Discard pending promises that are waiting to run.
|
|
16
19
|
|
|
@@ -26,10 +29,10 @@ export interface LimitFunction {
|
|
|
26
29
|
@returns The promise returned by calling `fn(...arguments)`.
|
|
27
30
|
*/
|
|
28
31
|
<Arguments extends unknown[], ReturnType>(
|
|
29
|
-
|
|
30
|
-
...
|
|
32
|
+
function_: (...arguments_: Arguments) => PromiseLike<ReturnType> | ReturnType,
|
|
33
|
+
...arguments_: Arguments
|
|
31
34
|
): Promise<ReturnType>;
|
|
32
|
-
}
|
|
35
|
+
};
|
|
33
36
|
|
|
34
37
|
/**
|
|
35
38
|
Run multiple promise-returning & async functions with limited concurrency.
|
package/index.js
CHANGED
|
@@ -164,29 +164,31 @@ var __generator = (void 0) && (void 0).__generator || function(thisArg, body) {
|
|
|
164
164
|
}
|
|
165
165
|
};
|
|
166
166
|
function pLimit(concurrency) {
|
|
167
|
-
|
|
168
|
-
throw new TypeError("Expected `concurrency` to be a number from 1 and up");
|
|
169
|
-
}
|
|
167
|
+
validateConcurrency(concurrency);
|
|
170
168
|
var queue = new _yoctoQueue.default();
|
|
171
169
|
var activeCount = 0;
|
|
172
|
-
var
|
|
173
|
-
activeCount
|
|
174
|
-
if (queue.size > 0) {
|
|
170
|
+
var resumeNext = function() {
|
|
171
|
+
if (activeCount < concurrency && queue.size > 0) {
|
|
175
172
|
queue.dequeue()();
|
|
173
|
+
// Since `pendingCount` has been decreased by one, increase `activeCount` by one.
|
|
174
|
+
activeCount++;
|
|
176
175
|
}
|
|
177
176
|
};
|
|
177
|
+
var next = function() {
|
|
178
|
+
activeCount--;
|
|
179
|
+
resumeNext();
|
|
180
|
+
};
|
|
178
181
|
var run = function() {
|
|
179
|
-
var _ref = _asyncToGenerator(function(
|
|
182
|
+
var _ref = _asyncToGenerator(function(function_, resolve, arguments_) {
|
|
180
183
|
var result, e;
|
|
181
184
|
return __generator(this, function(_state) {
|
|
182
185
|
switch(_state.label){
|
|
183
186
|
case 0:
|
|
184
|
-
activeCount++;
|
|
185
187
|
result = _asyncToGenerator(function() {
|
|
186
188
|
return __generator(this, function(_state) {
|
|
187
189
|
return [
|
|
188
190
|
2,
|
|
189
|
-
|
|
191
|
+
function_.apply(void 0, _toConsumableArray(arguments_))
|
|
190
192
|
];
|
|
191
193
|
});
|
|
192
194
|
})();
|
|
@@ -223,19 +225,23 @@ function pLimit(concurrency) {
|
|
|
223
225
|
}
|
|
224
226
|
});
|
|
225
227
|
});
|
|
226
|
-
return function run(
|
|
228
|
+
return function run(function_, resolve, arguments_) {
|
|
227
229
|
return _ref.apply(this, arguments);
|
|
228
230
|
};
|
|
229
231
|
}();
|
|
230
|
-
var enqueue = function(
|
|
231
|
-
|
|
232
|
+
var enqueue = function(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_));
|
|
232
238
|
_asyncToGenerator(function() {
|
|
233
239
|
return __generator(this, function(_state) {
|
|
234
240
|
switch(_state.label){
|
|
235
241
|
case 0:
|
|
236
242
|
// This function needs to wait until the next microtask before comparing
|
|
237
243
|
// `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
|
|
238
|
-
//
|
|
244
|
+
// after the `internalResolve` function is dequeued and called. The comparison in the if-statement
|
|
239
245
|
// needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
|
|
240
246
|
return [
|
|
241
247
|
4,
|
|
@@ -243,8 +249,8 @@ function pLimit(concurrency) {
|
|
|
243
249
|
];
|
|
244
250
|
case 1:
|
|
245
251
|
_state.sent();
|
|
246
|
-
if (activeCount < concurrency
|
|
247
|
-
|
|
252
|
+
if (activeCount < concurrency) {
|
|
253
|
+
resumeNext();
|
|
248
254
|
}
|
|
249
255
|
return [
|
|
250
256
|
2
|
|
@@ -253,12 +259,12 @@ function pLimit(concurrency) {
|
|
|
253
259
|
});
|
|
254
260
|
})();
|
|
255
261
|
};
|
|
256
|
-
var generator = function(
|
|
257
|
-
for(var _len = arguments.length,
|
|
258
|
-
|
|
262
|
+
var generator = function(function_) {
|
|
263
|
+
for(var _len = arguments.length, arguments_ = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
|
|
264
|
+
arguments_[_key - 1] = arguments[_key];
|
|
259
265
|
}
|
|
260
266
|
return new Promise(function(resolve) {
|
|
261
|
-
enqueue(
|
|
267
|
+
enqueue(function_, resolve, arguments_);
|
|
262
268
|
});
|
|
263
269
|
};
|
|
264
270
|
Object.defineProperties(generator, {
|
|
@@ -273,10 +279,30 @@ function pLimit(concurrency) {
|
|
|
273
279
|
}
|
|
274
280
|
},
|
|
275
281
|
clearQueue: {
|
|
276
|
-
value: function() {
|
|
282
|
+
value: function value() {
|
|
277
283
|
queue.clear();
|
|
278
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
|
+
}
|
|
279
300
|
}
|
|
280
301
|
});
|
|
281
302
|
return generator;
|
|
282
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,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/p-limit",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "6.1.0",
|
|
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
|
+
"sideEffects": false,
|
|
9
10
|
"engines": {
|
|
10
|
-
"node": "
|
|
11
|
+
"node": ">=18"
|
|
11
12
|
},
|
|
12
13
|
"scripts": {
|
|
13
14
|
"test": "xo && ava && tsd"
|
|
@@ -17,17 +18,18 @@
|
|
|
17
18
|
"index.d.ts"
|
|
18
19
|
],
|
|
19
20
|
"dependencies": {
|
|
20
|
-
"@common.js/yocto-queue": "^1.
|
|
21
|
+
"@common.js/yocto-queue": "^1.1.1"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"ava": "^
|
|
24
|
-
"delay": "^
|
|
24
|
+
"ava": "^6.1.3",
|
|
25
|
+
"delay": "^6.0.0",
|
|
25
26
|
"in-range": "^3.0.0",
|
|
26
27
|
"random-int": "^3.0.0",
|
|
27
|
-
"time-span": "^5.
|
|
28
|
-
"tsd": "^0.
|
|
29
|
-
"xo": "^0.
|
|
28
|
+
"time-span": "^5.1.0",
|
|
29
|
+
"tsd": "^0.31.1",
|
|
30
|
+
"xo": "^0.58.0"
|
|
30
31
|
},
|
|
31
32
|
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
33
|
+
"types": "./index.d.ts",
|
|
32
34
|
"main": "./index.js"
|
|
33
35
|
}
|
package/readme.md
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
# p-limit
|
|
2
|
-
|
|
3
|
-
> Run multiple promise-returning & async functions with limited concurrency
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
$ npm install p-limit
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import pLimit from 'p-limit';
|
|
15
|
-
|
|
16
|
-
const limit = pLimit(1);
|
|
17
|
-
|
|
18
|
-
const input = [
|
|
19
|
-
limit(() => fetchSomething('foo')),
|
|
20
|
-
limit(() => fetchSomething('bar')),
|
|
21
|
-
limit(() => doSomething())
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
// Only one promise is run at once
|
|
25
|
-
const result = await Promise.all(input);
|
|
26
|
-
console.log(result);
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## API
|
|
30
|
-
|
|
31
|
-
### pLimit(concurrency)
|
|
32
|
-
|
|
33
|
-
Returns a `limit` function.
|
|
34
|
-
|
|
35
|
-
#### concurrency
|
|
36
|
-
|
|
37
|
-
Type: `number`\
|
|
38
|
-
Minimum: `1`\
|
|
39
|
-
Default: `Infinity`
|
|
40
|
-
|
|
41
|
-
Concurrency limit.
|
|
42
|
-
|
|
43
|
-
### limit(fn, ...args)
|
|
44
|
-
|
|
45
|
-
Returns the promise returned by calling `fn(...args)`.
|
|
46
|
-
|
|
47
|
-
#### fn
|
|
48
|
-
|
|
49
|
-
Type: `Function`
|
|
50
|
-
|
|
51
|
-
Promise-returning/async function.
|
|
52
|
-
|
|
53
|
-
#### args
|
|
54
|
-
|
|
55
|
-
Any arguments to pass through to `fn`.
|
|
56
|
-
|
|
57
|
-
Support for passing arguments on to the `fn` is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a *lot* of functions.
|
|
58
|
-
|
|
59
|
-
### limit.activeCount
|
|
60
|
-
|
|
61
|
-
The number of promises that are currently running.
|
|
62
|
-
|
|
63
|
-
### limit.pendingCount
|
|
64
|
-
|
|
65
|
-
The number of promises that are waiting to run (i.e. their internal `fn` was not called yet).
|
|
66
|
-
|
|
67
|
-
### limit.clearQueue()
|
|
68
|
-
|
|
69
|
-
Discard pending promises that are waiting to run.
|
|
70
|
-
|
|
71
|
-
This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.
|
|
72
|
-
|
|
73
|
-
Note: This does not cancel promises that are already running.
|
|
74
|
-
|
|
75
|
-
## FAQ
|
|
76
|
-
|
|
77
|
-
### How is this different from the [`p-queue`](https://github.com/sindresorhus/p-queue) package?
|
|
78
|
-
|
|
79
|
-
This package is only about limiting the number of concurrent executions, while `p-queue` is a fully featured queue implementation with lots of different options, introspection, and ability to pause the queue.
|
|
80
|
-
|
|
81
|
-
## Related
|
|
82
|
-
|
|
83
|
-
- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
|
|
84
|
-
- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
|
|
85
|
-
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
|
|
86
|
-
- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
|
|
87
|
-
- [More…](https://github.com/sindresorhus/promise-fun)
|
|
88
|
-
|
|
89
|
-
---
|
|
90
|
-
|
|
91
|
-
<div align="center">
|
|
92
|
-
<b>
|
|
93
|
-
<a href="https://tidelift.com/subscription/pkg/npm-p-limit?utm_source=npm-p-limit&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
|
94
|
-
</b>
|
|
95
|
-
<br>
|
|
96
|
-
<sub>
|
|
97
|
-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
98
|
-
</sub>
|
|
99
|
-
</div>
|