socket-function 0.151.0 → 0.153.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 +32 -1
- package/src/misc.ts +4 -0
package/package.json
CHANGED
package/src/batching.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PromiseObj, isNode, timeoutToError } from "./misc";
|
|
2
2
|
import { measureWrap } from "./profiling/measure";
|
|
3
|
-
import { Args, MaybePromise } from "./types";
|
|
3
|
+
import { AnyFunction, Args, MaybePromise } from "./types";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
6
|
"numbers" use setTimeout
|
|
@@ -298,4 +298,35 @@ async function runPollFnc(fnc: () => Promise<void> | void) {
|
|
|
298
298
|
export async function shutdownPolling() {
|
|
299
299
|
pollingRunning = false;
|
|
300
300
|
await Promise.all(Array.from(pendingPolls));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
const DEFAULT_RETRY_DELAY = 5000;
|
|
305
|
+
const DEFAULT_MAX_RETRIES = 3;
|
|
306
|
+
export function retryFunctional<T extends AnyFunction>(fnc: T, config?: {
|
|
307
|
+
maxRetries?: number;
|
|
308
|
+
shouldRetry?: (message: string) => boolean;
|
|
309
|
+
minDelay?: number;
|
|
310
|
+
maxDelay?: number;
|
|
311
|
+
}): T {
|
|
312
|
+
let { maxRetries = DEFAULT_MAX_RETRIES, shouldRetry, minDelay = DEFAULT_RETRY_DELAY, maxDelay = DEFAULT_RETRY_DELAY } = config || {};
|
|
313
|
+
let expFactor = Math.max(1, Math.log(maxDelay / minDelay) / Math.log(Math.max(maxRetries, 2)));
|
|
314
|
+
async function runFnc(args: any[], retries: number): Promise<ReturnType<T>> {
|
|
315
|
+
try {
|
|
316
|
+
return await (fnc as any)(...args);
|
|
317
|
+
} catch (e: any) {
|
|
318
|
+
if (shouldRetry && !shouldRetry(String(e.stack))) {
|
|
319
|
+
throw e;
|
|
320
|
+
}
|
|
321
|
+
if (retries < 0) throw e;
|
|
322
|
+
console.warn(`Retrying ${fnc.name}, due to error ${String(e.stack)}`);
|
|
323
|
+
retries--;
|
|
324
|
+
let curCount = maxRetries - retries;
|
|
325
|
+
await delay(minDelay * expFactor ** curCount);
|
|
326
|
+
return runFnc(args, retries);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return async function (...args: any[]) {
|
|
330
|
+
return await runFnc(args, maxRetries);
|
|
331
|
+
} as any;
|
|
301
332
|
}
|
package/src/misc.ts
CHANGED
|
@@ -77,6 +77,7 @@ export function recursiveFreeze<T>(obj: T): T {
|
|
|
77
77
|
if (!canHaveChildren(obj)) return;
|
|
78
78
|
if (visited.has(obj)) return;
|
|
79
79
|
visited.add(obj);
|
|
80
|
+
if (ArrayBuffer.isView(obj)) return;
|
|
80
81
|
Object.freeze(obj);
|
|
81
82
|
let keys = getKeys(obj);
|
|
82
83
|
for (let key of keys) {
|
|
@@ -338,6 +339,9 @@ export class QueueLimited<T> {
|
|
|
338
339
|
export function binarySearchBasic<T, V>(array: T[], getVal: (val: T) => V, searchValue: V): number {
|
|
339
340
|
return binarySearchIndex(array.length, i => compare(getVal(array[i]), searchValue));
|
|
340
341
|
}
|
|
342
|
+
export function binarySearchBasic2<T, V>(array: T[], getVal: (val: T) => V, searchValue: T): number {
|
|
343
|
+
return binarySearchIndex(array.length, i => compare(getVal(array[i]), getVal(searchValue)));
|
|
344
|
+
}
|
|
341
345
|
|
|
342
346
|
/**
|
|
343
347
|
* Searches indexes, allowing you to query structures that aren't arrays. To search an array, use:
|