Skip to content

Commit

Permalink
events: Add mechanism to enqueue events.
Browse files Browse the repository at this point in the history
The implementation of the respective functions will be done through
subsequent PRs.
Also added the AppEvent type.
  • Loading branch information
kuv2707 committed Jun 3, 2024
1 parent 57e2de8 commit 199d751
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
37 changes: 29 additions & 8 deletions backend/eventRecipients.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
// this module is responsible for handling the clients currently connected to the server.
// It stores the clients in a Map object, where the key is the client's user_id and the value is the client's http response object.
// It stores the clients in a Map object, where the key is the client id and the value is the
// client's http response object.
// When sending an event, we first push the event to a queue. We need to use a queue because it
// is possible that the client hasn't sent the next polling request yet, i.e, we don't have a response
// object for that client yet. When the client sends the next polling request, we will send the events
// from the queue to the client.

import { Response } from 'express';

const clients = new Map<string, Response>();
type ClientId = string;

export function addClient(userId: string, res: Response) {
clients.set(userId, res);
const clients = new Map<ClientId, Response>();
const eventQueue = new Map<ClientId, AppEvent[]>();

export function addClient(clientId: ClientId, res: Response) {
// todo: We can immediately send any events that are in the queue.
clients.set(clientId, res);
eventQueue.set(clientId, []);
}

export function removeClient(clientId: ClientId) {
clients.delete(clientId);
eventQueue.delete(clientId);
}

export function getClient(clientId: ClientId) {
return clients.get(clientId);
}

export function removeClient(userId: string) {
clients.delete(userId);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function scheduleSend(clientId: ClientId, event: AppEvent) {
//todo: Enqueue the event for sending.
}

export function getClient(userId: string) {
return clients.get(userId);
// 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.
}
3 changes: 3 additions & 0 deletions backend/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ type GameEvent =
card: UNOCard;
};
};

// Represent all the events that can be sent to the client
type AppEvent = GameEvent;
//todo: Add more events

0 comments on commit 199d751

Please sign in to comment.