socket-function 0.152.0 → 0.154.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.152.0",
3
+ "version": "0.154.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.0",
13
13
  "typedev": "^0.4.0",
14
- "typenode": "^5.12.0",
14
+ "typenode": "^5.13.0",
15
15
  "ws": "^8.17.1"
16
16
  },
17
17
  "scripts": {
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
  }
@@ -132,6 +132,8 @@ export function formatNumber(count: number | undefined, maxAbsoluteValue?: numbe
132
132
 
133
133
  maxAbsoluteValue = maxAbsoluteValue ?? Math.abs(count);
134
134
 
135
+ let maxDecimals = noDecimal ? 0 : 3;
136
+
135
137
  // NOTE: We don't switch units as soon as we possible can, because...
136
138
  // 3.594 vs 3.584 is harder to quickly distinguish compared to 3594 and 3584,
137
139
  // the decimal simply makes it harder to read, and larger.
@@ -167,8 +169,6 @@ export function formatNumber(count: number | undefined, maxAbsoluteValue?: numbe
167
169
  count /= divisor;
168
170
  maxAbsoluteValue /= divisor;
169
171
 
170
- let maxDecimals = noDecimal ? 0 : 3;
171
-
172
172
  return formatMaxDecimals(count, maxDecimals, maxAbsoluteValue, currencyDecimalsNeeded ? 2 : undefined) + suffix;
173
173
  }
174
174