@hardlydifficult/throttle 3.0.4 → 3.0.6

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/dist/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export { getBackoffDelay, sleep, getRandomDelay, type BackoffOptions, } from "./
3
3
  export { isConnectionError } from "./isConnectionError";
4
4
  export { createThrottledUpdater, type ThrottledUpdater, } from "./ThrottledUpdater";
5
5
  export { eventRequest, type EventSubscriber, type EventRequestOptions, } from "./eventRequest";
6
+ export { retry, type RetryOptions } from "./retry";
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,KAAK,eAAe,EACpB,KAAK,iBAAiB,GACvB,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,eAAe,EACf,KAAK,EACL,cAAc,EACd,KAAK,cAAc,GACpB,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EACL,sBAAsB,EACtB,KAAK,gBAAgB,GACtB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,YAAY,EACZ,KAAK,eAAe,EACpB,KAAK,mBAAmB,GACzB,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,KAAK,eAAe,EACpB,KAAK,iBAAiB,GACvB,MAAM,YAAY,CAAC;AAEpB,OAAO,EACL,eAAe,EACf,KAAK,EACL,cAAc,EACd,KAAK,cAAc,GACpB,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EACL,sBAAsB,EACtB,KAAK,gBAAgB,GACtB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,YAAY,EACZ,KAAK,eAAe,EACpB,KAAK,mBAAmB,GACzB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.eventRequest = exports.createThrottledUpdater = exports.isConnectionError = exports.getRandomDelay = exports.sleep = exports.getBackoffDelay = exports.Throttle = void 0;
3
+ exports.retry = exports.eventRequest = exports.createThrottledUpdater = exports.isConnectionError = exports.getRandomDelay = exports.sleep = exports.getBackoffDelay = exports.Throttle = void 0;
4
4
  var Throttle_1 = require("./Throttle");
5
5
  Object.defineProperty(exports, "Throttle", { enumerable: true, get: function () { return Throttle_1.Throttle; } });
6
6
  var backoff_1 = require("./backoff");
@@ -13,4 +13,6 @@ var ThrottledUpdater_1 = require("./ThrottledUpdater");
13
13
  Object.defineProperty(exports, "createThrottledUpdater", { enumerable: true, get: function () { return ThrottledUpdater_1.createThrottledUpdater; } });
14
14
  var eventRequest_1 = require("./eventRequest");
15
15
  Object.defineProperty(exports, "eventRequest", { enumerable: true, get: function () { return eventRequest_1.eventRequest; } });
16
+ var retry_1 = require("./retry");
17
+ Object.defineProperty(exports, "retry", { enumerable: true, get: function () { return retry_1.retry; } });
16
18
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAIoB;AAHlB,oGAAA,QAAQ,OAAA;AAKV,qCAKmB;AAJjB,0GAAA,eAAe,OAAA;AACf,gGAAA,KAAK,OAAA;AACL,yGAAA,cAAc,OAAA;AAIhB,yDAAwD;AAA/C,sHAAA,iBAAiB,OAAA;AAE1B,uDAG4B;AAF1B,0HAAA,sBAAsB,OAAA;AAIxB,+CAIwB;AAHtB,4GAAA,YAAY,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAIoB;AAHlB,oGAAA,QAAQ,OAAA;AAKV,qCAKmB;AAJjB,0GAAA,eAAe,OAAA;AACf,gGAAA,KAAK,OAAA;AACL,yGAAA,cAAc,OAAA;AAIhB,yDAAwD;AAA/C,sHAAA,iBAAiB,OAAA;AAE1B,uDAG4B;AAF1B,0HAAA,sBAAsB,OAAA;AAIxB,+CAIwB;AAHtB,4GAAA,YAAY,OAAA;AAKd,iCAAmD;AAA1C,8FAAA,KAAK,OAAA"}
@@ -0,0 +1,14 @@
1
+ export interface RetryOptions {
2
+ /** Maximum number of attempts (must be >= 1) */
3
+ maxAttempts: number;
4
+ /** Called before each retry with the error and 1-based attempt number that failed */
5
+ onRetry?: (error: Error, attempt: number) => void;
6
+ }
7
+ /**
8
+ * Retry an async function up to `maxAttempts` times.
9
+ * Returns the first successful result or throws the last error.
10
+ *
11
+ * No built-in delay — callers can `await sleep()` inside `onRetry` for backoff.
12
+ */
13
+ export declare function retry<T>(fn: () => Promise<T>, options: RetryOptions): Promise<T>;
14
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,gDAAgD;IAChD,WAAW,EAAE,MAAM,CAAC;IACpB,qFAAqF;IACrF,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACnD;AAED;;;;;GAKG;AACH,wBAAsB,KAAK,CAAC,CAAC,EAC3B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,CAAC,CAAC,CAaZ"}
package/dist/retry.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.retry = retry;
4
+ /**
5
+ * Retry an async function up to `maxAttempts` times.
6
+ * Returns the first successful result or throws the last error.
7
+ *
8
+ * No built-in delay — callers can `await sleep()` inside `onRetry` for backoff.
9
+ */
10
+ async function retry(fn, options) {
11
+ let lastError;
12
+ for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
13
+ try {
14
+ return await fn();
15
+ }
16
+ catch (error) {
17
+ lastError = error instanceof Error ? error : new Error(String(error));
18
+ if (attempt < options.maxAttempts) {
19
+ options.onRetry?.(lastError, attempt);
20
+ }
21
+ }
22
+ }
23
+ throw lastError;
24
+ }
25
+ //# sourceMappingURL=retry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.js","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":";;AAaA,sBAgBC;AAtBD;;;;;GAKG;AACI,KAAK,UAAU,KAAK,CACzB,EAAoB,EACpB,OAAqB;IAErB,IAAI,SAAiB,CAAC;IACtB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,OAAO,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC;QAChE,IAAI,CAAC;YACH,OAAO,MAAM,EAAE,EAAE,CAAC;QACpB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YACtE,IAAI,OAAO,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;gBAClC,OAAO,CAAC,OAAO,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;IACH,CAAC;IACD,MAAM,SAAS,CAAC;AAClB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hardlydifficult/throttle",
3
- "version": "3.0.4",
3
+ "version": "3.0.6",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [