fix ws subscribe/unsubscribe workflows, which were repeating requests to each active ws connection unintentionally

This commit is contained in:
tiagosiebler
2022-10-09 19:21:46 +01:00
parent 6a925f18e1
commit 9c3a37e7b0
2 changed files with 21 additions and 22 deletions

View File

@@ -116,22 +116,21 @@ export class WebsocketClient extends EventEmitter {
public subscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) {
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
topics.forEach((topic) =>
this.wsStore.addTopic(
getWsKeyForTopic(this.options.market, topic, isPrivateTopic),
topic
)
);
topics.forEach((topic) => {
const wsKey = getWsKeyForTopic(
this.options.market,
topic,
isPrivateTopic
);
// Persist topic for reconnects
this.wsStore.addTopic(wsKey, topic);
// attempt to send subscription topic per websocket
this.wsStore.getKeys().forEach((wsKey: WsKey) => {
// if connected, send subscription request
if (
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
) {
return this.requestSubscribeTopics(wsKey, [
...this.wsStore.getTopics(wsKey),
]);
return this.requestSubscribeTopics(wsKey, [topic]);
}
// start connection process if it hasn't yet begun. Topics are automatically subscribed to on-connect
@@ -157,21 +156,21 @@ export class WebsocketClient extends EventEmitter {
*/
public unsubscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) {
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
topics.forEach((topic) =>
this.wsStore.deleteTopic(
getWsKeyForTopic(this.options.market, topic, isPrivateTopic),
topic
)
);
topics.forEach((topic) => {
const wsKey = getWsKeyForTopic(
this.options.market,
topic,
isPrivateTopic
);
// Remove topic from persistence for reconnects
this.wsStore.deleteTopic(wsKey, topic);
this.wsStore.getKeys().forEach((wsKey: WsKey) => {
// unsubscribe request only necessary if active connection exists
if (
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
) {
this.requestUnsubscribeTopics(wsKey, [
...this.wsStore.getTopics(wsKey),
]);
this.requestUnsubscribeTopics(wsKey, [topic]);
}
});
}