fix(#89): fix unsubscribe logic when connected & harden types

This commit is contained in:
tiagosiebler
2021-07-01 23:44:58 +01:00
parent ab3f64579c
commit af4193832f
3 changed files with 47 additions and 45 deletions

View File

@@ -3,10 +3,8 @@ import { DefaultLogger } from '../logger';
import WebSocket from 'isomorphic-ws';
type WsTopicList = Set<string>;
type KeyedWsTopicLists = {
[key: string]: WsTopicList;
};
type WsTopic = string;
type WsTopicList = Set<WsTopic>;
interface WsStoredState {
ws?: WebSocket;
@@ -16,10 +14,9 @@ interface WsStoredState {
subscribedTopics: WsTopicList;
};
export default class WsStore {
private wsState: {
[key: string]: WsStoredState;
}
private wsState: Record<string, WsStoredState>
private logger: typeof DefaultLogger;
constructor(logger: typeof DefaultLogger) {
@@ -35,8 +32,6 @@ export default class WsStore {
if (createIfMissing) {
return this.create(key);
}
return undefined;
}
getKeys(): string[] {
@@ -65,7 +60,7 @@ export default class WsStore {
/* connection websocket */
hasExistingActiveConnection(key) {
hasExistingActiveConnection(key: string) {
return this.get(key) && this.isWsOpen(key);
}
@@ -106,19 +101,19 @@ export default class WsStore {
return this.get(key, true)!.subscribedTopics;
}
getTopicsByKey(): KeyedWsTopicLists {
const result = {};
getTopicsByKey(): Record<string, WsTopicList> {
const result = {};
for (const refKey in this.wsState) {
result[refKey] = this.getTopics(refKey);
}
return result;
}
addTopic(key: string, topic: string) {
addTopic(key: string, topic: WsTopic) {
return this.getTopics(key).add(topic);
}
deleteTopic(key: string, topic: string) {
deleteTopic(key: string, topic: WsTopic) {
return this.getTopics(key).delete(topic);
}
}
}