set => map

This commit is contained in:
Darius
2026-02-05 04:31:54 +01:00
parent 8daeed6b5f
commit cdd2fcb59e
4 changed files with 22 additions and 26 deletions

View File

@@ -1,7 +1,8 @@
import { UUID } from "crypto";
import { logInfo } from "./logger.js";
export type SseClient = {
id: number;
id: UUID;
send: (data: SseEvent) => void;
}
@@ -12,29 +13,26 @@ export type SseEvent = {
}
export class SseService {
private clients = new Set<SseClient>();
private clients: Map<string, SseClient> = new Map();
addClient(client: SseClient): void {
this.clients.add(client);
this.clients.set(client.id, client);
logInfo(
`SSE client connected: ${client.id}. Total clients: ${this.clients.size}`,
);
}
removeClient(clientId: number): void {
const client = [...this.clients].find((c) => c.id === clientId);
if (client) {
this.clients.delete(client);
logInfo(
`SSE client disconnected: ${clientId}. Total clients: ${this.clients.size}`,
);
}
removeClient(clientId: string): void {
this.clients.delete(clientId);
logInfo(
`SSE client disconnected: ${clientId}. Total clients: ${this.clients.size}`,
);
}
notifyClients(event: SseEvent): void {
this.clients.forEach((client) => {
client.send(event);
});
for (const client of this.clients.values()) {
client.send(event);
}
}
getClientCount(): number {