@ls-stack/utils 3.6.0 → 3.8.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/lib/arrayUtils.cjs +28 -0
- package/lib/arrayUtils.d.cts +3 -1
- package/lib/arrayUtils.d.ts +3 -1
- package/lib/arrayUtils.js +5 -1
- package/lib/asyncQueue.cjs +239 -0
- package/lib/asyncQueue.d.cts +56 -0
- package/lib/asyncQueue.d.ts +56 -0
- package/lib/asyncQueue.js +201 -0
- package/lib/cache.js +1 -1
- package/lib/chunk-DFXNVEH6.js +14 -0
- package/lib/{chunk-5JQGMNNY.js → chunk-XMUFRC2U.js} +27 -1
- package/lib/concurrentCalls.cjs +288 -0
- package/lib/concurrentCalls.d.cts +75 -0
- package/lib/concurrentCalls.d.ts +75 -0
- package/lib/concurrentCalls.js +247 -0
- package/lib/createThrottleController.js +1 -1
- package/lib/parallelAsyncCalls.d.cts +2 -0
- package/lib/parallelAsyncCalls.d.ts +2 -0
- package/lib/promiseUtils.js +3 -10
- package/lib/testUtils.js +1 -1
- package/lib/yamlStringify.js +3 -3
- package/package.json +11 -2
package/lib/arrayUtils.cjs
CHANGED
|
@@ -25,8 +25,10 @@ __export(arrayUtils_exports, {
|
|
|
25
25
|
filterAndMap: () => filterAndMap,
|
|
26
26
|
findAfterIndex: () => findAfterIndex,
|
|
27
27
|
findBeforeIndex: () => findBeforeIndex,
|
|
28
|
+
hasDuplicates: () => hasDuplicates,
|
|
28
29
|
isInArray: () => isInArray,
|
|
29
30
|
rejectArrayUndefinedValues: () => rejectArrayUndefinedValues,
|
|
31
|
+
rejectDuplicates: () => rejectDuplicates,
|
|
30
32
|
sortBy: () => sortBy
|
|
31
33
|
});
|
|
32
34
|
module.exports = __toCommonJS(arrayUtils_exports);
|
|
@@ -107,6 +109,30 @@ function findBeforeIndex(array, index, predicate) {
|
|
|
107
109
|
function rejectArrayUndefinedValues(array) {
|
|
108
110
|
return array.filter((item) => item !== void 0);
|
|
109
111
|
}
|
|
112
|
+
function hasDuplicates(array, getKey = (item) => item) {
|
|
113
|
+
const seen = /* @__PURE__ */ new Set();
|
|
114
|
+
for (const item of array) {
|
|
115
|
+
const key = getKey(item);
|
|
116
|
+
if (seen.has(key)) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
seen.add(key);
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
function rejectDuplicates(array, getKey = (item) => item) {
|
|
124
|
+
const seen = /* @__PURE__ */ new Set();
|
|
125
|
+
const result = [];
|
|
126
|
+
for (const item of array) {
|
|
127
|
+
const key = getKey(item);
|
|
128
|
+
if (seen.has(key)) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
seen.add(key);
|
|
132
|
+
result.push(item);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
110
136
|
// Annotate the CommonJS export names for ESM import in node:
|
|
111
137
|
0 && (module.exports = {
|
|
112
138
|
arrayWithPrev,
|
|
@@ -114,7 +140,9 @@ function rejectArrayUndefinedValues(array) {
|
|
|
114
140
|
filterAndMap,
|
|
115
141
|
findAfterIndex,
|
|
116
142
|
findBeforeIndex,
|
|
143
|
+
hasDuplicates,
|
|
117
144
|
isInArray,
|
|
118
145
|
rejectArrayUndefinedValues,
|
|
146
|
+
rejectDuplicates,
|
|
119
147
|
sortBy
|
|
120
148
|
});
|
package/lib/arrayUtils.d.cts
CHANGED
|
@@ -50,5 +50,7 @@ declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]):
|
|
|
50
50
|
declare function findAfterIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
|
|
51
51
|
declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
|
|
52
52
|
declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
|
|
53
|
+
declare function hasDuplicates<T>(array: T[], getKey?: (item: T) => unknown): boolean;
|
|
54
|
+
declare function rejectDuplicates<T>(array: T[], getKey?: (item: T) => unknown): T[];
|
|
53
55
|
|
|
54
|
-
export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, rejectArrayUndefinedValues, sortBy };
|
|
56
|
+
export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy };
|
package/lib/arrayUtils.d.ts
CHANGED
|
@@ -50,5 +50,7 @@ declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]):
|
|
|
50
50
|
declare function findAfterIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
|
|
51
51
|
declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item: T) => boolean): T | undefined;
|
|
52
52
|
declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
|
|
53
|
+
declare function hasDuplicates<T>(array: T[], getKey?: (item: T) => unknown): boolean;
|
|
54
|
+
declare function rejectDuplicates<T>(array: T[], getKey?: (item: T) => unknown): T[];
|
|
53
55
|
|
|
54
|
-
export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, isInArray, rejectArrayUndefinedValues, sortBy };
|
|
56
|
+
export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy };
|
package/lib/arrayUtils.js
CHANGED
|
@@ -4,17 +4,21 @@ import {
|
|
|
4
4
|
filterAndMap,
|
|
5
5
|
findAfterIndex,
|
|
6
6
|
findBeforeIndex,
|
|
7
|
+
hasDuplicates,
|
|
7
8
|
isInArray,
|
|
8
9
|
rejectArrayUndefinedValues,
|
|
10
|
+
rejectDuplicates,
|
|
9
11
|
sortBy
|
|
10
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-XMUFRC2U.js";
|
|
11
13
|
export {
|
|
12
14
|
arrayWithPrev,
|
|
13
15
|
arrayWithPrevAndIndex,
|
|
14
16
|
filterAndMap,
|
|
15
17
|
findAfterIndex,
|
|
16
18
|
findBeforeIndex,
|
|
19
|
+
hasDuplicates,
|
|
17
20
|
isInArray,
|
|
18
21
|
rejectArrayUndefinedValues,
|
|
22
|
+
rejectDuplicates,
|
|
19
23
|
sortBy
|
|
20
24
|
};
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/asyncQueue.ts
|
|
21
|
+
var asyncQueue_exports = {};
|
|
22
|
+
__export(asyncQueue_exports, {
|
|
23
|
+
createAsyncQueue: () => createAsyncQueue,
|
|
24
|
+
createAsyncQueueWithId: () => createAsyncQueueWithId
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(asyncQueue_exports);
|
|
27
|
+
var import_evtmitter = require("evtmitter");
|
|
28
|
+
var import_t_result = require("t-result");
|
|
29
|
+
|
|
30
|
+
// src/assertions.ts
|
|
31
|
+
function isObject(value) {
|
|
32
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
function isFunction(value) {
|
|
35
|
+
return typeof value === "function";
|
|
36
|
+
}
|
|
37
|
+
function isPromise(value) {
|
|
38
|
+
return isObject(value) && "then" in value && isFunction(value.then);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/promiseUtils.ts
|
|
42
|
+
function defer() {
|
|
43
|
+
let resolve;
|
|
44
|
+
let reject;
|
|
45
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
46
|
+
resolve = _resolve;
|
|
47
|
+
reject = _reject;
|
|
48
|
+
});
|
|
49
|
+
return { resolve, reject, promise };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/asyncQueue.ts
|
|
53
|
+
var AsyncQueue = class {
|
|
54
|
+
#queue = [];
|
|
55
|
+
#pending = 0;
|
|
56
|
+
#size = 0;
|
|
57
|
+
#concurrency;
|
|
58
|
+
#completed = 0;
|
|
59
|
+
#failed = 0;
|
|
60
|
+
#idleResolvers = [];
|
|
61
|
+
events = (0, import_evtmitter.evtmitter)();
|
|
62
|
+
#signal;
|
|
63
|
+
#taskTimeout;
|
|
64
|
+
constructor({
|
|
65
|
+
concurrency = 1,
|
|
66
|
+
signal,
|
|
67
|
+
timeout: taskTimeout
|
|
68
|
+
} = {}) {
|
|
69
|
+
this.#concurrency = concurrency;
|
|
70
|
+
this.#signal = signal;
|
|
71
|
+
this.#taskTimeout = taskTimeout;
|
|
72
|
+
}
|
|
73
|
+
#enqueue(task) {
|
|
74
|
+
this.#queue.push(task);
|
|
75
|
+
this.#size++;
|
|
76
|
+
}
|
|
77
|
+
add(fn, options) {
|
|
78
|
+
const deferred = defer();
|
|
79
|
+
const taskTimeout = this.#taskTimeout ?? options?.timeout;
|
|
80
|
+
const task = {
|
|
81
|
+
run: async (ctx) => {
|
|
82
|
+
if (isPromise(fn)) {
|
|
83
|
+
return fn;
|
|
84
|
+
}
|
|
85
|
+
return await fn(ctx);
|
|
86
|
+
},
|
|
87
|
+
resolve: deferred.resolve,
|
|
88
|
+
reject: deferred.reject,
|
|
89
|
+
signal: options?.signal,
|
|
90
|
+
id: options?.id,
|
|
91
|
+
timeout: taskTimeout
|
|
92
|
+
};
|
|
93
|
+
this.#enqueue(task);
|
|
94
|
+
this.#processQueue();
|
|
95
|
+
return deferred.promise;
|
|
96
|
+
}
|
|
97
|
+
resultifyAdd(fn, options) {
|
|
98
|
+
const cb = async (ctx) => {
|
|
99
|
+
if (isPromise(fn)) {
|
|
100
|
+
return await fn;
|
|
101
|
+
}
|
|
102
|
+
return fn(ctx);
|
|
103
|
+
};
|
|
104
|
+
return this.add((ctx) => (0, import_t_result.resultify)(cb(ctx)), options);
|
|
105
|
+
}
|
|
106
|
+
async #processQueue() {
|
|
107
|
+
if (this.#pending >= this.#concurrency || this.#queue.length === 0) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const task = this.#queue.shift();
|
|
111
|
+
if (!task) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
this.#pending++;
|
|
115
|
+
this.#size--;
|
|
116
|
+
const signals = [];
|
|
117
|
+
if (task.signal) {
|
|
118
|
+
signals.push(task.signal);
|
|
119
|
+
}
|
|
120
|
+
if (this.#signal) {
|
|
121
|
+
signals.push(this.#signal);
|
|
122
|
+
}
|
|
123
|
+
if (task.timeout) {
|
|
124
|
+
signals.push(AbortSignal.timeout(task.timeout));
|
|
125
|
+
}
|
|
126
|
+
const signal = signals.length > 1 ? AbortSignal.any(signals) : signals[0];
|
|
127
|
+
let abortListener;
|
|
128
|
+
const signalAbortPromise = new Promise((_, reject) => {
|
|
129
|
+
if (signal) {
|
|
130
|
+
const error = signal.reason instanceof Error ? signal.reason : new DOMException("Aborted", "AbortError");
|
|
131
|
+
if (signal.aborted) {
|
|
132
|
+
reject(error);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
abortListener = () => {
|
|
136
|
+
reject(error);
|
|
137
|
+
};
|
|
138
|
+
signal.addEventListener("abort", abortListener, { once: true });
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
try {
|
|
142
|
+
const taskRunPromise = task.run({ signal, id: task.id });
|
|
143
|
+
this.events.emit("start", { id: task.id });
|
|
144
|
+
const result = await Promise.race([taskRunPromise, signalAbortPromise]);
|
|
145
|
+
if ((0, import_t_result.isResult)(result)) {
|
|
146
|
+
task.resolve(result);
|
|
147
|
+
if (result.error) {
|
|
148
|
+
this.#failed++;
|
|
149
|
+
this.events.emit("error", { id: task.id, error: result.error });
|
|
150
|
+
} else {
|
|
151
|
+
this.#completed++;
|
|
152
|
+
this.events.emit("complete", { id: task.id, value: result.value });
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
const error = new Error("Response not a Result");
|
|
156
|
+
task.resolve(import_t_result.Result.err(error));
|
|
157
|
+
this.#failed++;
|
|
158
|
+
this.events.emit("error", {
|
|
159
|
+
id: task.id,
|
|
160
|
+
error: (0, import_t_result.unknownToError)(error)
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
task.resolve(import_t_result.Result.err(error));
|
|
165
|
+
this.#failed++;
|
|
166
|
+
this.events.emit("error", { id: task.id, error: (0, import_t_result.unknownToError)(error) });
|
|
167
|
+
} finally {
|
|
168
|
+
if (signal && abortListener) {
|
|
169
|
+
signal.removeEventListener("abort", abortListener);
|
|
170
|
+
}
|
|
171
|
+
this.#pending--;
|
|
172
|
+
this.#processQueue();
|
|
173
|
+
if (this.#pending === 0 && this.#size === 0) {
|
|
174
|
+
this.#resolveIdleWaiters();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
#resolveIdleWaiters() {
|
|
179
|
+
while (this.#idleResolvers.length > 0) {
|
|
180
|
+
const resolve = this.#idleResolvers.shift();
|
|
181
|
+
if (resolve) {
|
|
182
|
+
resolve();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async onIdle() {
|
|
187
|
+
if (this.#pending === 0 && this.#size === 0) {
|
|
188
|
+
return Promise.resolve();
|
|
189
|
+
}
|
|
190
|
+
return new Promise((resolve) => {
|
|
191
|
+
this.#idleResolvers.push(resolve);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
clear({ resetCounters = true } = {}) {
|
|
195
|
+
this.#queue = [];
|
|
196
|
+
this.#size = 0;
|
|
197
|
+
if (resetCounters) {
|
|
198
|
+
this.#completed = 0;
|
|
199
|
+
this.#failed = 0;
|
|
200
|
+
}
|
|
201
|
+
if (this.#pending === 0) {
|
|
202
|
+
this.#resolveIdleWaiters();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
get completed() {
|
|
206
|
+
return this.#completed;
|
|
207
|
+
}
|
|
208
|
+
get failed() {
|
|
209
|
+
return this.#failed;
|
|
210
|
+
}
|
|
211
|
+
get pending() {
|
|
212
|
+
return this.#pending;
|
|
213
|
+
}
|
|
214
|
+
get size() {
|
|
215
|
+
return this.#size;
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
var AsyncQueueWithId = class extends AsyncQueue {
|
|
219
|
+
constructor(options) {
|
|
220
|
+
super(options);
|
|
221
|
+
}
|
|
222
|
+
add(fn, options) {
|
|
223
|
+
return super.add(fn, options);
|
|
224
|
+
}
|
|
225
|
+
resultifyAdd(fn, options) {
|
|
226
|
+
return super.resultifyAdd(fn, options);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
function createAsyncQueue(options) {
|
|
230
|
+
return new AsyncQueue(options);
|
|
231
|
+
}
|
|
232
|
+
function createAsyncQueueWithId(options) {
|
|
233
|
+
return new AsyncQueueWithId(options);
|
|
234
|
+
}
|
|
235
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
236
|
+
0 && (module.exports = {
|
|
237
|
+
createAsyncQueue,
|
|
238
|
+
createAsyncQueueWithId
|
|
239
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as evtmitter from 'evtmitter';
|
|
2
|
+
import { ResultValidErrors, Result } from 't-result';
|
|
3
|
+
|
|
4
|
+
type AsyncQueueOptions = {
|
|
5
|
+
concurrency?: number;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
};
|
|
9
|
+
type AddOptions<I> = {
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
id?: I;
|
|
13
|
+
};
|
|
14
|
+
type RunCtx<I> = {
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
id?: I;
|
|
17
|
+
};
|
|
18
|
+
declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = undefined> {
|
|
19
|
+
#private;
|
|
20
|
+
events: evtmitter.Emitter<{
|
|
21
|
+
error: {
|
|
22
|
+
id: I;
|
|
23
|
+
error: E | Error;
|
|
24
|
+
};
|
|
25
|
+
complete: {
|
|
26
|
+
id: I;
|
|
27
|
+
value: T;
|
|
28
|
+
};
|
|
29
|
+
start: {
|
|
30
|
+
id: I;
|
|
31
|
+
};
|
|
32
|
+
}>;
|
|
33
|
+
constructor({ concurrency, signal, timeout: taskTimeout, }?: AsyncQueueOptions);
|
|
34
|
+
add(fn: ((ctx: RunCtx<I>) => Promise<Result<T, E>> | Result<T, E>) | Promise<Result<T, E>>, options?: AddOptions<I>): Promise<Result<T, E | Error>>;
|
|
35
|
+
resultifyAdd(fn: ((ctx: RunCtx<I>) => Promise<T> | T) | Promise<T>, options?: AddOptions<I>): Promise<Result<T, E | Error>>;
|
|
36
|
+
onIdle(): Promise<void>;
|
|
37
|
+
clear({ resetCounters }?: {
|
|
38
|
+
resetCounters?: boolean;
|
|
39
|
+
}): void;
|
|
40
|
+
get completed(): number;
|
|
41
|
+
get failed(): number;
|
|
42
|
+
get pending(): number;
|
|
43
|
+
get size(): number;
|
|
44
|
+
}
|
|
45
|
+
type AddOptionsWithId<I> = Omit<AddOptions<I>, 'id'> & {
|
|
46
|
+
id: I;
|
|
47
|
+
};
|
|
48
|
+
declare class AsyncQueueWithId<T, I, E extends ResultValidErrors = Error> extends AsyncQueue<T, E, I> {
|
|
49
|
+
constructor(options?: AsyncQueueOptions);
|
|
50
|
+
add(fn: ((ctx: RunCtx<I>) => Promise<Result<T, E>> | Result<T, E>) | Promise<Result<T, E>>, options?: AddOptionsWithId<I>): Promise<Result<T, E | Error>>;
|
|
51
|
+
resultifyAdd(fn: ((ctx: RunCtx<I>) => Promise<T> | T) | Promise<T>, options?: AddOptionsWithId<I>): Promise<Result<T, E | Error>>;
|
|
52
|
+
}
|
|
53
|
+
declare function createAsyncQueue<T, E extends ResultValidErrors = Error>(options?: AsyncQueueOptions): AsyncQueue<T, E, undefined>;
|
|
54
|
+
declare function createAsyncQueueWithId<T, I, E extends ResultValidErrors = Error>(options?: AsyncQueueOptions): AsyncQueueWithId<T, I, E>;
|
|
55
|
+
|
|
56
|
+
export { createAsyncQueue, createAsyncQueueWithId };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as evtmitter from 'evtmitter';
|
|
2
|
+
import { ResultValidErrors, Result } from 't-result';
|
|
3
|
+
|
|
4
|
+
type AsyncQueueOptions = {
|
|
5
|
+
concurrency?: number;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
};
|
|
9
|
+
type AddOptions<I> = {
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
timeout?: number;
|
|
12
|
+
id?: I;
|
|
13
|
+
};
|
|
14
|
+
type RunCtx<I> = {
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
id?: I;
|
|
17
|
+
};
|
|
18
|
+
declare class AsyncQueue<T, E extends ResultValidErrors = Error, I = undefined> {
|
|
19
|
+
#private;
|
|
20
|
+
events: evtmitter.Emitter<{
|
|
21
|
+
error: {
|
|
22
|
+
id: I;
|
|
23
|
+
error: E | Error;
|
|
24
|
+
};
|
|
25
|
+
complete: {
|
|
26
|
+
id: I;
|
|
27
|
+
value: T;
|
|
28
|
+
};
|
|
29
|
+
start: {
|
|
30
|
+
id: I;
|
|
31
|
+
};
|
|
32
|
+
}>;
|
|
33
|
+
constructor({ concurrency, signal, timeout: taskTimeout, }?: AsyncQueueOptions);
|
|
34
|
+
add(fn: ((ctx: RunCtx<I>) => Promise<Result<T, E>> | Result<T, E>) | Promise<Result<T, E>>, options?: AddOptions<I>): Promise<Result<T, E | Error>>;
|
|
35
|
+
resultifyAdd(fn: ((ctx: RunCtx<I>) => Promise<T> | T) | Promise<T>, options?: AddOptions<I>): Promise<Result<T, E | Error>>;
|
|
36
|
+
onIdle(): Promise<void>;
|
|
37
|
+
clear({ resetCounters }?: {
|
|
38
|
+
resetCounters?: boolean;
|
|
39
|
+
}): void;
|
|
40
|
+
get completed(): number;
|
|
41
|
+
get failed(): number;
|
|
42
|
+
get pending(): number;
|
|
43
|
+
get size(): number;
|
|
44
|
+
}
|
|
45
|
+
type AddOptionsWithId<I> = Omit<AddOptions<I>, 'id'> & {
|
|
46
|
+
id: I;
|
|
47
|
+
};
|
|
48
|
+
declare class AsyncQueueWithId<T, I, E extends ResultValidErrors = Error> extends AsyncQueue<T, E, I> {
|
|
49
|
+
constructor(options?: AsyncQueueOptions);
|
|
50
|
+
add(fn: ((ctx: RunCtx<I>) => Promise<Result<T, E>> | Result<T, E>) | Promise<Result<T, E>>, options?: AddOptionsWithId<I>): Promise<Result<T, E | Error>>;
|
|
51
|
+
resultifyAdd(fn: ((ctx: RunCtx<I>) => Promise<T> | T) | Promise<T>, options?: AddOptionsWithId<I>): Promise<Result<T, E | Error>>;
|
|
52
|
+
}
|
|
53
|
+
declare function createAsyncQueue<T, E extends ResultValidErrors = Error>(options?: AsyncQueueOptions): AsyncQueue<T, E, undefined>;
|
|
54
|
+
declare function createAsyncQueueWithId<T, I, E extends ResultValidErrors = Error>(options?: AsyncQueueOptions): AsyncQueueWithId<T, I, E>;
|
|
55
|
+
|
|
56
|
+
export { createAsyncQueue, createAsyncQueueWithId };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defer
|
|
3
|
+
} from "./chunk-DFXNVEH6.js";
|
|
4
|
+
import {
|
|
5
|
+
isPromise
|
|
6
|
+
} from "./chunk-3XCS7FVO.js";
|
|
7
|
+
|
|
8
|
+
// src/asyncQueue.ts
|
|
9
|
+
import { evtmitter } from "evtmitter";
|
|
10
|
+
import {
|
|
11
|
+
isResult,
|
|
12
|
+
Result,
|
|
13
|
+
resultify,
|
|
14
|
+
unknownToError
|
|
15
|
+
} from "t-result";
|
|
16
|
+
var AsyncQueue = class {
|
|
17
|
+
#queue = [];
|
|
18
|
+
#pending = 0;
|
|
19
|
+
#size = 0;
|
|
20
|
+
#concurrency;
|
|
21
|
+
#completed = 0;
|
|
22
|
+
#failed = 0;
|
|
23
|
+
#idleResolvers = [];
|
|
24
|
+
events = evtmitter();
|
|
25
|
+
#signal;
|
|
26
|
+
#taskTimeout;
|
|
27
|
+
constructor({
|
|
28
|
+
concurrency = 1,
|
|
29
|
+
signal,
|
|
30
|
+
timeout: taskTimeout
|
|
31
|
+
} = {}) {
|
|
32
|
+
this.#concurrency = concurrency;
|
|
33
|
+
this.#signal = signal;
|
|
34
|
+
this.#taskTimeout = taskTimeout;
|
|
35
|
+
}
|
|
36
|
+
#enqueue(task) {
|
|
37
|
+
this.#queue.push(task);
|
|
38
|
+
this.#size++;
|
|
39
|
+
}
|
|
40
|
+
add(fn, options) {
|
|
41
|
+
const deferred = defer();
|
|
42
|
+
const taskTimeout = this.#taskTimeout ?? options?.timeout;
|
|
43
|
+
const task = {
|
|
44
|
+
run: async (ctx) => {
|
|
45
|
+
if (isPromise(fn)) {
|
|
46
|
+
return fn;
|
|
47
|
+
}
|
|
48
|
+
return await fn(ctx);
|
|
49
|
+
},
|
|
50
|
+
resolve: deferred.resolve,
|
|
51
|
+
reject: deferred.reject,
|
|
52
|
+
signal: options?.signal,
|
|
53
|
+
id: options?.id,
|
|
54
|
+
timeout: taskTimeout
|
|
55
|
+
};
|
|
56
|
+
this.#enqueue(task);
|
|
57
|
+
this.#processQueue();
|
|
58
|
+
return deferred.promise;
|
|
59
|
+
}
|
|
60
|
+
resultifyAdd(fn, options) {
|
|
61
|
+
const cb = async (ctx) => {
|
|
62
|
+
if (isPromise(fn)) {
|
|
63
|
+
return await fn;
|
|
64
|
+
}
|
|
65
|
+
return fn(ctx);
|
|
66
|
+
};
|
|
67
|
+
return this.add((ctx) => resultify(cb(ctx)), options);
|
|
68
|
+
}
|
|
69
|
+
async #processQueue() {
|
|
70
|
+
if (this.#pending >= this.#concurrency || this.#queue.length === 0) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const task = this.#queue.shift();
|
|
74
|
+
if (!task) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.#pending++;
|
|
78
|
+
this.#size--;
|
|
79
|
+
const signals = [];
|
|
80
|
+
if (task.signal) {
|
|
81
|
+
signals.push(task.signal);
|
|
82
|
+
}
|
|
83
|
+
if (this.#signal) {
|
|
84
|
+
signals.push(this.#signal);
|
|
85
|
+
}
|
|
86
|
+
if (task.timeout) {
|
|
87
|
+
signals.push(AbortSignal.timeout(task.timeout));
|
|
88
|
+
}
|
|
89
|
+
const signal = signals.length > 1 ? AbortSignal.any(signals) : signals[0];
|
|
90
|
+
let abortListener;
|
|
91
|
+
const signalAbortPromise = new Promise((_, reject) => {
|
|
92
|
+
if (signal) {
|
|
93
|
+
const error = signal.reason instanceof Error ? signal.reason : new DOMException("Aborted", "AbortError");
|
|
94
|
+
if (signal.aborted) {
|
|
95
|
+
reject(error);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
abortListener = () => {
|
|
99
|
+
reject(error);
|
|
100
|
+
};
|
|
101
|
+
signal.addEventListener("abort", abortListener, { once: true });
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
try {
|
|
105
|
+
const taskRunPromise = task.run({ signal, id: task.id });
|
|
106
|
+
this.events.emit("start", { id: task.id });
|
|
107
|
+
const result = await Promise.race([taskRunPromise, signalAbortPromise]);
|
|
108
|
+
if (isResult(result)) {
|
|
109
|
+
task.resolve(result);
|
|
110
|
+
if (result.error) {
|
|
111
|
+
this.#failed++;
|
|
112
|
+
this.events.emit("error", { id: task.id, error: result.error });
|
|
113
|
+
} else {
|
|
114
|
+
this.#completed++;
|
|
115
|
+
this.events.emit("complete", { id: task.id, value: result.value });
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
const error = new Error("Response not a Result");
|
|
119
|
+
task.resolve(Result.err(error));
|
|
120
|
+
this.#failed++;
|
|
121
|
+
this.events.emit("error", {
|
|
122
|
+
id: task.id,
|
|
123
|
+
error: unknownToError(error)
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
task.resolve(Result.err(error));
|
|
128
|
+
this.#failed++;
|
|
129
|
+
this.events.emit("error", { id: task.id, error: unknownToError(error) });
|
|
130
|
+
} finally {
|
|
131
|
+
if (signal && abortListener) {
|
|
132
|
+
signal.removeEventListener("abort", abortListener);
|
|
133
|
+
}
|
|
134
|
+
this.#pending--;
|
|
135
|
+
this.#processQueue();
|
|
136
|
+
if (this.#pending === 0 && this.#size === 0) {
|
|
137
|
+
this.#resolveIdleWaiters();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
#resolveIdleWaiters() {
|
|
142
|
+
while (this.#idleResolvers.length > 0) {
|
|
143
|
+
const resolve = this.#idleResolvers.shift();
|
|
144
|
+
if (resolve) {
|
|
145
|
+
resolve();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async onIdle() {
|
|
150
|
+
if (this.#pending === 0 && this.#size === 0) {
|
|
151
|
+
return Promise.resolve();
|
|
152
|
+
}
|
|
153
|
+
return new Promise((resolve) => {
|
|
154
|
+
this.#idleResolvers.push(resolve);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
clear({ resetCounters = true } = {}) {
|
|
158
|
+
this.#queue = [];
|
|
159
|
+
this.#size = 0;
|
|
160
|
+
if (resetCounters) {
|
|
161
|
+
this.#completed = 0;
|
|
162
|
+
this.#failed = 0;
|
|
163
|
+
}
|
|
164
|
+
if (this.#pending === 0) {
|
|
165
|
+
this.#resolveIdleWaiters();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
get completed() {
|
|
169
|
+
return this.#completed;
|
|
170
|
+
}
|
|
171
|
+
get failed() {
|
|
172
|
+
return this.#failed;
|
|
173
|
+
}
|
|
174
|
+
get pending() {
|
|
175
|
+
return this.#pending;
|
|
176
|
+
}
|
|
177
|
+
get size() {
|
|
178
|
+
return this.#size;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var AsyncQueueWithId = class extends AsyncQueue {
|
|
182
|
+
constructor(options) {
|
|
183
|
+
super(options);
|
|
184
|
+
}
|
|
185
|
+
add(fn, options) {
|
|
186
|
+
return super.add(fn, options);
|
|
187
|
+
}
|
|
188
|
+
resultifyAdd(fn, options) {
|
|
189
|
+
return super.resultifyAdd(fn, options);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function createAsyncQueue(options) {
|
|
193
|
+
return new AsyncQueue(options);
|
|
194
|
+
}
|
|
195
|
+
function createAsyncQueueWithId(options) {
|
|
196
|
+
return new AsyncQueueWithId(options);
|
|
197
|
+
}
|
|
198
|
+
export {
|
|
199
|
+
createAsyncQueue,
|
|
200
|
+
createAsyncQueueWithId
|
|
201
|
+
};
|
package/lib/cache.js
CHANGED
|
@@ -2,10 +2,10 @@ import {
|
|
|
2
2
|
durationObjToMs
|
|
3
3
|
} from "./chunk-5MNYPLZI.js";
|
|
4
4
|
import "./chunk-HTCYUMDR.js";
|
|
5
|
+
import "./chunk-II4R3VVX.js";
|
|
5
6
|
import {
|
|
6
7
|
isPromise
|
|
7
8
|
} from "./chunk-3XCS7FVO.js";
|
|
8
|
-
import "./chunk-II4R3VVX.js";
|
|
9
9
|
|
|
10
10
|
// src/cache.ts
|
|
11
11
|
function cachedGetter(getter) {
|