Skip to content

Commit

Permalink
chore(): tidier types for pending subs, bump axios to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Jan 21, 2025
1 parent 50bae8b commit 13cc5dd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
15 changes: 8 additions & 7 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
"target": "ES6",
"module": "commonjs"
},
"exclude": [
"node_modules",
"**/node_modules/*",
"coverage",
"doc"
"node_modules",
"**/node_modules/*",
"coverage",
"doc",
"examples/ignored/*"
]
}
}
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Stefan Aebischer <os@pixtron.ch> (https://pixtron.ch)"
],
"dependencies": {
"axios": "^1.6.6",
"axios": "^1.7.9",
"isomorphic-ws": "^4.0.1",
"ws": "^7.4.0"
},
Expand Down
36 changes: 19 additions & 17 deletions src/util/BaseWSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ export abstract class BaseWebsocketClient<

private timeOffsetMs: number = 0;

private pendingTopicSubscriptionRequests: {
[key in TWSKey]?: {
[requestKey: string]:
| undefined
| WsKeyPendingTopicSubscriptions<TWSRequestEvent>;
};
} = {};
/**
* A nested wsKey->request key store.
* pendingTopicSubscriptionRequests[wsKey][requestKey] = WsKeyPendingTopicSubscriptions<TWSRequestEvent>
*/
private pendingTopicSubscriptionRequests: Record<
string,
Record<string, undefined | WsKeyPendingTopicSubscriptions<TWSRequestEvent>>
> = {};

constructor(
options?: WSClientConfigurableOptions,
Expand Down Expand Up @@ -281,9 +282,9 @@ export abstract class BaseWebsocketClient<
const requestKey = requestData.requestKey;

// Should not be possible to see a requestKey collision in the current design, since the req ID increments automatically with every request, so this should never be true, but just in case a future mistake happens...
const existingWsKeyPendingRequests =
this.getWsKeyPendingSubscriptionStore(wsKey);
if (existingWsKeyPendingRequests[requestKey]) {

const pendingSubReqs = this.getWsKeyPendingSubscriptionStore(wsKey);
if (pendingSubReqs[requestKey]) {
throw new Error(
'Implementation error: attempted to upsert pending topics with duplicate request ID!',
);
Expand All @@ -294,8 +295,8 @@ export abstract class BaseWebsocketClient<
resolver: TopicsPendingSubscriptionsResolver<TWSRequestEvent>,
rejector: TopicsPendingSubscriptionsRejector<TWSRequestEvent>,
) => {
const store = this.getWsKeyPendingSubscriptionStore(wsKey);
store[requestKey] = {
const pendingSubReqs = this.getWsKeyPendingSubscriptionStore(wsKey);
pendingSubReqs[requestKey] = {
requestData: requestData.requestEvent,
resolver,
rejector,
Expand All @@ -305,8 +306,8 @@ export abstract class BaseWebsocketClient<
}

protected removeTopicPendingSubscription(wsKey: TWSKey, requestKey: string) {
const store = this.getWsKeyPendingSubscriptionStore(wsKey);
delete store[requestKey];
const pendingSubReqs = this.getWsKeyPendingSubscriptionStore(wsKey);
delete pendingSubReqs[requestKey];
}

private clearTopicsPendingSubscriptions(
Expand All @@ -315,9 +316,10 @@ export abstract class BaseWebsocketClient<
rejectReason: string,
) {
if (rejectAll) {
const wsKeyPendingRequests = this.getWsKeyPendingSubscriptionStore(wsKey);
for (const requestKey in wsKeyPendingRequests) {
const request = wsKeyPendingRequests[requestKey];
const pendingSubReqs = this.getWsKeyPendingSubscriptionStore(wsKey);

for (const requestKey in pendingSubReqs) {
const request = pendingSubReqs[requestKey];
request?.rejector(request.requestData, rejectReason);
}
}
Expand Down

0 comments on commit 13cc5dd

Please sign in to comment.