From 4d49081fe59a2dd5b561ac345e676b4e53e0c18f Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Fri, 22 Jul 2022 17:54:24 +0400 Subject: [PATCH] Added logger to WS --- .eslintrc.js | 7 +++++++ package.json | 2 +- src/services/OrionAggregator/ws/index.ts | 11 ++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index f42aa78..0a2b5eb 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -26,6 +26,13 @@ module.exports = { '@typescript-eslint', ], rules: { + "no-param-reassign": [ + "error", + { + "props": true, + "ignorePropertyModificationsFor": ["acc", "prev"] + } + ], "camelcase": "off", '@typescript-eslint/consistent-type-assertions': [ 'error', diff --git a/package.json b/package.json index 005e498..76ddb81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.12.14", + "version": "0.12.15", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index e064054..02b58cb 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -158,10 +158,13 @@ class OrionAggregatorWS { public onError?: (err: string) => void; + private logger?: (message: string) => void; + private readonly wsUrl: string; - constructor(wsUrl: string, onInit?: () => void, onError?: (err: string) => void) { + constructor(wsUrl: string, logger?: (msg: string) => void, onInit?: () => void, onError?: (err: string) => void) { this.wsUrl = wsUrl; + this.logger = logger; this.onInit = onInit; this.onError = onError; } @@ -268,7 +271,11 @@ class OrionAggregatorWS { init(isReconnect = false) { this.isClosedIntentionally = false; this.ws = new WebSocket(this.wsUrl); + this.ws.onerror = (err) => { + this.logger?.(`OrionAggregatorWS: ${err.message}`); + }; this.ws.onclose = () => { + this.logger?.(`OrionAggregatorWS: connection closed ${this.isClosedIntentionally ? 'intentionally' : ''}`); if (!this.isClosedIntentionally) this.init(true); }; this.ws.onopen = () => { @@ -286,9 +293,11 @@ class OrionAggregatorWS { } }); } + this.logger?.(`OrionAggregatorWS: connection opened${isReconnect ? ' (reconnect)' : ''}`); }; this.ws.onmessage = (e) => { const { data } = e; + this.logger?.(`OrionAggregatorWS: received message: ${e.data.toString()}`); const rawJson: unknown = JSON.parse(data.toString()); const messageSchema = z.union([