socket-function 0.119.0 → 0.121.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/package.json +1 -1
- package/src/batching.ts +50 -2
- package/src/misc.ts +1 -1
package/package.json
CHANGED
package/src/batching.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isNode } from "./misc";
|
|
1
|
+
import { PromiseObj, isNode, timeoutToError } from "./misc";
|
|
2
2
|
import { measureWrap } from "./profiling/measure";
|
|
3
|
-
import { MaybePromise } from "./types";
|
|
3
|
+
import { Args, MaybePromise } from "./types";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
6
|
"numbers" use setTimeout
|
|
@@ -197,6 +197,54 @@ export function runInSerial<T extends (...args: any[]) => Promise<any>>(fnc: T):
|
|
|
197
197
|
}) as T;
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
+
|
|
201
|
+
export function runInParallel<T extends (...args: any[]) => Promise<any>>(
|
|
202
|
+
config: {
|
|
203
|
+
parallelCount: number;
|
|
204
|
+
callTimeout?: number;
|
|
205
|
+
},
|
|
206
|
+
fnc: T
|
|
207
|
+
): T {
|
|
208
|
+
let queued: {
|
|
209
|
+
parameters: Args<T>;
|
|
210
|
+
result: PromiseObj<ReturnType<T>>;
|
|
211
|
+
}[] = [];
|
|
212
|
+
let runningCount = 0;
|
|
213
|
+
|
|
214
|
+
function runIfNeeded() {
|
|
215
|
+
if (runningCount >= config.parallelCount) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const queuedObj = queued.shift();
|
|
219
|
+
if (!queuedObj) return;
|
|
220
|
+
|
|
221
|
+
queuedObj.result.resolve((async () => {
|
|
222
|
+
runningCount++;
|
|
223
|
+
try {
|
|
224
|
+
if (config.callTimeout) {
|
|
225
|
+
return await timeoutToError(config.callTimeout, fnc(...queuedObj.parameters), () => new Error(`Parallel call timed out for fnc ${fnc.name}`));
|
|
226
|
+
} else {
|
|
227
|
+
return await fnc(...queuedObj.parameters);
|
|
228
|
+
}
|
|
229
|
+
} finally {
|
|
230
|
+
runningCount--;
|
|
231
|
+
runIfNeeded();
|
|
232
|
+
}
|
|
233
|
+
})());
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function parallelCall(...args: Args<T>) {
|
|
237
|
+
queued.push({
|
|
238
|
+
parameters: args,
|
|
239
|
+
result: new PromiseObj<ReturnType<T>>(),
|
|
240
|
+
});
|
|
241
|
+
let queuedObj = queued[queued.length - 1];
|
|
242
|
+
runIfNeeded();
|
|
243
|
+
return queuedObj.result.promise;
|
|
244
|
+
}
|
|
245
|
+
return parallelCall as T;
|
|
246
|
+
}
|
|
247
|
+
|
|
200
248
|
export function runInfinitePoll(
|
|
201
249
|
delayTime: number,
|
|
202
250
|
fnc: () => Promise<void> | void
|
package/src/misc.ts
CHANGED
|
@@ -51,7 +51,7 @@ export function arrayEqual(a: { [key: number]: unknown; length: number }, b: { [
|
|
|
51
51
|
return true;
|
|
52
52
|
}
|
|
53
53
|
export function isNode() {
|
|
54
|
-
return typeof document === "undefined";
|
|
54
|
+
return typeof document === "undefined" && typeof (globalThis as any).WorkerGlobalScope === "undefined";
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export function isNodeTrue() {
|