Fix PriceFeed

This commit is contained in:
Aleksandr Kraiz
2023-03-03 22:14:01 +04:00
parent 44bbed0ca1
commit 66f068e7a0
5 changed files with 33 additions and 10 deletions

View File

@@ -11,7 +11,7 @@ import {
import UnsubscriptionType from './UnsubscriptionType';
import type {
SwapInfoBase, AssetPairUpdate, OrderbookItem,
Balance, Exchange, CFDBalance, FuturesTradeInfo, SwapInfo,
Balance, Exchange, CFDBalance, FuturesTradeInfo, SwapInfo, AnyJSON,
} from '../../../types';
import unsubscriptionDoneSchema from './schemas/unsubscriptionDoneSchema';
import assetPairConfigSchema from './schemas/assetPairConfigSchema';
@@ -202,14 +202,14 @@ class OrionAggregatorWS {
}
}
private send(data: unknown) {
if (this.ws?.readyState === 1) {
const jsonData = JSON.stringify(data);
private send(jsonObject: AnyJSON) {
if (this.ws?.readyState === WebSocket.OPEN) {
const jsonData = JSON.stringify(jsonObject);
this.ws.send(jsonData);
this.logger?.(`Sent: ${jsonData}`);
} else {
setTimeout(() => {
this.send(data);
this.send(jsonObject);
}, 50);
}
}
@@ -228,7 +228,7 @@ class OrionAggregatorWS {
const id = type === 'aobus'
? ((subscription as any).payload as string) // TODO: Refactor!!!
: uuidv4();
const subRequest: Partial<Record<string, unknown>> = {};
const subRequest: AnyJSON = {};
subRequest['T'] = type;
subRequest['id'] = id;
@@ -259,7 +259,7 @@ class OrionAggregatorWS {
this.send({
T: UNSUBSCRIBE,
S: subscription,
d: details,
...(details !== undefined) && { d: details },
});
if (subscription.includes('0x')) { // is wallet address (ADDRESS_UPDATE)

View File

@@ -4,6 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
import priceFeedSubscriptions from './priceFeedSubscriptions';
import { tickerInfoSchema, candleSchema } from './schemas';
import priceSchema from './schemas/priceSchema';
import type { AnyJSON } from '../../../types';
type TickerInfo = {
pairName: string
@@ -100,6 +101,17 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
this.init();
}
private send(jsonObject: AnyJSON) {
if (this.ws?.readyState === WebSocket.OPEN) {
const jsonData = JSON.stringify(jsonObject);
this.ws.send(jsonData);
} else {
setTimeout(() => {
this.send(jsonObject);
}, 50);
}
}
init() {
this.isClosedIntentionally = false;
@@ -139,12 +151,13 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
};
this.heartbeatInterval = setInterval(() => {
this.ws?.send('heartbeat');
this.send('heartbeat');
}, 15000);
}
kill() {
this.isClosedIntentionally = true;
this.ws?.close();
clearInterval(this.heartbeatInterval);
}
}