-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: Add mechanism to enqueue events.
The implementation of the respective functions will be done through subsequent PRs. Also added the AppEvent type.
- Loading branch information
Showing
2 changed files
with
32 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters