move topics to store

This commit is contained in:
tiagosiebler
2021-02-06 16:48:04 +00:00
parent d81381d7ec
commit dd7a4a899b
2 changed files with 42 additions and 18 deletions

View File

@@ -1,12 +1,18 @@
import { WsConnectionState } from '../websocket-client';
import { DefaultLogger, Logger } from '../logger';
type WsTopicList = Set<string>;
type KeyedWsTopicLists = {
[key: string]: WsTopicList;
};
interface WsStoredState {
ws?: WebSocket;
connectionState?: WsConnectionState;
activePingTimer?: NodeJS.Timeout | undefined;
activePongTimer?: NodeJS.Timeout | undefined;
subscribedTopics: Set<string>;
subscribedTopics: WsTopicList;
};
export default class WsStore {
@@ -32,6 +38,10 @@ export default class WsStore {
return undefined;
}
getKeys(): string[] {
return Object.keys(this.wsState);
}
create(key: string): WsStoredState | undefined {
if (this.hasExistingActiveConnection(key)) {
this.logger.warning('WsStore setConnection() overwriting existing open connection: ', this.getWs(key));
@@ -91,10 +101,18 @@ export default class WsStore {
/* subscribed topics */
getTopics(key: string): Set<string> {
getTopics(key: string): WsTopicList {
return this.get(key, true)!.subscribedTopics;
}
getTopicsByKey(): KeyedWsTopicLists {
const result = {};
for (const refKey in this.wsState) {
result[refKey] = this.getTopics(refKey);
}
return result;
}
addTopic(key: string, topic: string) {
return this.getTopics(key).add(topic);
}