2024-07-04 17:27:46 +02:00
|
|
|
import { WS } from "./index";
|
|
|
|
|
|
|
|
|
|
export class SubscriptionMap {
|
2024-07-05 12:48:42 +02:00
|
|
|
channelsPerClient = new Map<WS, Set<string>>();
|
|
|
|
|
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) {
|
2024-07-04 18:18:44 +02:00
|
|
|
const channelList = this.getChannelList(ws);
|
|
|
|
|
const clientList = this.getClientList(channel);
|
|
|
|
|
|
|
|
|
|
channelList?.delete(channel);
|
|
|
|
|
if (channelList?.size == 0) {
|
|
|
|
|
this.channelsPerClient.delete(ws);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clientList?.delete(ws);
|
|
|
|
|
if (clientList?.size == 0) {
|
|
|
|
|
this.clientsPerChannel.delete(channel);
|
|
|
|
|
}
|
2024-07-04 16:39:52 +02:00
|
|
|
}
|
|
|
|
|
}
|