@haechi-labs/henesis-provider
Advanced tools
Comparing version 1.0.0-beta.12 to 1.0.0-beta.13
@@ -6,2 +6,14 @@ # Change Log | ||
# [1.0.0-beta.13](https://github.com/HAECHI-LABS/henesis-sdk-js/compare/v1.0.0-beta.10...v1.0.0-beta.13) (2020-02-13) | ||
### Features | ||
* add retry logic ([2a720ff](https://github.com/HAECHI-LABS/henesis-sdk-js/commit/2a720ff)) | ||
* add Sentry to sdk ([b6f1440](https://github.com/HAECHI-LABS/henesis-sdk-js/commit/b6f1440)) | ||
# [1.0.0-beta.12](https://github.com/HAECHI-LABS/henesis-sdk-js/compare/v1.0.0-beta.10...v1.0.0-beta.12) (2020-01-17) | ||
@@ -8,0 +20,0 @@ |
@@ -15,2 +15,4 @@ import { w3cwebsocket as W3CWebsocket } from "websocket"; | ||
private readonly KEEP_ALIVE_INTERVAL; | ||
private readonly RECONNECT_INTERVAL; | ||
private readonly RETRY_INTERVAL; | ||
private keepAliveHandle; | ||
@@ -22,4 +24,9 @@ constructor(connection: W3CWebsocket, timeout: number); | ||
send(method: string, parameters: any[]): Promise<any>; | ||
sendWithRetry(method: string, parameters: any[]): Promise<any>; | ||
private validateJsonRpcResponse; | ||
sendAsync(method: string, parameters: any[]): Promise<void>; | ||
sendAsyncWithRetry(method: string, parameters: any[]): Promise<void>; | ||
sendPayload(payload: any, waitResponse: boolean): Promise<JsonRpcResponse>; | ||
private sendPayloadWithRetry; | ||
private retry; | ||
private _keepAlive; | ||
@@ -31,3 +38,3 @@ onConnect(e: any): Promise<void>; | ||
onClose(e: any): void; | ||
reconnect(): void; | ||
private reconnect; | ||
disconnect(code?: number, reason?: string): void; | ||
@@ -34,0 +41,0 @@ registerEventListeners(): void; |
@@ -18,3 +18,5 @@ "use strict"; | ||
this.HEARTBEAT = 'HEARTBEAT'; | ||
this.KEEP_ALIVE_INTERVAL = 30 * 1000; | ||
this.KEEP_ALIVE_INTERVAL = 20 * 1000; | ||
this.RECONNECT_INTERVAL = 55 * 1000; | ||
this.RETRY_INTERVAL = 20 * 1000; | ||
this.connection = connection; | ||
@@ -44,2 +46,11 @@ this.timeout = timeout; | ||
const response = await this.sendPayload(JsonRpcMapper_1.default.toPayload(method, parameters), true); | ||
this.validateJsonRpcResponse(response); | ||
return response.result; | ||
} | ||
async sendWithRetry(method, parameters) { | ||
const response = await this.sendPayloadWithRetry(JsonRpcMapper_1.default.toPayload(method, parameters), true); | ||
this.validateJsonRpcResponse(response); | ||
return response.result; | ||
} | ||
validateJsonRpcResponse(response) { | ||
const validationResult = JsonRpcResponseValidator_1.default.validate(response); | ||
@@ -49,8 +60,9 @@ if (validationResult instanceof Error) { | ||
} | ||
return response.result; | ||
} | ||
async sendAsync(method, parameters) { | ||
const payload = JsonRpcMapper_1.default.toPayload(method, parameters); | ||
await this.sendPayload(payload, false); | ||
await this.sendPayload(JsonRpcMapper_1.default.toPayload(method, parameters), false); | ||
} | ||
async sendAsyncWithRetry(method, parameters) { | ||
return this.retry(() => this.sendAsync(method, parameters), this.RETRY_INTERVAL, 5); | ||
} | ||
async sendPayload(payload, waitResponse) { | ||
@@ -112,2 +124,20 @@ return new Promise((resolve, reject) => { | ||
} | ||
; | ||
async sendPayloadWithRetry(payload, waitResponse) { | ||
return this.retry(() => this.sendPayload(payload, waitResponse), this.RETRY_INTERVAL, 5); | ||
} | ||
async retry(fn, ms, maxRetries = 5) { | ||
return new Promise((resolve, reject) => { | ||
fn() | ||
.then(resolve) | ||
.catch((error) => { | ||
setTimeout(() => { | ||
if (maxRetries == 1) { | ||
return reject(error); | ||
} | ||
this.retry(fn, ms, maxRetries - 1).then(resolve).catch(reject); | ||
}, ms); | ||
}); | ||
}); | ||
} | ||
_keepAlive() { | ||
@@ -186,3 +216,3 @@ this.sendAsync(this.HEARTBEAT, []) | ||
this.registerEventListeners(); | ||
}, 5000); | ||
}, this.RECONNECT_INTERVAL); | ||
} | ||
@@ -189,0 +219,0 @@ disconnect(code = 0, reason = "") { |
@@ -18,2 +18,3 @@ import { IClientConfig } from "websocket"; | ||
send(method: string, parameters: any[]): Promise<any>; | ||
sendWithRetry(method: string, parameters: any[]): Promise<any>; | ||
sendAsync(method: string, parameters: any[]): Promise<void>; | ||
@@ -20,0 +21,0 @@ subscribe(subscriptionId: string, subscribeMethod: string, topic: string, parameters: any[]): Promise<string>; |
{ | ||
"name": "@haechi-labs/henesis-provider", | ||
"version": "1.0.0-beta.12", | ||
"version": "1.0.0-beta.13", | ||
"description": "henesis-provider", | ||
@@ -43,3 +43,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "58e28f1f05ce7505b45f12b1f0f0bb6a28771311" | ||
"gitHead": "c7a2335755101f17f64248941fd432492b4329f6" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41535
458