@common.js/p-queue 7.3.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 +5 -0
- package/dist/index.d.ts +87 -0
- package/dist/index.js +850 -0
- package/dist/lower-bound.d.ts +1 -0
- package/dist/lower-bound.js +27 -0
- package/dist/options.d.ts +102 -0
- package/dist/options.js +4 -0
- package/dist/priority-queue.d.ts +12 -0
- package/dist/priority-queue.js +123 -0
- package/dist/queue.d.ts +7 -0
- package/dist/queue.js +4 -0
- package/license +9 -0
- package/package.json +68 -0
- package/readme.md +518 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function lowerBound<T>(array: readonly T[], value: T, comparator: (a: T, b: T) => number): number;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
2
|
+
// Used to compute insertion index to keep queue sorted after insertion
|
|
3
|
+
"use strict";
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "default", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function() {
|
|
10
|
+
return lowerBound;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
function lowerBound(array, value, comparator) {
|
|
14
|
+
var first = 0;
|
|
15
|
+
var count = array.length;
|
|
16
|
+
while(count > 0){
|
|
17
|
+
var step = Math.trunc(count / 2);
|
|
18
|
+
var it = first + step;
|
|
19
|
+
if (comparator(array[it], value) <= 0) {
|
|
20
|
+
first = ++it;
|
|
21
|
+
count -= step + 1;
|
|
22
|
+
} else {
|
|
23
|
+
count = step;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return first;
|
|
27
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Queue, RunFunction } from './queue.js';
|
|
2
|
+
interface TimeoutOptions {
|
|
3
|
+
/**
|
|
4
|
+
Per-operation timeout in milliseconds. Operations fulfill once `timeout` elapses if they haven't already.
|
|
5
|
+
*/
|
|
6
|
+
timeout?: number;
|
|
7
|
+
/**
|
|
8
|
+
Whether or not a timeout is considered an exception.
|
|
9
|
+
|
|
10
|
+
@default false
|
|
11
|
+
*/
|
|
12
|
+
throwOnTimeout?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> extends TimeoutOptions {
|
|
15
|
+
/**
|
|
16
|
+
Concurrency limit.
|
|
17
|
+
|
|
18
|
+
Minimum: `1`.
|
|
19
|
+
|
|
20
|
+
@default Infinity
|
|
21
|
+
*/
|
|
22
|
+
readonly concurrency?: number;
|
|
23
|
+
/**
|
|
24
|
+
Whether queue tasks within concurrency limit, are auto-executed as soon as they're added.
|
|
25
|
+
|
|
26
|
+
@default true
|
|
27
|
+
*/
|
|
28
|
+
readonly autoStart?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
Class with a `enqueue` and `dequeue` method, and a `size` getter. See the [Custom QueueClass](https://github.com/sindresorhus/p-queue#custom-queueclass) section.
|
|
31
|
+
*/
|
|
32
|
+
readonly queueClass?: new () => QueueType;
|
|
33
|
+
/**
|
|
34
|
+
The max number of runs in the given interval of time.
|
|
35
|
+
|
|
36
|
+
Minimum: `1`.
|
|
37
|
+
|
|
38
|
+
@default Infinity
|
|
39
|
+
*/
|
|
40
|
+
readonly intervalCap?: number;
|
|
41
|
+
/**
|
|
42
|
+
The length of time in milliseconds before the interval count resets. Must be finite.
|
|
43
|
+
|
|
44
|
+
Minimum: `0`.
|
|
45
|
+
|
|
46
|
+
@default 0
|
|
47
|
+
*/
|
|
48
|
+
readonly interval?: number;
|
|
49
|
+
/**
|
|
50
|
+
Whether the task must finish in the given interval or will be carried over into the next interval count.
|
|
51
|
+
|
|
52
|
+
@default false
|
|
53
|
+
*/
|
|
54
|
+
readonly carryoverConcurrencyCount?: boolean;
|
|
55
|
+
}
|
|
56
|
+
export interface QueueAddOptions extends TaskOptions, TimeoutOptions {
|
|
57
|
+
/**
|
|
58
|
+
Priority of operation. Operations with greater priority will be scheduled first.
|
|
59
|
+
|
|
60
|
+
@default 0
|
|
61
|
+
*/
|
|
62
|
+
readonly priority?: number;
|
|
63
|
+
}
|
|
64
|
+
export interface TaskOptions {
|
|
65
|
+
/**
|
|
66
|
+
[`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.
|
|
67
|
+
|
|
68
|
+
@example
|
|
69
|
+
```
|
|
70
|
+
import PQueue, {AbortError} from 'p-queue';
|
|
71
|
+
import got, {CancelError} from 'got';
|
|
72
|
+
|
|
73
|
+
const queue = new PQueue();
|
|
74
|
+
|
|
75
|
+
const controller = new AbortController();
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
await queue.add(({signal}) => {
|
|
79
|
+
const request = got('https://sindresorhus.com');
|
|
80
|
+
|
|
81
|
+
signal.addEventListener('abort', () => {
|
|
82
|
+
request.cancel();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
return await request;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!(error instanceof CancelError)) {
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}, {signal: controller.signal});
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (!(error instanceof AbortError)) {
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
*/
|
|
100
|
+
readonly signal?: AbortSignal;
|
|
101
|
+
}
|
|
102
|
+
export {};
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Queue, RunFunction } from './queue.js';
|
|
2
|
+
import { QueueAddOptions } from './options.js';
|
|
3
|
+
export interface PriorityQueueOptions extends QueueAddOptions {
|
|
4
|
+
priority?: number;
|
|
5
|
+
}
|
|
6
|
+
export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOptions> {
|
|
7
|
+
#private;
|
|
8
|
+
enqueue(run: RunFunction, options?: Partial<PriorityQueueOptions>): void;
|
|
9
|
+
dequeue(): RunFunction | undefined;
|
|
10
|
+
filter(options: Readonly<Partial<PriorityQueueOptions>>): RunFunction[];
|
|
11
|
+
get size(): number;
|
|
12
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return PriorityQueue;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _lowerBoundJs = /*#__PURE__*/ _interopRequireDefault(require("./lower-bound.js"));
|
|
12
|
+
function _classCallCheck(instance, Constructor) {
|
|
13
|
+
if (!(instance instanceof Constructor)) {
|
|
14
|
+
throw new TypeError("Cannot call a class as a function");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function _defineProperties(target, props) {
|
|
18
|
+
for(var i = 0; i < props.length; i++){
|
|
19
|
+
var descriptor = props[i];
|
|
20
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
21
|
+
descriptor.configurable = true;
|
|
22
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
23
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
27
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
28
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
29
|
+
return Constructor;
|
|
30
|
+
}
|
|
31
|
+
function _defineProperty(obj, key, value) {
|
|
32
|
+
if (key in obj) {
|
|
33
|
+
Object.defineProperty(obj, key, {
|
|
34
|
+
value: value,
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
obj[key] = value;
|
|
41
|
+
}
|
|
42
|
+
return obj;
|
|
43
|
+
}
|
|
44
|
+
function _interopRequireDefault(obj) {
|
|
45
|
+
return obj && obj.__esModule ? obj : {
|
|
46
|
+
default: obj
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function _objectSpread(target) {
|
|
50
|
+
for(var i = 1; i < arguments.length; i++){
|
|
51
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
52
|
+
var ownKeys = Object.keys(source);
|
|
53
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
54
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
55
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
ownKeys.forEach(function(key) {
|
|
59
|
+
_defineProperty(target, key, source[key]);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return target;
|
|
63
|
+
}
|
|
64
|
+
var __classPrivateFieldGet = (void 0) && (void 0).__classPrivateFieldGet || function(receiver, state, kind, f) {
|
|
65
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
66
|
+
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");
|
|
67
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
68
|
+
};
|
|
69
|
+
var _PriorityQueue_queue;
|
|
70
|
+
var PriorityQueue = /*#__PURE__*/ function() {
|
|
71
|
+
"use strict";
|
|
72
|
+
function PriorityQueue() {
|
|
73
|
+
_classCallCheck(this, PriorityQueue);
|
|
74
|
+
_PriorityQueue_queue.set(this, []);
|
|
75
|
+
}
|
|
76
|
+
_createClass(PriorityQueue, [
|
|
77
|
+
{
|
|
78
|
+
key: "enqueue",
|
|
79
|
+
value: function enqueue(run, options) {
|
|
80
|
+
options = _objectSpread({
|
|
81
|
+
priority: 0
|
|
82
|
+
}, options);
|
|
83
|
+
var element = {
|
|
84
|
+
priority: options.priority,
|
|
85
|
+
run: run
|
|
86
|
+
};
|
|
87
|
+
if (this.size && __classPrivateFieldGet(this, _PriorityQueue_queue, "f")[this.size - 1].priority >= options.priority) {
|
|
88
|
+
__classPrivateFieldGet(this, _PriorityQueue_queue, "f").push(element);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
var index = (0, _lowerBoundJs.default)(__classPrivateFieldGet(this, _PriorityQueue_queue, "f"), element, function(a, b) {
|
|
92
|
+
return b.priority - a.priority;
|
|
93
|
+
});
|
|
94
|
+
__classPrivateFieldGet(this, _PriorityQueue_queue, "f").splice(index, 0, element);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
key: "dequeue",
|
|
99
|
+
value: function dequeue() {
|
|
100
|
+
var item = __classPrivateFieldGet(this, _PriorityQueue_queue, "f").shift();
|
|
101
|
+
return item === null || item === void 0 ? void 0 : item.run;
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
key: "filter",
|
|
106
|
+
value: function filter(options) {
|
|
107
|
+
return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").filter(function(element) {
|
|
108
|
+
return element.priority === options.priority;
|
|
109
|
+
}).map(function(element) {
|
|
110
|
+
return element.run;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
key: "size",
|
|
116
|
+
get: function get() {
|
|
117
|
+
return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").length;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
]);
|
|
121
|
+
return PriorityQueue;
|
|
122
|
+
}();
|
|
123
|
+
_PriorityQueue_queue = new WeakMap();
|
package/dist/queue.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare type RunFunction = () => Promise<unknown>;
|
|
2
|
+
export interface Queue<Element, Options> {
|
|
3
|
+
size: number;
|
|
4
|
+
filter: (options: Partial<Options>) => Element[];
|
|
5
|
+
dequeue: () => Element | undefined;
|
|
6
|
+
enqueue: (run: Element, options?: Partial<Options>) => void;
|
|
7
|
+
}
|
package/dist/queue.js
ADDED
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/p-queue",
|
|
3
|
+
"version": "7.3.0",
|
|
4
|
+
"description": "Promise queue with concurrency control",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "etienne-martin/common.js",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=12"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "del dist && tsc",
|
|
14
|
+
"test": "xo && npm run build && nyc ava",
|
|
15
|
+
"bench": "node --loader=ts-node/esm bench.ts"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"eventemitter3": "^4.0.7",
|
|
23
|
+
"@common.js/p-timeout": "^5.0.2"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@sindresorhus/tsconfig": "^2.0.0",
|
|
27
|
+
"@types/benchmark": "^2.1.1",
|
|
28
|
+
"@types/node": "^17.0.13",
|
|
29
|
+
"ava": "^4.0.1",
|
|
30
|
+
"benchmark": "^2.1.4",
|
|
31
|
+
"codecov": "^3.8.3",
|
|
32
|
+
"del-cli": "^4.0.1",
|
|
33
|
+
"delay": "^5.0.0",
|
|
34
|
+
"in-range": "^3.0.0",
|
|
35
|
+
"nyc": "^15.1.0",
|
|
36
|
+
"p-defer": "^4.0.0",
|
|
37
|
+
"random-int": "^3.0.0",
|
|
38
|
+
"time-span": "^5.0.0",
|
|
39
|
+
"ts-node": "^10.4.0",
|
|
40
|
+
"typescript": "^4.5.5",
|
|
41
|
+
"xo": "^0.44.0"
|
|
42
|
+
},
|
|
43
|
+
"ava": {
|
|
44
|
+
"files": [
|
|
45
|
+
"test/**"
|
|
46
|
+
],
|
|
47
|
+
"extensions": {
|
|
48
|
+
"ts": "module"
|
|
49
|
+
},
|
|
50
|
+
"nodeArguments": [
|
|
51
|
+
"--loader=ts-node/esm"
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
"xo": {
|
|
55
|
+
"rules": {
|
|
56
|
+
"@typescript-eslint/member-ordering": "off",
|
|
57
|
+
"@typescript-eslint/no-floating-promises": "off",
|
|
58
|
+
"@typescript-eslint/no-invalid-void-type": "off"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"nyc": {
|
|
62
|
+
"extension": [
|
|
63
|
+
".ts"
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
67
|
+
"main": "./dist/index.js"
|
|
68
|
+
}
|