From 4acd8fe9ce68bd28f198798e4c88ed444e28cd29 Mon Sep 17 00:00:00 2001 From: sksmagr23 Date: Fri, 7 Jun 2024 11:06:08 +0530 Subject: [PATCH] Server: Implement doSendEvents Function The doSendEvents function has been added to eventRecipients.ts. This function sends the events array as the response body and clears the event queue after sending. Additionally, the client is removed from the clients map after events are sent. Fixes: #61 --- backend/src/eventRecipients.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/src/eventRecipients.ts b/backend/src/eventRecipients.ts index 64db5c4..c96fefd 100644 --- a/backend/src/eventRecipients.ts +++ b/backend/src/eventRecipients.ts @@ -37,7 +37,13 @@ export function scheduleSend(clientId: ClientId, event: AppEvent) { //todo: Enqueue the event for sending. } -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export function doSendEvent(clientId: ClientId) { - //todo: Send all the events in the queue to the client, only if the response object is available. +export function doSendEvents(clientId: ClientId) { + const res = clients.get(clientId); + const events = eventQueue.get(clientId) || []; + + if (res && events.length > 0) { + res.json({ events }); + eventQueue.set(clientId, []); + clients.delete(clientId); + } }