@keplr-wallet/common 0.12.30 → 0.12.31

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/build/index.d.ts CHANGED
@@ -5,3 +5,5 @@ export * from "./utils";
5
5
  export * from "./escape";
6
6
  export * from "./json";
7
7
  export * from "./icns";
8
+ export * from "./retry";
9
+ export * from "./sleep";
package/build/index.js CHANGED
@@ -21,4 +21,6 @@ __exportStar(require("./utils"), exports);
21
21
  __exportStar(require("./escape"), exports);
22
22
  __exportStar(require("./json"), exports);
23
23
  __exportStar(require("./icns"), exports);
24
+ __exportStar(require("./retry"), exports);
25
+ __exportStar(require("./sleep"), exports);
24
26
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB;AACxB,yCAAuB;AACvB,0CAAwB;AACxB,2CAAyB;AACzB,yCAAuB;AACvB,yCAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,0CAAwB;AACxB,yCAAuB;AACvB,0CAAwB;AACxB,2CAAyB;AACzB,yCAAuB;AACvB,yCAAuB;AACvB,0CAAwB;AACxB,0CAAwB"}
@@ -0,0 +1,8 @@
1
+ export interface RetryOpts {
2
+ readonly maxRetries: number;
3
+ readonly waitMsAfterError?: number;
4
+ readonly maxWaitMsAfterError?: number;
5
+ readonly onFailed?: (e: any) => void;
6
+ }
7
+ export declare function retry<R>(task: () => Promise<R>, opts: RetryOpts): Promise<R>;
8
+ export declare function retryInfinite<R>(task: () => Promise<R>, opts: Omit<RetryOpts, "maxRetries">): Promise<R>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.retryInfinite = exports.retry = void 0;
13
+ const sleep_1 = require("../sleep");
14
+ function retry(task, opts) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ let retries = 0;
17
+ while (true) {
18
+ try {
19
+ const promise = task();
20
+ return yield promise;
21
+ }
22
+ catch (e) {
23
+ if (retries >= opts.maxRetries) {
24
+ throw e;
25
+ }
26
+ if (opts.onFailed) {
27
+ opts.onFailed(e);
28
+ }
29
+ let waitMs = opts.waitMsAfterError || 0;
30
+ if (waitMs > 0) {
31
+ if (opts.maxWaitMsAfterError != null && opts.maxWaitMsAfterError > 0) {
32
+ waitMs = Math.pow(2, retries) * waitMs;
33
+ waitMs = Math.min(waitMs, opts.maxWaitMsAfterError);
34
+ }
35
+ yield (0, sleep_1.sleep)(waitMs);
36
+ }
37
+ retries++;
38
+ }
39
+ }
40
+ });
41
+ }
42
+ exports.retry = retry;
43
+ function retryInfinite(task, opts) {
44
+ return retry(task, Object.assign(Object.assign({}, opts), { maxRetries: Number.MAX_SAFE_INTEGER }));
45
+ }
46
+ exports.retryInfinite = retryInfinite;
47
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/retry/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,oCAAiC;AASjC,SAAsB,KAAK,CACzB,IAAsB,EACtB,IAAe;;QAEf,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,OAAO,IAAI,EAAE;YACX,IAAI;gBACF,MAAM,OAAO,GAAG,IAAI,EAAE,CAAC;gBACvB,OAAO,MAAM,OAAO,CAAC;aACtB;YAAC,OAAO,CAAC,EAAE;gBACV,IAAI,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE;oBAC9B,MAAM,CAAC,CAAC;iBACT;gBAED,IAAI,IAAI,CAAC,QAAQ,EAAE;oBACjB,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;iBAClB;gBAED,IAAI,MAAM,GAAG,IAAI,CAAC,gBAAgB,IAAI,CAAC,CAAC;gBACxC,IAAI,MAAM,GAAG,CAAC,EAAE;oBACd,IAAI,IAAI,CAAC,mBAAmB,IAAI,IAAI,IAAI,IAAI,CAAC,mBAAmB,GAAG,CAAC,EAAE;wBACpE,MAAM,GAAG,SAAA,CAAC,EAAI,OAAO,CAAA,GAAG,MAAM,CAAC;wBAC/B,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,mBAAmB,CAAC,CAAC;qBACrD;oBAED,MAAM,IAAA,aAAK,EAAC,MAAM,CAAC,CAAC;iBACrB;gBAED,OAAO,EAAE,CAAC;aACX;SACF;IACH,CAAC;CAAA;AA/BD,sBA+BC;AAED,SAAgB,aAAa,CAC3B,IAAsB,EACtB,IAAmC;IAEnC,OAAO,KAAK,CAAC,IAAI,kCACZ,IAAI,KACP,UAAU,EAAE,MAAM,CAAC,gBAAgB,IACnC,CAAC;AACL,CAAC;AARD,sCAQC"}
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number): Promise<void>;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sleep = void 0;
4
+ function sleep(ms) {
5
+ return new Promise((resolve) => setTimeout(resolve, ms));
6
+ }
7
+ exports.sleep = sleep;
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sleep/index.ts"],"names":[],"mappings":";;;AAAA,SAAgB,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAFD,sBAEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keplr-wallet/common",
3
- "version": "0.12.30",
3
+ "version": "0.12.31",
4
4
  "main": "build/index.js",
5
5
  "author": "chainapsis",
6
6
  "license": "Apache-2.0",
@@ -16,11 +16,11 @@
16
16
  "lint-fix": "eslint --fix \"src/**/*\" && prettier --write \"src/**/*\""
17
17
  },
18
18
  "dependencies": {
19
- "@keplr-wallet/crypto": "0.12.30",
20
- "@keplr-wallet/types": "0.12.30",
19
+ "@keplr-wallet/crypto": "0.12.31",
20
+ "@keplr-wallet/types": "0.12.31",
21
21
  "buffer": "^6.0.3",
22
22
  "delay": "^4.4.0",
23
23
  "mobx": "^6.1.7"
24
24
  },
25
- "gitHead": "1bbf3366c918735099f8a22389cc49330b3c2ced"
25
+ "gitHead": "92f9663be3d8ec5876c41430e309dfd2d56006b0"
26
26
  }
package/src/index.ts CHANGED
@@ -5,3 +5,5 @@ export * from "./utils";
5
5
  export * from "./escape";
6
6
  export * from "./json";
7
7
  export * from "./icns";
8
+ export * from "./retry";
9
+ export * from "./sleep";
@@ -0,0 +1,51 @@
1
+ import { sleep } from "../sleep";
2
+
3
+ export interface RetryOpts {
4
+ readonly maxRetries: number;
5
+ readonly waitMsAfterError?: number;
6
+ readonly maxWaitMsAfterError?: number;
7
+ readonly onFailed?: (e: any) => void;
8
+ }
9
+
10
+ export async function retry<R>(
11
+ task: () => Promise<R>,
12
+ opts: RetryOpts
13
+ ): Promise<R> {
14
+ let retries = 0;
15
+ while (true) {
16
+ try {
17
+ const promise = task();
18
+ return await promise;
19
+ } catch (e) {
20
+ if (retries >= opts.maxRetries) {
21
+ throw e;
22
+ }
23
+
24
+ if (opts.onFailed) {
25
+ opts.onFailed(e);
26
+ }
27
+
28
+ let waitMs = opts.waitMsAfterError || 0;
29
+ if (waitMs > 0) {
30
+ if (opts.maxWaitMsAfterError != null && opts.maxWaitMsAfterError > 0) {
31
+ waitMs = 2 ** retries * waitMs;
32
+ waitMs = Math.min(waitMs, opts.maxWaitMsAfterError);
33
+ }
34
+
35
+ await sleep(waitMs);
36
+ }
37
+
38
+ retries++;
39
+ }
40
+ }
41
+ }
42
+
43
+ export function retryInfinite<R>(
44
+ task: () => Promise<R>,
45
+ opts: Omit<RetryOpts, "maxRetries">
46
+ ): Promise<R> {
47
+ return retry(task, {
48
+ ...opts,
49
+ maxRetries: Number.MAX_SAFE_INTEGER,
50
+ });
51
+ }
@@ -0,0 +1,3 @@
1
+ export function sleep(ms: number): Promise<void> {
2
+ return new Promise((resolve) => setTimeout(resolve, ms));
3
+ }