From 85f9656f056ead23c5df684a87d4da46f5b46aaf 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 9c97f9e..b1a64df 100644 --- a/backend/src/eventRecipients.ts +++ b/backend/src/eventRecipients.ts @@ -33,7 +33,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); + } }