handling multiple subscriptions confirmations received at once

This commit is contained in:
Caius Citiriga
2023-05-09 04:02:13 +02:00
parent 4b854ddb39
commit 75a97fc9d4

View File

@@ -1070,7 +1070,7 @@ export class WebsocketClient extends EventEmitter {
} }
private updatePendingTopicSubscriptionStatus(wsKey: string, msg: any) { private updatePendingTopicSubscriptionStatus(wsKey: string, msg: any) {
const req_id = msg['req_id'] as string; const requestsIds = msg['req_id'] as string;
const pendingTopicsSubscriptions = this.pendingTopicsSubscriptions.find( const pendingTopicsSubscriptions = this.pendingTopicsSubscriptions.find(
(s) => s.wsKey === wsKey, (s) => s.wsKey === wsKey,
); );
@@ -1081,33 +1081,38 @@ export class WebsocketClient extends EventEmitter {
} }
const subscriptionSuccess = isTopicSubscriptionSuccess(msg); const subscriptionSuccess = isTopicSubscriptionSuccess(msg);
const splitRequestsIds = requestsIds.split(',');
if (!subscriptionSuccess) { if (!subscriptionSuccess) {
pendingTopicsSubscriptions.failedTopicsSubscriptions.add(req_id); splitRequestsIds.forEach((req_id) =>
pendingTopicsSubscriptions.failedTopicsSubscriptions.add(req_id),
);
} }
this.removeTopicPendingSubscription(wsKey, req_id); splitRequestsIds.forEach((req_id) => {
this.removeTopicPendingSubscription(wsKey, req_id);
if ( if (
!pendingTopicsSubscriptions.pendingTopicsSubscriptions.size && !pendingTopicsSubscriptions.pendingTopicsSubscriptions.size &&
!pendingTopicsSubscriptions.failedTopicsSubscriptions.size !pendingTopicsSubscriptions.failedTopicsSubscriptions.size
) { ) {
// all topics have been subscribed successfully, so we can resolve the subscription request // all topics have been subscribed successfully, so we can resolve the subscription request
pendingTopicsSubscriptions.resolver(); pendingTopicsSubscriptions.resolver();
this.clearTopicsPendingSubscriptions(wsKey); this.clearTopicsPendingSubscriptions(wsKey);
} }
if ( if (
!pendingTopicsSubscriptions.pendingTopicsSubscriptions.size && !pendingTopicsSubscriptions.pendingTopicsSubscriptions.size &&
pendingTopicsSubscriptions.failedTopicsSubscriptions.size pendingTopicsSubscriptions.failedTopicsSubscriptions.size
) { ) {
// not all topics have been subscribed successfully, so we reject the subscription request // not all topics have been subscribed successfully, so we reject the subscription request
// and let the caller handle the situation by providing the list of failed subscriptions requests // and let the caller handle the situation by providing the list of failed subscriptions requests
const failedSubscriptionsMessage = `(${[ const failedSubscriptionsMessage = `(${[
...pendingTopicsSubscriptions.failedTopicsSubscriptions, ...pendingTopicsSubscriptions.failedTopicsSubscriptions,
].toString()}) failed to subscribe`; ].toString()}) failed to subscribe`;
pendingTopicsSubscriptions.rejector(failedSubscriptionsMessage); pendingTopicsSubscriptions.rejector(failedSubscriptionsMessage);
this.clearTopicsPendingSubscriptions(wsKey); this.clearTopicsPendingSubscriptions(wsKey);
} }
});
} }
private onWsClose(event, wsKey: WsKey) { private onWsClose(event, wsKey: WsKey) {