OrionAggregator connect on demand

This commit is contained in:
Aleksandr Kraiz
2022-04-27 14:24:51 +04:00
parent 321c5745c7
commit 2d0cea557c
3 changed files with 18 additions and 7 deletions

View File

@@ -1,5 +1,8 @@
# Orion Protocol SDK
[![npm version](https://img.shields.io/npm/v/@orionprotocol/sdk.svg)](https://www.npmjs.com/package/@orionprotocol/sdk)
[![Downloads](https://img.shields.io/npm/dm/@orionprotocol/sdk.svg)](https://www.npmjs.com/package/@orionprotocol/sdk)
# Install
```console

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.2.8",
"version": "0.3.0",
"description": "Orion Protocol SDK",
"main": "./lib/umd/index.js",
"module": "./lib/esm/index.js",

View File

@@ -90,10 +90,12 @@ class OrionAggregatorWS {
private onError?: (err: string) => void;
constructor(url: string, chainId: SupportedChainId, onError?: (err: string) => void) {
private readonly wsUrl: string;
constructor(wsUrl: string, chainId: SupportedChainId, onError?: (err: string) => void) {
this.wsUrl = wsUrl;
this.chainId = chainId;
this.onError = onError;
this.init(url);
}
sendRaw(data: string | ArrayBufferLike | Blob | ArrayBufferView) {
@@ -120,6 +122,7 @@ class OrionAggregatorWS {
type: T,
subscription: Subscription[T],
) {
if (!this.ws) this.init();
this.send({
T: type,
...('payload' in subscription) && {
@@ -150,11 +153,16 @@ class OrionAggregatorWS {
}
}
init(url: string) {
this.ws = new WebSocket(url);
this.ws.onclose = () => {
destroy() {
this.ws?.close(4000);
delete this.ws;
}
init() {
this.ws = new WebSocket(this.wsUrl);
this.ws.onclose = (e) => {
console.log(`Orion Aggregator ${this.chainId} WS Connection closed`);
this.init(url);
if (e.code !== 4000) this.init();
};
this.ws.onopen = () => {
console.log(`Orion Aggregator ${this.chainId} WS Connection established`);