move topics to store
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user