Files
UDN/auxilary.ts
T

41 lines
1006 B
TypeScript
Raw Normal View History

2024-07-04 17:27:46 +02:00
import { WS } from "./index";
export class SubscriptionMap {
private channelsPerClient = new Map<WS, Set<string>>();
private clientsPerChannel = new Map<string, Set<WS>>();
2024-07-04 16:39:52 +02:00
// prepare
2024-07-04 17:27:46 +02:00
private prepareChannelSet(ws: WS) {
if (this.channelsPerClient.has(ws)) return;
this.channelsPerClient.set(ws, new Set());
2024-07-04 16:39:52 +02:00
}
2024-07-04 17:27:46 +02:00
private prepareClientSet(channel: string) {
if (this.clientsPerChannel.has(channel)) return;
this.clientsPerChannel.set(channel, new Set());
2024-07-04 16:39:52 +02:00
}
// utility
2024-07-04 17:27:46 +02:00
getChannelList(ws: WS) {
return this.channelsPerClient.get(ws);
2024-07-04 16:39:52 +02:00
}
2024-07-04 17:27:46 +02:00
getClientList(channel: string) {
return this.clientsPerChannel.get(channel);
2024-07-04 16:39:52 +02:00
}
2024-07-04 17:27:46 +02:00
// modifications
set(ws: WS, channel: string) {
this.prepareChannelSet(ws);
this.prepareClientSet(channel);
2024-07-04 16:39:52 +02:00
2024-07-04 17:27:46 +02:00
this.getChannelList(ws)?.add(channel);
this.getClientList(channel)?.add(ws);
2024-07-04 16:39:52 +02:00
}
2024-07-04 17:27:46 +02:00
delete(ws: WS, channel: string) {
this.getChannelList(ws)?.delete(channel);
this.getClientList(channel)?.delete(ws);
2024-07-04 16:39:52 +02:00
}
}