fix ws subscribe/unsubscribe workflows, which were repeating requests to each active ws connection unintentionally
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "bybit-api",
|
"name": "bybit-api",
|
||||||
"version": "3.0.2",
|
"version": "3.1.0",
|
||||||
"description": "Complete & robust node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & integration tests.",
|
"description": "Complete & robust node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & integration tests.",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"types": "lib/index.d.ts",
|
"types": "lib/index.d.ts",
|
||||||
|
|||||||
@@ -116,22 +116,21 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
public subscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) {
|
public subscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) {
|
||||||
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
|
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
|
||||||
|
|
||||||
topics.forEach((topic) =>
|
topics.forEach((topic) => {
|
||||||
this.wsStore.addTopic(
|
const wsKey = getWsKeyForTopic(
|
||||||
getWsKeyForTopic(this.options.market, topic, isPrivateTopic),
|
this.options.market,
|
||||||
topic
|
topic,
|
||||||
)
|
isPrivateTopic
|
||||||
);
|
);
|
||||||
|
|
||||||
// attempt to send subscription topic per websocket
|
// Persist topic for reconnects
|
||||||
this.wsStore.getKeys().forEach((wsKey: WsKey) => {
|
this.wsStore.addTopic(wsKey, topic);
|
||||||
|
|
||||||
// if connected, send subscription request
|
// if connected, send subscription request
|
||||||
if (
|
if (
|
||||||
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
|
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
|
||||||
) {
|
) {
|
||||||
return this.requestSubscribeTopics(wsKey, [
|
return this.requestSubscribeTopics(wsKey, [topic]);
|
||||||
...this.wsStore.getTopics(wsKey),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// start connection process if it hasn't yet begun. Topics are automatically subscribed to on-connect
|
// 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) {
|
public unsubscribe(wsTopics: WsTopic[] | WsTopic, isPrivateTopic?: boolean) {
|
||||||
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
|
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
|
||||||
topics.forEach((topic) =>
|
topics.forEach((topic) => {
|
||||||
this.wsStore.deleteTopic(
|
const wsKey = getWsKeyForTopic(
|
||||||
getWsKeyForTopic(this.options.market, topic, isPrivateTopic),
|
this.options.market,
|
||||||
topic
|
topic,
|
||||||
)
|
isPrivateTopic
|
||||||
);
|
);
|
||||||
|
|
||||||
this.wsStore.getKeys().forEach((wsKey: WsKey) => {
|
// Remove topic from persistence for reconnects
|
||||||
|
this.wsStore.deleteTopic(wsKey, topic);
|
||||||
|
|
||||||
// unsubscribe request only necessary if active connection exists
|
// unsubscribe request only necessary if active connection exists
|
||||||
if (
|
if (
|
||||||
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
|
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
|
||||||
) {
|
) {
|
||||||
this.requestUnsubscribeTopics(wsKey, [
|
this.requestUnsubscribeTopics(wsKey, [topic]);
|
||||||
...this.wsStore.getTopics(wsKey),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user