import { Message } from '../Message.js'; import { ClientId, createClientId } from './ClientId.js'; import { SessionConnection } from './SessionConnection.js'; export class SessionClients< ServerMessage extends Message, ClientMessage extends Message > { private readonly clients = new Map< ClientId, SessionConnection >(); get(id: ClientId) { return this.clients.get(id) || null; } /** * Ids are issued here and nowhere else. `add` used to accept one, which is how * a client could pick its own session id: `SessionServer` passed straight * through whatever arrived on the wire. */ add() { const client = new SessionConnection( createClientId(), ); this.clients.set(client.id, client); return client; } remove(id: ClientId) { if (!this.clients.has(id)) { return false; } const client = this.clients.get(id)!; this.clients.delete(id); client.destroy(); return true; } }