cleaning & param keys
This commit is contained in:
@@ -63,7 +63,7 @@ export interface WebsocketClientOptions extends WSClientConfigurableOptions {
|
|||||||
reconnectTimeout: number;
|
reconnectTimeout: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultWsKey = 'inverse';
|
export const defaultWsKey = 'inverse';
|
||||||
|
|
||||||
const getLinearWsKeyForTopic = (topic: string) => {
|
const getLinearWsKeyForTopic = (topic: string) => {
|
||||||
switch (topic) {
|
switch (topic) {
|
||||||
@@ -138,10 +138,6 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getWsKeyForTopic(topic: string) {
|
|
||||||
return this.isInverse() ? defaultWsKey : getLinearWsKeyForTopic(topic);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove topic/topics from WS subscription list
|
* Remove topic/topics from WS subscription list
|
||||||
*/
|
*/
|
||||||
@@ -163,21 +159,11 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
public close(wsKey: string = defaultWsKey) {
|
public close(wsKey: string = defaultWsKey) {
|
||||||
this.logger.info('Closing connection', loggerCategory);
|
this.logger.info('Closing connection', loggerCategory);
|
||||||
this.setWsState(wsKey, READY_STATE_CLOSING);
|
this.setWsState(wsKey, READY_STATE_CLOSING);
|
||||||
this.clearTimers();
|
this.clearTimers(wsKey);
|
||||||
|
|
||||||
this.getWs(wsKey)?.close();
|
this.getWs(wsKey)?.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private getWsUrl(): string {
|
|
||||||
if (this.options.wsUrl) {
|
|
||||||
return this.options.wsUrl;
|
|
||||||
}
|
|
||||||
if (this.options.linear){
|
|
||||||
return linearEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
|
||||||
}
|
|
||||||
return inverseEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
|
||||||
}
|
|
||||||
|
|
||||||
private async connect(wsKey: string = defaultWsKey): Promise<WebSocket | void> {
|
private async connect(wsKey: string = defaultWsKey): Promise<WebSocket | void> {
|
||||||
try {
|
try {
|
||||||
if (this.wsStore.isWsOpen(wsKey)) {
|
if (this.wsStore.isWsOpen(wsKey)) {
|
||||||
@@ -185,14 +171,14 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
return this.wsStore.getWs(wsKey);
|
return this.wsStore.getWs(wsKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.wsStore.isConnectionState(defaultWsKey, READY_STATE_CONNECTING)) {
|
if (this.wsStore.isConnectionState(wsKey, READY_STATE_CONNECTING)) {
|
||||||
this.logger.error('Refused to connect to ws, connection attempt already active', { ...loggerCategory, wsKey })
|
this.logger.error('Refused to connect to ws, connection attempt already active', { ...loggerCategory, wsKey })
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!this.wsStore.getConnectionState(defaultWsKey) ||
|
!this.wsStore.getConnectionState(wsKey) ||
|
||||||
this.wsStore.isConnectionState(defaultWsKey, READY_STATE_INITIAL)
|
this.wsStore.isConnectionState(wsKey, READY_STATE_INITIAL)
|
||||||
) {
|
) {
|
||||||
this.setWsState(wsKey, READY_STATE_CONNECTING);
|
this.setWsState(wsKey, READY_STATE_CONNECTING);
|
||||||
}
|
}
|
||||||
@@ -203,8 +189,8 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
return this.wsStore.setWs(wsKey, ws);
|
return this.wsStore.setWs(wsKey, ws);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.parseWsError('Connection failed', err);
|
this.parseWsError('Connection failed', err, wsKey);
|
||||||
this.reconnectWithDelay(this.options.reconnectTimeout!);
|
this.reconnectWithDelay(wsKey, this.options.reconnectTimeout!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,10 +239,10 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
private reconnectWithDelay(connectionDelayMs: number) {
|
private reconnectWithDelay(wsKey: string = defaultWsKey, connectionDelayMs: number) {
|
||||||
this.clearTimers();
|
this.clearTimers(wsKey);
|
||||||
if (this.wsStore.getConnectionState(defaultWsKey) !== READY_STATE_CONNECTING) {
|
if (this.wsStore.getConnectionState(wsKey) !== READY_STATE_CONNECTING) {
|
||||||
this.setWsState(defaultWsKey, READY_STATE_RECONNECTING);
|
this.setWsState(wsKey, READY_STATE_RECONNECTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -277,7 +263,7 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
}, this.options.pongTimeout);
|
}, this.options.pongTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
private clearTimers(wsKey: string = defaultWsKey) {
|
private clearTimers(wsKey: string) {
|
||||||
this.clearPingTimer(wsKey);
|
this.clearPingTimer(wsKey);
|
||||||
this.clearPongTimer(wsKey);
|
this.clearPongTimer(wsKey);
|
||||||
}
|
}
|
||||||
@@ -350,7 +336,7 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
this.emit('reconnected');
|
this.emit('reconnected');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setWsState(defaultWsKey, READY_STATE_CONNECTED);
|
this.setWsState(wsKey, READY_STATE_CONNECTED);
|
||||||
|
|
||||||
this.wsStore.getKeys().forEach(wsKey =>
|
this.wsStore.getKeys().forEach(wsKey =>
|
||||||
this.requestSubscribeTopics(wsKey, [...this.wsStore.getTopics(wsKey)])
|
this.requestSubscribeTopics(wsKey, [...this.wsStore.getTopics(wsKey)])
|
||||||
@@ -410,4 +396,18 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
private setWsState(wsKey: string, state: WsConnectionState) {
|
private setWsState(wsKey: string, state: WsConnectionState) {
|
||||||
this.wsStore.setConnectionState(wsKey, state);
|
this.wsStore.setConnectionState(wsKey, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getWsUrl(): string {
|
||||||
|
if (this.options.wsUrl) {
|
||||||
|
return this.options.wsUrl;
|
||||||
|
}
|
||||||
|
if (this.options.linear){
|
||||||
|
return linearEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
||||||
|
}
|
||||||
|
return inverseEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
||||||
|
}
|
||||||
|
|
||||||
|
private getWsKeyForTopic(topic: string) {
|
||||||
|
return this.isInverse() ? defaultWsKey : getLinearWsKeyForTopic(topic);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user