socket-function 0.120.0 → 0.122.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 +2 -2
- package/src/batching.ts +50 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "socket-function",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.122.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"dependencies": {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"pako": "^2.1.0",
|
|
12
12
|
"preact": "10.24.3",
|
|
13
13
|
"typedev": "^0.4.0",
|
|
14
|
-
"typenode": "^5.
|
|
14
|
+
"typenode": "^5.12.0",
|
|
15
15
|
"ws": "^8.17.1"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
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
|