import type { Server } from "socket.io"; import { createLogger } from "../logger/logger.controller.js"; import { FLUSH_INTERVAL, MAX_HISTORY, roomBuffers, roomFlushTimers, roomHistory, } from "./roomCache.socket.js"; import { type RoomId, roomDefinition } from "./roomDefinitions.socket.js"; // get the db data if not exiting already const log = createLogger({ module: "socket.io", subModule: "roomService" }); let ioRef: Server | null = null; export const registerRoomService = (io: Server) => { ioRef = io; }; export const hasRoomMembers = (roomId: string): boolean => { if (!ioRef) return false; return (ioRef.sockets.adapter.rooms.get(roomId)?.size ?? 0) > 0; }; export const getRoomMemberCount = (roomId: string): number => { if (!ioRef) return 0; return ioRef.sockets.adapter.rooms.get(roomId)?.size ?? 0; }; export const preseedRoom = async (roomId: RoomId) => { if (roomHistory.has(roomId)) { if (!roomId.includes("dock")) { return roomHistory.get(roomId); } } const roomDef = roomDefinition[roomId] as any; if (!roomDef) { log.error({}, `Room ${roomId} is not defined`); } const latestData = await roomDef.seed(MAX_HISTORY); roomHistory.set(roomId, latestData); return latestData; }; export const createRoomEmitter = (io: Server) => { const addDataToRoom = (roomId: RoomId, payload: T[]) => { if (!roomHistory.has(roomId)) { roomHistory.set(roomId, []); } const history = roomHistory.get(roomId)!; history?.push(payload); if (history?.length > MAX_HISTORY) { history?.shift(); } if (!roomBuffers.has(roomId)) { roomBuffers.set(roomId, []); } roomBuffers.get(roomId)!.push(payload); if (!roomFlushTimers.has(roomId)) { const timer = setTimeout(() => { const buffered = roomBuffers.get(roomId) || []; if (buffered.length > 0) { io.to(roomId).emit("room-update", { roomId, payloads: buffered, // ✅ array now }); } roomBuffers.set(roomId, []); roomFlushTimers.delete(roomId); }, FLUSH_INTERVAL); roomFlushTimers.set(roomId, timer); } }; return { addDataToRoom }; };