mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
OrionAggregator connect on demand
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
# Orion Protocol SDK
|
||||
|
||||
[](https://www.npmjs.com/package/@orionprotocol/sdk)
|
||||
[](https://www.npmjs.com/package/@orionprotocol/sdk)
|
||||
|
||||
# Install
|
||||
|
||||
```console
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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`);
|
||||
|
||||
Reference in New Issue
Block a user