Skip to content

Commit

Permalink
chore(): ws client tidying and misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Jan 24, 2025
1 parent 9c12727 commit ee23e13
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 261 deletions.
9 changes: 8 additions & 1 deletion src/util/BaseWSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,11 @@ export abstract class BaseWebsocketClient<
/**
* Try sending a string event on a WS connection (identified by the WS Key)
*/
public tryWsSend(wsKey: TWSKey, wsMessage: string) {
public tryWsSend(
wsKey: TWSKey,
wsMessage: string,
throwExceptions?: boolean,
) {
try {
this.logger.trace('Sending upstream ws message: ', {
...WS_LOGGER_CATEGORY,
Expand All @@ -934,6 +938,9 @@ export abstract class BaseWebsocketClient<
wsKey,
exception: e,
});
if (throwExceptions) {
throw e;
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/util/websockets/websocket-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CategoryV5,
WebsocketClientOptions,
WsKey,
WsTopic,
} from '../../types';

import { DefaultLogger } from '../logger';
Expand Down Expand Up @@ -660,3 +661,43 @@ export function getNormalisedTopicRequests(
}
return normalisedTopicRequests;
}

/**
* Groups topics in request into per-wsKey groups
* @param normalisedTopicRequests
* @param wsKey
* @param isPrivateTopic
* @returns
*/
export function getTopicsPerWSKey(
normalisedTopicRequests: WsTopicRequest[],
wsKey?: WsKey,
isPrivateTopic?: boolean,
): {
[key in WsKey]?: WsTopicRequest<WsTopic>[];
} {
const perWsKeyTopics: { [key in WsKey]?: WsTopicRequest<WsTopic>[] } = {};

// Sort into per wsKey arrays, in case topics are mixed together for different wsKeys
for (const topicRequest of normalisedTopicRequests) {
const derivedWsKey =
wsKey ||
getWsKeyForTopic(
this.options.market,
topicRequest.topic,
isPrivateTopic,
topicRequest.category,
);

if (
!perWsKeyTopics[derivedWsKey] ||
!Array.isArray(perWsKeyTopics[derivedWsKey])
) {
perWsKeyTopics[derivedWsKey] = [];
}

perWsKeyTopics[derivedWsKey]!.push(topicRequest);
}

return perWsKeyTopics;
}
Loading

0 comments on commit ee23e13

Please sign in to comment.